308

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.

jacefarm
  • 6,747
  • 6
  • 36
  • 46
Hina Dawood
  • 3,201
  • 2
  • 10
  • 8

22 Answers22

194

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

ArrH
  • 2,131
  • 1
  • 13
  • 5
  • 1
    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. – Anders Jun 02 '22 at 06:59
  • 2
    even after using this flag, in watch mode, when tests are re-triggered, console logs are not emitted by jest – gaurav5430 Mar 28 '23 at 16:47
129

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

David Dehghan
  • 22,159
  • 10
  • 107
  • 95
  • 5
    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` – Gpack Sep 10 '21 at 02:14
  • 11
    This answer is incorrect for Jest 27. `verbose` is related to how the test outputs pass/fail results. `silent` is for suppressing console output. https://jestjs.io/docs/cli#--silent – jcollum Oct 15 '21 at 20:24
  • 2
    this (`--verbose false`) doesn't work for me, I still don't see the console.log statements output – ajendrex Nov 10 '21 at 13:55
  • 74
    Nobody else thinks it's weird that to get _more_ output you have to set `verbose` mode to `false`? – aroth Feb 22 '22 at 05:24
79

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 { error } from "console";

error("This is an error");

As easy as that.

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
72

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.

Apurva Mulay
  • 853
  • 6
  • 4
  • 7
    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: https://github.com/facebook/jest/issues/2441#issuecomment-286248619 – philraj Aug 13 '18 at 18:33
  • 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! – nkhil Jul 09 '20 at 13:43
  • 1
    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 – Crysfel Aug 05 '21 at 21:30
  • 2
    This answer is incorrect for Jest 27. `verbose` is related to how the test outputs pass/fail results. `silent` is for suppressing console output. https://jestjs.io/docs/cli#--silent – jcollum Oct 15 '21 at 20:24
29

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",
    ],
Dharman
  • 30,962
  • 25
  • 85
  • 135
Erkka Mutanen
  • 639
  • 1
  • 10
  • 14
19

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

Radu
  • 524
  • 1
  • 6
  • 19
19

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

Isaac
  • 12,042
  • 16
  • 52
  • 116
  • 1
    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. – li ki Jan 29 '22 at 18:22
17

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

Liran Brimer
  • 3,418
  • 1
  • 28
  • 23
15

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.

Tom Wayson
  • 1,187
  • 1
  • 12
  • 21
15

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

Webstorm Jest panel

Ambroise Rabier
  • 3,636
  • 3
  • 27
  • 38
12

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

dgh500
  • 131
  • 1
  • 5
6

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

  • 2
    Can you please provide more info? Where is the flag applied? – Alireza Jul 23 '21 at 01:56
  • 2
    @Alireza Not really a flag, but when you do this `test.only( ... )`. remove the only function. See the [docs](https://jestjs.io/docs/api) – Isaac Pak Nov 03 '21 at 15:19
6

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.

jcollum
  • 43,623
  • 55
  • 191
  • 321
6

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

Joan
  • 101
  • 1
  • 9
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

Joe Van Leeuwen
  • 266
  • 3
  • 8
  • 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. – dnuske Jun 27 '23 at 22:02
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.

pegasus013
  • 51
  • 4
3

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

Aakash Verma
  • 3,705
  • 5
  • 29
  • 66
2

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.

jfunk
  • 7,176
  • 4
  • 37
  • 38
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.

ksadjad
  • 593
  • 8
  • 20
0

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.

Matt Zeunert
  • 16,075
  • 6
  • 52
  • 78
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.

Sam Tags
  • 1
  • 1
-4

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

Joel
  • 46
  • 1
  • 4