Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Jest swallow console.log statements? Is there a way to change this?

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.

like image 517
Joseph Fraley Avatar asked Dec 12 '16 02:12

Joseph Fraley


People also ask

Does console log work in Jest?

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 .

How do I get rid of console error in 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.

What does console log () do?

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.

Does console log slow down Nodejs?

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.


2 Answers

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.

like image 86
Derek Hill Avatar answered Oct 20 '22 09:10

Derek Hill


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.

like image 34
Tamlyn Avatar answered Oct 20 '22 09:10

Tamlyn