Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide console logging for passing tests and show it for failures #4156

Closed
kumar303 opened this issue Jul 28, 2017 · 49 comments
Closed

Hide console logging for passing tests and show it for failures #4156

kumar303 opened this issue Jul 28, 2017 · 49 comments

Comments

@kumar303
Copy link

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,
};
@thymikee
Copy link
Collaborator

You can write a custom reporter. Cc @aaronabramov

@thymikee
Copy link
Collaborator

Here's example from our tests: https://github.com/facebook/jest/blob/master/integration_tests/custom_reporters/reporters/test_reporter.js.

@kumar303
Copy link
Author

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 +

@aaronabramov
Copy link
Contributor

aaronabramov commented Aug 15, 2017

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?

@cpojer
Copy link
Member

cpojer commented Aug 24, 2017

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

@cpojer cpojer closed this as completed Aug 24, 2017
@kumar303
Copy link
Author

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

@miracle2k
Copy link

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 facebook/flow#4673, and fortunately there are only a couple log messages, but if I want to add more logging it will get much worse.

@design1online
Copy link

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.

@garcianavalon
Copy link

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

@andrewrady
Copy link

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.

@saintplay
Copy link

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?

@SimenB
Copy link
Member

SimenB commented Dec 27, 2017

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

@kumar303
Copy link
Author

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.

@kumar303
Copy link
Author

kumar303 commented Dec 28, 2017

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.

@thymikee
Copy link
Collaborator

thymikee commented Jan 2, 2018

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

@martijnthe
Copy link

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

@kumar303
Copy link
Author

kumar303 commented Feb 6, 2018

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.

@dortamiguel
Copy link

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

@jamietre
Copy link
Contributor

jamietre commented Jun 8, 2018

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

@SimenB
Copy link
Member

SimenB commented Jun 9, 2018

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

@jazoom
Copy link

jazoom commented Jul 27, 2018

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?

@adamchenwei
Copy link

adamchenwei commented Jul 30, 2018

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

@jamietre
Copy link
Contributor

jamietre commented Aug 8, 2018 via email

@jazoom
Copy link

jazoom commented Aug 8, 2018

@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

@kumar303
Copy link
Author

kumar303 commented Aug 9, 2018

@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 (#6441).

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

@kumar303
Copy link
Author

kumar303 commented Aug 9, 2018

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

@jazoom
Copy link

jazoom commented Aug 9, 2018

@kumar303 it's not set to verbose

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

@kumar303
Copy link
Author

kumar303 commented Aug 9, 2018

What do you mean by "completely restart"?

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

@jazoom
Copy link

jazoom commented Aug 9, 2018

Okay. I don't use watch mode.

@willdurand
Copy link

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.

@willdurand
Copy link

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

bfirsh added a commit to arxiv-vanity/engrafo that referenced this issue Sep 7, 2018
Implementation is due to jestjs/jest#4156

This is to get around travis-ci/travis-ci#4704
but is also much neater.
bfirsh added a commit to arxiv-vanity/engrafo that referenced this issue Sep 7, 2018
Implementation is due to jestjs/jest#4156

This is to get around travis-ci/travis-ci#4704
but is also much neater.
bfirsh added a commit to arxiv-vanity/engrafo that referenced this issue Sep 7, 2018
Implementation is due to jestjs/jest#4156

This is to get around travis-ci/travis-ci#4704
but is also much neater.
@Morikko
Copy link

Morikko commented Mar 25, 2019

@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

@GeeWee
Copy link

GeeWee commented May 9, 2019

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.

@SimenB
Copy link
Member

SimenB commented May 9, 2019

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?

@jarl-dk
Copy link

jarl-dk commented Sep 27, 2019

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?

@kumar303
Copy link
Author

Can you @kumar303 open it again?

Heh. No, I don't have access. This was the rationale for closing: #4156 (comment) 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.

@demisx
Copy link

demisx commented Nov 20, 2019

Please reopen. We need this too.

@ExtraGG
Copy link

ExtraGG commented Nov 26, 2019

Why is this not possible yet?

@zacaj
Copy link

zacaj commented Jan 20, 2020

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

@omgoshjosh
Copy link

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.

@joffarex
Copy link

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

@geekysquirrel
Copy link

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.

@earonesty
Copy link

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

@fkirc
Copy link

fkirc commented Oct 24, 2020

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.

@siilike
Copy link

siilike commented Jan 9, 2021

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',
	],
...

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests