Created
August 9, 2018 12:20
-
-
Save jamietre/e918032bea1a1a2814fb6ba16736fc34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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