42

Does Jest swallow console.log output?

// __tests__/log.test.js

it('logs', () => {
  console.log('hey') // expect to see "hey" printed in terminal
})

// terminal output
$ jest --forceExit
PASS  __tests__/log.test.js
✓ logs (1ms) # where's "hey"?

The main reason I care is that I'm writing some async beforeAll and afterAll stuff, and I want to use console.log statements to debug the order of events.

Joseph Fraley
  • 1,360
  • 1
  • 10
  • 26
  • 2
    `--useStderr` worked for me for version `v22.4.2` https://jestjs.io/docs/en/cli.html#--usestderr https://jestjs.io/docs/en/cli.html#options – Rishiraj Purohit Feb 25 '20 at 12:18
  • Does this answer your question? [Console.log statements output nothing at all in Jest](https://stackoverflow.com/questions/48695717/console-log-statements-output-nothing-at-all-in-jest) – Michael Freidgeim Jul 03 '20 at 22:40

5 Answers5

13

This seems to be an ongoing issue.

With Node 10.7.0 and Jest 23.4.1, adding verbose: false to the jest config (per this suggestion) worked for me.

Edit

Now that I've gone to Jest 23.6 I also need to pass TERM=dumb as an environment variable, per Tamlyn's answer.

Derek Hill
  • 5,965
  • 5
  • 55
  • 74
12

Update: this issue should be fixed as of Jest 24.

Another partial solution to the current bug affecting tests in --watch mode is to pass TERM=dumb as an environment variable.

TERM=dumb jest --watch

This has a small price in that it no longer clears the console before each test run so you have to scroll to see the results.

Tamlyn
  • 22,122
  • 12
  • 111
  • 127
10

The problem is that I was using jest --forceExit. Jest's logging model saves all the logs and spits them out later. --forceExit causes the process to bail before it reaches the spit-out-logs point.

Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126
Joseph Fraley
  • 1,360
  • 1
  • 10
  • 26
  • 4
    I am having this issue without using `--forceExit`. =/ Any advice? – thisissami Dec 29 '16 at 00:40
  • 3
    I am not using the `forceExit` setting, and I am still seeing jest swallow console statements. – Matt S Jan 09 '17 at 03:15
  • 4
    Not sure what version of node you're both on but I have the same problem and it seems to be a known issue on node 7.3. https://github.com/facebook/jest/issues/2441 – Nick Cox Jan 09 '17 at 23:29
  • 1
    happens also with the `--bail, -b` option – Yo Ludke Feb 05 '18 at 09:50
  • 1
    Interesting find, thank you! As an alternative to removing forceExit you can also follow up your console.log with something like `await new Promise( resolve => setTimeout( resolve, 100 ) );` (as long as you're using an async function on a supported Node version, that is) – Tim Malone Aug 11 '18 at 04:45
4

I found a work around, despite the fact that sadly neither TERM=dumb, nor verbose: false nor upgrading to version 24 worked (I'm on 24.9). Just spy on the console, and you can see the results. Set it up with this:

const consoleSpy = jest.spyOn(console, 'log')

And then view the calls after running your tests using:

consoleSpy.mock.calls[0][0]

Paul F. Wood
  • 1,453
  • 16
  • 19
3

Try adding --verbose to the Jest arguments.

This worked for me.

Samuel Fekete
  • 314
  • 2
  • 5