42

From what I know console.log() should work without problem printing to the STDOUT of my console, when running a script.

But in my case I have NPM configured to run Jest when issuing npm test from the shell, and any console.log() inside the test files doesn't print anything on the screen. I tried also to use process.stdout.write() but still I get no custom output when running npm test.

How am I supposed to debug stuff in my test scripts? I can't figure out if it is a problem from Node, from NPM, or from Jest. There's a Jest issue that looks similar to mine but still I cannot solve and get to output a simple string; while there rest of Jest output is echoed as usual.

Anybody experienced a similar problem?

EDIT 1: I tried running npm test -- --runInBand but the log doesn't appear.

Note

trying to run the command repeatedly like

console.log('foo...');console.log('bar..');console.log('baz..');

I can sometimes see the log, but this is then overwritten/hidden by the rest of the following Jest output.

Kamafeather
  • 8,663
  • 14
  • 69
  • 99
  • I would like to know this too, it's very annoying, if it's of use to you, the thing I do presently is where I would have a console.log I instead write to a file. – Countingstuff Jan 14 '19 at 23:48
  • 1
    @Countingstuff seems quite tricky. Take a look at the issues from the *Jest* repository that I've linked, to track what's going on (since 2 years; and in the last 2 hours ). Try the `--verbose` command option, it should help!! (please let us know if it works!) I also thought about writing to a file.... but then I told myself I'd rather spend my energies in trying to not surrender to it – Kamafeather Jan 15 '19 at 00:04
  • I use this command and the logs are being printed: `npm run test src/rentcafe.com/ -- --runInBand --verbose` – Gus Aug 02 '19 at 03:44
  • If you read you'd see I tried those already – There's a Github issue regarding the problem, linked in my answer; I suppose they resolved it and released a patched version. – Kamafeather Aug 02 '19 at 10:09
  • 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:35

3 Answers3

26

From the linked issue since the problem is going on since years...

There is a pull request in the alpha release that is going to fix this problem. Looks like the last refining is being worked in the past few hours (see Github issue link).

However I found a solution that works for my simplest case:

  • just run enabling verbose mode and the log will magically appear!
npm test -- --verbose=true

This will not fix in every case; maybe async/multi-thread/etc will still have problems (please report your experiences, so I can update the answer); fo r example adding a process.exit(1) after the log will hide it again.

Trying to press ctrl + c before the process.exit() runs (with the right timing...) will show that the log is actually there and being overridden.

I will update the answer with news, but probably this will help others in starting with Node/NPM/Jest setups!


Jest's GitHub issue mention some useful details/suggestions:

Jest replaces the global console module; this apparently is the source of the issue. And also the fact that Jest will set it to true automatically if you're running a single test. upside_down_face

Kamafeather
  • 8,663
  • 14
  • 69
  • 99
13

This isn't clean, but I've had luck with it, hopefully it'll help you until a fix is published: log some extra newlines after the log that's getting overwritten:

console.log('I want to be seen!');
console.log('\n\n\n\n');
helloitsjoe
  • 6,264
  • 3
  • 19
  • 32
1

I had this issue and it turns out that in my case, the problem was calling process.exit(1) immediately after writing to the console. When you do this during Jest tests, the output seems to be lost.

The (not-entirely-satisfying) workaround that I settled on is:

console.warn('stuff I want to write to the console');

if (process.env.NODE_ENV === 'test') {
  setTimeout(() => process.exit(1), 1000);
} else {
  process.exit(1);
}

For this, the NODE_ENV environment variable would need to be set to test in your Jest setup script or elsewhere.

Aurast
  • 3,189
  • 15
  • 24