2 ways to print Jest console debug logs with examples

Sometimes, it is very difficult to debug test case execution for fixing failed test cases in JEST.

I faced console.log not working for printing debug logs. The problem is console.log outputs nothing during test case execution.

Let’s see how to prints logs during test case execution.

Jest prints log using console.log statements

There are two options to enable logging

  • using Jest CLI —silent option

Jest by default suppresses the output of console log statements. It shows nothing instead of printing a log message.

JEST provides CLI option --silent=false to enable print log messages.

jest --silent=false

Other ways we can configure these options

  • npm scripts jest command configured to run all test cases using the below scripts in package.json
{
  "scripts": {
    "test": "jest --config=jest.config.json"
  }
}

Next, You can run the below command to disable suppress logs. -- allows you to pass command line options to scripts tags

npm run test -- --silent=false

You can still configure the option directly in the scripts

{
"scripts": {
"test": "jest --silent --config=jest.config.json"
}
}
  • For local dependency without global installation, use the below command
./node_modules/.bin/jest --silent

For yarn users

yarn jest --silent true

For npx uses without installation

npx jest --silent true
  • using the verbose mode option

Jest provides a --verbose CLI option to output the verbose of the log messages during execution.

To enable verbose using the command line

jest --verbose true

If you are using npm scripts, test configured as given below in package.json

{
"scripts": {
"test": "jest --silent"
}
}

Next, You can run the below command

npm test -- --verbose=true

For local dependency without global installation, use the below command

./node_modules/.bin/jest --verbose true

For yarn users

yarn jest --verbose true

For npx uses without installation

npx jest --verbose true