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.
To fix console. log statements output nothing in Jest, we can set the silent option to false . to set silent to false given that test is a script in package. json that runs jest .
To disable console inside unit tests with Jest, we can use the --silent option or set the console methods to mocked functions. to run jest with the --silient to disable console output when running tests.
The console. log() is a function that writes a message to log on the debugging console, such as Webkit or Firebug. In a browser you will not see anything on the screen. It logs a message to a debugging console.
As said above, the console. log is asynchronous and non-blocking, so it would not slow your application too much except one tick for the function invocation. But it is a good habit to use some module to turn some logs of certain level off when deploy it in production instead of using console.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With