Is there any way to make a certain test silent (show console output only if it fails) in Jest? Looks like jest --silent
makes all tests silent, which is not desirable. I need to simulate an error and make sure it's correctly handled, but unfortunately there is a third-party code that calls console.error in this case. But I don't want disable console.error completely, since I need to see the details if it fails.
Asked
Active
Viewed 4,468 times
3

Andreas Köberle
- 106,652
- 57
- 273
- 297

Dmitry Druganov
- 2,088
- 3
- 23
- 32
-
Did you manage to figure out how to mute individual tests? I have one test that generates a LOT of output and would very much like to silence it (but only when successful). – Bart Feb 18 '22 at 18:14
1 Answers
1
I don't know if it's possible to silence a test case directly, but you could always use a different testRegex
with a different jest configuration for your test files.
For example, you could adopt the following naming convention for your test files:
myfile.{js,jsx,ts,tsx}
: Jest should not suppress any output (console.log, errors, etc)myfile.silent.{js,jsx,ts,tsx}
: Jest should suppress the output
and, in your package.json
, define your test scripts this way:
"test": "yarn test:default && yarn test:silent",
"test:silent": "jest __tests__\\/.*\\(silent\\)\\.\\(jsx?\\|tsx?\\)$ --silent",
"test:default": "jest __tests__\\/\\(?!.*silent\\).*\\.\\(jsx?\\|tsx?\\)$",
It's not easy to read regexes in the package.json
, so I made this:

jackdbd
- 4,583
- 3
- 26
- 36