Does Jest swallow console.log statements? Is there

2019-02-21 08:23发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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.