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

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,
};

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 68
  • Comments: 49 (7 by maintainers)

Commits related to this issue

Most upvoted comments

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

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

@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.

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?

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.

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.

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

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.

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

Why is this not possible yet?

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.

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

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.

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.

const { DefaultReporter } = require('@jest/reporters')

class Reporter extends DefaultReporter
{
	constructor()
	{
		super(...arguments)
	}

	printTestFileHeader(_testPath, config, result)
	{
		const console = result.console

		if(result.numFailingTests === 0 && !result.testExecError)
		{
			result.console = null
		}

		super.printTestFileHeader(...arguments)

		result.console = console
	}
}

module.exports = Reporter
...
	// Use this configuration option to add custom reporters to Jest
	reporters:
	[
		'<rootDir>/tests/reporter.js',
	],
...

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?

Need this. It really disrupts.

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 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.

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.

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.

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