Jest: Hide console logging for passing tests and show it for failures

Created on 28 Jul 2017  ·  47Comments  ·  Source: facebook/jest


Do you want to request a feature or report a bug?

feature

What is the current behavior?

When you run jest --watch it will show console logging (unless you use --silent).

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

What is the expected behavior?

It would be super helpful to only see console logging for failing tests because that's when you need it the most. For passing tests, the console logs could be hidden.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

$ jest --version && node --version && yarn --version
v20.0.4
v6.10.3
0.27.5

Mac OS X 10.12.5

jest.config.js:

module.exports = {
  collectCoverageFrom: ['src/**/*.{js,jsx}'],
  coveragePathIgnorePatterns: [
    '<rootDir>/node_modules/',
    '<rootDir>/src/core/server/webpack-isomorphic-tools-config.js',
    '<rootDir>/src/locale/',
  ],
  moduleDirectories: [
    'src',
    'node_modules',
  ],
  moduleFileExtensions: [
    'js',
    'json',
    'jsx',
  ],
  moduleNameMapper: {
    // Prevent un-transpiled react-photoswipe code being required.
    '^photoswipe$': '<rootDir>/node_modules/photoswipe',
    // Use the client-side logger by default for tests.
    '^core/logger$': '<rootDir>/src/core/client/logger',
    // Alias tests for tests to be able to import helpers.
    '^tests/(.*)$': '<rootDir>/tests/$1',
    // Replaces the following formats with an empty module.
    '^.+\\.(scss|css|svg|woff|woff2|mp4|webm)$': '<rootDir>/tests/emptyModule',
  },
  setupTestFrameworkScriptFile: '<rootDir>/tests/setup.js',
  testPathIgnorePatterns: [
    '<rootDir>/node_modules/',
    '<rootDir>/(assets|bin|config|coverage|dist|docs|flow|locale|src)/',
  ],
  testMatch: [
    '**/[Tt]est(*).js?(x)',
    '**/__tests__/**/*.js?(x)',
  ],
  transform: {
    '^.+\\.js$': 'babel-jest',
    // This transforms images to be a module that exports the filename.
    // Tests can assert on the filenname.
    '^.+\\.(jpg|jpeg|gif|png)$': '<rootDir>/tests/fileTransformer',
  },
  transformIgnorePatterns: [
    '<rootDir>/node_modules/',
  ],
  verbose: false,
};

Most helpful comment

Agreed, Having a a flag to hide console output for PASS test and leave it for FAILED test would be a great addition to make test output more readable

All 47 comments

You can write a custom reporter. Cc @aaronabramov

Hi, thanks for the helpful info.

However, I tried writing a custom reporter and ran into a few snags:

  • There is no easy way to inherit all the functionality from the default reporter (informative test output, etc) and I'd rather not re-implement it all from scratch
  • When I include the default reporter in my config, pass --silent in the CLI (to make the default reporter hide console logging), and add my custom reporter to my config, I don't see an easy way in my custom reporter to print the logging. It seems that because of the --silent option, the reporter classes no longer have access to the buffered console.

Because of this, I'd like to propose a patch to Jest that will introduce a config value to show the console only on failing tests. Would you consider such a patch?

This obviously needs tests and it will need to check for a config value but here is the general idea (which is working):

diff --git a/packages/jest-cli/src/reporters/default_reporter.js b/packages/jest-cli/src/reporters/default_reporter.js
index 08d4a9f2..adedbdd3 100644
--- a/packages/jest-cli/src/reporters/default_reporter.js
+++ b/packages/jest-cli/src/reporters/default_reporter.js
@@ -176,7 +176,8 @@ class DefaultReporter extends BaseReporter {
       this.log(getResultHeader(result, config));

       const consoleBuffer = result.console;
-      if (consoleBuffer && consoleBuffer.length) {
+      const testFailed = result.numFailingTests > 0;
+      if (testFailed && consoleBuffer && consoleBuffer.length) {
         this.log(
           '  ' +
             TITLE_BULLET +

i actually like this idea, but there's many things that we need to consider

we need to add some information about the hidden output

PASS __tests__/my_test.js (hidden output)

we should also disable it when running a few tests or a single test (i guess pretty much only enable it for a full test run)

@cpojer do you have any thoughts on this?

I think this behavior is confusing and I would prefer Jest to be consistent in what it outputs per test, regardless of state.

@cpojer for me, it's confusing to try and find console messages relating to my test that failed :/ If you can suggest better ways to achieve that then please do.

As a compromise, would you accept a patch that exposes DefaultReporter in jest.js so I can extend it? Otherwise, I'd have to copy and paste the world to implement this feature in a custom reporter.

This is what my test output looks like:

screen shot 2017-11-05 at 16 11 29

I can't get rid of the warnings because of https://github.com/facebook/flow/issues/4673, and fortunately there are only a couple log messages, but if I want to add more logging it will get much worse.

I second that @miracle2k, when you're getting a lot of warnings and errors from dependancies it makes it much harder to find failed tests. It would be nice to have a flag you could pass that would only return the list of failed tests.

Agreed, Having a a flag to hide console output for PASS test and leave it for FAILED test would be a great addition to make test output more readable

I would agree. I am currently working on a project with a large group of tests and the output of passing tests makes the workflow harder when debugging.

Agreed on the flag that hide console output for PASS tests.

PASS __tests__/my_test.js (hidden output)

Can we get this addition reconsidered by any chance?

We now have a way of running just failing tests, which should cover this use case. See #4886 (available in jest 22)

We now have a way of running just failing tests, which should cover this use case.

It only partially covers the case. For example, if 5 out of 100 tests fail in a suite with lots of logging, you could re-run only the failing tests to make sense of the console output. However, if you were hiding the logging for passing tests all along then you wouldn't have needed to re-run the tests.

Also, re-running only failing tests has a downside in that it won't catch any new test failures introduced by code edits.

If the core team doesn't want to implement this feature, could someone please consider my proposal for a compromise? This proposal would allow me to more easily write a custom reporter to implement console hiding. I can make the patch but I don't want to submit a pull request if it won't get accepted.

@kumar303 please send a PR, seems fairly not-complicated to maintain :)

@kumar303 did you end up submitting a PR? I'd like to have this as well.

I still intend to submit one but I haven't been able to find time between my other work priorities. If anyone else beats me to it, please let me know so I can help test it out!

My idea was to export DefaultReporter from jest.js so that a custom reporter could extend it. I was thinking to start by changing this line to something more like:

const testFailed = result.numFailingTests > 0;
if (testFailed && consoleBuffer && consoleBuffer.length) {
  // Log console output
}

I'm sure it would need more tweaks after that.

@kumar303 How I can add your code to my jest config?

I'm also interested in this. Following @kumar303's idea I was able to write a custom reporter that extends default_reporter easily enough (though brittle, since I'm importing it directly from jest-cli/build/reporters/default_reporter), and then transform result.console as I see fit (in this case I let the user set a minimum log level).

It works fine except for one thing -- when running a single test, console messages are not buffered. This is mentioned here: https://github.com/facebook/jest/issues/2080

So in these scenarios there's no ability to influence the console output from within a custom reporter. So I don't think @thymikee's original suggestion to use a custom reporter to manage console output works universally, unless we can also have some way to force jest to always buffer console output.

Happy to expose our default reporter in a more clean way.

Mind opening up a separate issue about force buffering console.logs? Should be consistent

Is there a significant downside to having a global config variable like showLogsForFailedTests: true? The default value changes nothing from how Jest currently works and the value of false would make reading through tests much more pleasant.

Is this issue closed because something was done to fix it or is it closed because 30+ people are imagining they have a problem with Jest that they aren't actually having?

boy, I thought this is pretty much a standard approach to show only failed log for jest ... is this still an issue?

Need this. It really disrupts.

@willdurand I tried your reporter. I'm not sure what I could have done wrong since all I did was select that file as the reporter. All it did was prevent this from showing at the end of the tests:

Test Suites: 48 passed, 48 total
Tests:       78 passed, 78 total
Snapshots:   73 passed, 73 total

All logs during the tests still showed up.

Did you perhaps run it for only a single test? See my previous comment and
associated bug report. It's not possible to capture logs with a reporter
when only one test is run, this is the real sticking point.

On Wed, Aug 8, 2018, 7:08 PM jazoom notifications@github.com wrote:

@willdurand https://github.com/willdurand I tried your reporter. I'm
not sure what I could have done wrong since all I did was select that file
as the reporter. All it did was prevent this from showing at the end of the
tests:

Test Suites: 48 passed, 48 total
Tests: 78 passed, 78 total
Snapshots: 73 passed, 73 total

All logs during the tests still showed up.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/facebook/jest/issues/4156#issuecomment-411582223, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAi-gO9_uEJPO4xnhkpfGore_hEX81fUks5uO29bgaJpZM4OnBQQ
.

@jamietre I ran it for the exact same command I used for the test that ran 48 suites.

Edit: to clarify, I ran the same command twice. The only difference with the custom reporter was it didn't print this summary at the end:

Test Suites: 48 passed, 48 total
Tests:       78 passed, 78 total
Snapshots:   73 passed, 73 total

@jazoom the reporter won't have any effect if you have verbose: true in your config. Try setting that to false.

@jamietre I agree. It is odd behavior that a single test run does not capture or display any console output whatsoever (https://github.com/facebook/jest/issues/6441).

It's also not helpful how jest does not group console output by test (https://github.com/facebook/jest/issues/2080). A custom reporter can only show output for the suite (i.e. a test file), not a specific failing test.

@jazoom also make sure you completely restart jest after installing or changing the reporter. This may not be obvious since jest will recognize changes to other source files while running (but not reporters).

@kumar303 it's not set to verbose

What do you mean by "completely restart"? It's just a script that runs.

What do you mean by "completely restart"?

I meant that if you're in jest's watch mode you need to exit.

Okay. I don't use watch mode.

ah yes, indeed. I did not notice that but for some reasons, Jest does not output the final summary at the bottom, once all test suites have been run.

I suppose that's because we extend the DefaultReporter and not the SummaryReporter, maybe..

@willdurand I tried your configuration that successfully hides the logs. However, the terminal is not cleared any more (previous logs) and all the logs stack the ones below the others.

Note: By just exporting the DefaultReporter class, I fall back to the default logging but the logs stack as well

I toyed a bit around with the FingersCrossedReporter from @kumar303 / @willdurand
However as some other noted, it doesn't print the test summary at the end of the tests. This is because (I think) the default Jest settings use two reporters - the DefaultReporter and the SummaryReporter.
Now I'm unable to directly import the SummaryReporter in my jest-configuration as it's default-exported and that doesn't seem importable. I've gotten around it by re-exporting it from another file.

//summary-reporter.js
const SummaryReporter = require('@jest/reporters/build/summary_reporter')
  .default;
module.exports = SummaryReporter;
//log-on-fail-reporter.js
Content: https://gist.github.com/GeeWee/71db0d9911b4a087e4b2486386168b05
Same as reporter above, but with updated import paths for the new jest structure

Jest configuration

    "reporters": [
      "<rootDir>/src/test-reporters/log-on-fail-reporter.js",
      "<rootDir>/src/test-reporters/summary-reporter.js"
    ],

Edit: After playing around with it for a bit, I see that this logs for the entire describe block, if a single test fails though.

we'll fix support for default export for jest 25.

you can also do

//summary-reporter.js
const {SummaryReporter} = require('@jest/reporters')
module.exports = SummaryReporter;

We might wanna add @jest/reporters/SummaryReporter etc files though, so you don't need the intermediary js file... Wanna open up a separate feature request for that?

Why is this feature request closed?
It seem that many people find it reasonable to have this feature included in out-of-the-box jest. At least as a configuration option.
Can you @kumar303 open it again?

Can you @kumar303 open it again?

Heh. No, I don't have access. This was the rationale for closing: https://github.com/facebook/jest/issues/4156#issuecomment-324638718 I agree that it's an essential feature. I am surprised how core jest devs can live without it but maybe they don't write code with bugs so they don't need logs.

Please reopen. We need this too.

Why is this not possible yet?

Seems like no-brainer functionality to me. Most of our tests produce at least a page of console text each, it's incredibly annoying having to wade through it to find the failed tests

I know no other way to register my support for this behavior than to leave a comment. I know 👍 and the like are less useful so this is the best I feel I can do. Thanks for everything from everyone involved! I followed all the threads and I'm looking forward to this whenever people have cycles to get the people what they want lol.

The thing that this feature has not been implemented for almost three years, when it is this necessary, is kind of strange.

Based on snippets I found around the internet I came up with a global config for this; see https://stackoverflow.com/questions/58936650/javascript-jest-how-to-show-logs-from-test-case-only-when-test-fails/61909588#61909588

Hopefully this helps somebody.

Might want to take a look at https://github.com/AtakamaLLC/capio for async capture.

Please reopen and implement it as an optional config.
I would do it myself if there is a realistic chance of getting a PR accepted.
In my view, writing a custom reporter for this "tiny" feature-request would be an unsustainable maintenance overhead.

Although this feature-request is neither "clean" nor "consistent", it is still highly needed by many people.

Was this page helpful?
0 / 5 - 0 ratings