380

console.log statements output nothing at all in Jest. This was working for me yesterday, and all of sudden, it's not working today. I have made zero changes to my config and haven't installed any updates.

I'm not using the --forceExit option. Still seeing this issue.

1

24 Answers 24

275

Jest suppresses the console log message by default. In order to show the console log message, set silent option to false at the command line

set --silent=false in the command line:

npm run test -- --silent=false

Sign up to request clarification or add additional context in comments.

2 Comments

I vote for this one! Also search your code/command for the text --SILENT as there are so many different places with jest where this can be added, if you can't find any silent parameter then you should see logging.
even after using this flag, in watch mode, when tests are re-triggered, console logs are not emitted by jest
162

This is a pretty old question and still there's no accepted answer. However, none of the suggested solutions worked for me (settings like --silent --verbose etc.). The main problem is that Jest changes the global console object. So, the easiest solution is to not use the global console object.

Instead import dedicated log functions from the console module and work with those:

import { log, error } from "console";

error("This is an error");
log("Something to log");

As easy as that.

7 Comments

worked for me but I had to add // @ts-ignore above the import
The only solution that works for me, thank you! I did import { log } from 'console', and use log instead of console.log.
Since JEST redefines the global console object, this is the only workaround. Works perfectly.
Simple. Works (in Jest 29). +10 if I could.
Or use a dedicated logging module...
|
135

You can run both options together like this --watch --verbose false if you want to also be watching the files and see the output.

for one time runs just do --verbose false

8 Comments

Note that the order of options matter. For example, if you use -t, you need to put --verbose false before the -t: jest --verbose false -t my-test
This answer is incorrect for Jest 27. verbose is related to how the test outputs pass/fail results. silent is for suppressing console output. jestjs.io/docs/cli#--silent
this (--verbose false) doesn't work for me, I still don't see the console.log statements output
Nobody else thinks it's weird that to get more output you have to set verbose mode to false?
YES, ITS WEIRD.
|
74

As per comment on https://github.com/facebook/jest/issues/2441,

Try setting verbose: false (or removing it) in the jest options in package.json.

4 Comments

Just to add to this, if you have --verbose in your command line flags you need to remove that as well. I thought this answer was incorrect until I realized that. Thanks. Also, the relevant comment is here: github.com/facebook/jest/issues/2441#issuecomment-286248619
Removing "verbose": true didn't solve it for me, I had to change it to false "verbose": false, which seems to have fixed it. Thank you!
In my case there was a --silent flag in the package.json file, had to remove it in order to see the console.log messages
This answer is incorrect for Jest 27. verbose is related to how the test outputs pass/fail results. silent is for suppressing console output. jestjs.io/docs/cli#--silent
39

Try using console.debug() instead.

Run console.debug('Message here', yourValueHere) inside test function and it should show in the console output when running test script. You can verify if it works using Ctrl+F and find Message here in the standard output.

This does the trick of showing output in the console, while it is not an answer quite on how to use console.log I understand.

I am running @testing-library/jest-dom and jest-junit 12.0.0 as devDependencies. jest-junit has a minimal configuration of

  "jest-junit": {
    "usePathForSuiteName": "true"
  },

in package.json. This is mainly to configure coverage reporting. jest is configured like this:

  "jest": {
    "testMatch": [
      "**/__tests__/**/*.[jt]s?(x)",
      "**/?(*.)+(spec|test).[jt]s?(x)",
      "!**/utilities.ts",
    ],

Comments

22

One of the potential reason that logging is not printing is due to console.log has been mocked. Something as below

// jest-setup.js
global.console = {
  // eslint-disable-next-line no-undef
  log: jest.fn(), // console.log are ignored in tests
  // log: console.log,

  // Keep native behaviour for other methods, use those to print out things in your own tests, not `console.log`
  error: console.error,
  warn: console.warn,
  info: console.info,
  debug: console.debug,
};
// package.json
  "jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ],
    "setupFilesAfterEnv": [
      "@testing-library/jest-native/extend-expect",
      "<rootDir>/src/config/jest-setup.js"
    ],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.test.{ts,tsx}"
    ]
  },

This is commonly used if you wish to disable console.log in jest

1 Comment

I've seen global.console = { log: process.env['JEST_VERBOSE'] ? console.log : jest.fn(), }; in some projects. I think it's a better way to control the console output.
19

Check for your command line flags in package.json to see that you don't have --silent in there.

Comments

18

If using Webstorm with Jest configuration, click on the file name instead of the test name.

Webstorm Jest panel

2 Comments

That is the case for not just webstorm, but any jetbrain IDE
This of all the things is what I needed to know. Thank you so much!
17

in addition to --verbose option which can cause this as mentioned, be aware that the --watch may also cause this bug.

Comments

17

Also be sure that your jest config does not have silent: true. In my case, I didn't realize that someone else had added that to our config.

I don't see it in the list of config options, but the command line flag is documented here.

Comments

14

Having tried a few of the config options in the previous replies, using console.debug() instead of console.log() worked.

1 Comment

Also works in vitest 🙏
10

This is what worked for me: jest --verbose true

Comments

9

According to the v27 docs silent is what you want here. verbose false (the default) prevents Jest from outputting the result of every test in a hierarchy while silent true (the default) will:

Prevent tests from printing messages through the console.

Use npx jest --silent false if you want to run Jest with that option from the CLI. Tested this just now with console.log and it works as expected.

Comments

8

In my case, the issue was caused by [only] flag in: it.only() or test.only('some text',()=>{})

2 Comments

Can you please provide more info? Where is the flag applied?
@Alireza Not really a flag, but when you do this test.only( ... ). remove the only function. See the docs
5

Tried the advice given regarding jest config settings to no avail. Instead, in my case, the issue seemed related to not awaiting asynchronous code:

test("test", async () => {
  console.log("Does output")

  new Promise(resolve => {
    // some expectation depending on async code
    setTimeout(() => resolve(console.log("Does not output")) , 1)
  })
})

Rather, awaiting the promise does output the async log:

test("test", async () => {
  console.log("Does output")

  await new Promise(resolve => {
    // some expectation depending on async code
    setTimeout(() => resolve(console.log("Does output")) , 1)
  })
})

Possibly related background: https://github.com/facebook/jest/issues/2441

1 Comment

this was my case as well. I have the console.debug right before the expect(calls.length).toBe(1); and only after having await expect(calls.length).toBe(1); the console.debug worked. 🤯
4

Try using console.info() which is an alias for console.log(). I tried almost all the above answers but still console.log() didn't worked for me by any means. So, used console.info() which did the work.

Comments

4

On MacOS with jest version 26.6.3 I had to append --silent="false"

Comments

4

Nothing else was working for me, not verbose or runInBand etc.. Finally I tried:

--useStderr=true

Even though it is a setting for errors, I was able to see console.log('test log'); after setting this option.

1 Comment

This is a useful answer & solves the printing issue efficiently.
1

In my case the problem was that the logs where made when the module is required, so before the start of an actual test case. Change from a top-level import to using require inside the test case fixed the problem.

Comments

1

In my case the problem was importing the functions from the compiled version (present in dist folder) instead of the src folder. And therefore it was using the old version. So rebuilding the project and/or importing from src fixed my issue.

Comments

1

As of November 2023 with jest v. 29.7.0 you need to set the env variable DEBUG to test, like this:

DEBUG=jest npx jest -t 'my test' 

...and it results in the test running with the console.log outputs, for example:

  console.log
    Testing redirect from /foo to /foo/bar

      at Object.log (src/index.test.js:143:15)

  console.log
    Map(21) {

(...)

But it's not specific to running a single test - running all of them gives the same result.

2 Comments

Where is this documented?
I can't find any docs about it, @smac89... I found this suggestion here: github.com/jestjs/jest/issues/8208#issuecomment-595188558
0

In my case transform-remove-console plugin is present in my babel.config.js

if (api.env() !== 'development') {
  plugins.push('transform-remove-console');
}

I remove the plugin and it now works.

Comments

-2

Try to set "verbose: false" in jest.config.js

Comments

-4

renaming my file to index.test.js from index.spec.js did the trick for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.