Skip to content

Instantly share code, notes, and snippets.

@jamietre
Created August 9, 2018 12:20
Show Gist options
  • Save jamietre/e918032bea1a1a2814fb6ba16736fc34 to your computer and use it in GitHub Desktop.
Save jamietre/e918032bea1a1a2814fb6ba16736fc34 to your computer and use it in GitHub Desktop.
/**
* A reporter that suppresses logs for passing tests
*
* There's no direct way to do this at this point other than a custom reporter. Future
* changes may give us an option to do this without a custom reporter:
*
* https://github.com/facebook/jest/issues/4156
*
* ts-jest seems to not have registered TypeScript extension at the point the reporters are
* registered; this must be JavaScript unless we want to precompile it.
*/
/* eslint flowtype/require-valid-file-annotation: 0, no-console: 0 */
const DefaultReporter = require('jest-cli/build/reporters/default_reporter').default;
const getResultHeader = require('jest-cli/build/reporters/get_result_header').default;
const loglevels = ['debug', 'warn', 'error', 'none'];
class LogSuppressingReporter extends DefaultReporter {
constructor(globalConfig) {
super(globalConfig);
}
printTestFileHeader(testPath, config, result) {
if (result.numFailingTests === 0) {
this.log(getResultHeader(result, this._globalConfig, config));
return;
}
const loglevel = config.globals.__LOGLEVEL__ || 'debug';
if (result.console) {
const loglevelIndex = loglevels.indexOf(loglevel);
result.console = result.console.filter(e => {
return e.message.startsWith('[jest]') || loglevels.indexOf(e.type) >= loglevelIndex;
});
}
return super.printTestFileHeader(testPath, config, result);
}
}
module.exports = LogSuppressingReporter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment