Skip to content

Instantly share code, notes, and snippets.

@jamietre
Created August 9, 2018 12:20
Show Gist options
  • Select an option

  • Save jamietre/e918032bea1a1a2814fb6ba16736fc34 to your computer and use it in GitHub Desktop.

Select an option

Save jamietre/e918032bea1a1a2814fb6ba16736fc34 to your computer and use it in GitHub Desktop.

Revisions

  1. jamietre created this gist Aug 9, 2018.
    44 changes: 44 additions & 0 deletions jest-log-suppressing-reporter.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    /**
    * 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;