jest: add option to fail tests that print errors and warnings
Do you want to request a feature or report a bug?
Feature.
What is the current behavior?
Tests pass even if they print errors and warnings to the console.
What is the expected behavior?
It would be nice if we could force the test to fail.
Our test suite prints lots of things like this:
console.warn node_modules/moment/moment.js:293
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
console.error node_modules/fbjs/lib/warning.js:33
Warning: Each child in an array or iterator should have a unique "key" prop.
(node:15601) UnhandledPromiseRejectionWarning: SyntaxError: The string did not match the expected pattern.
console.error node_modules/fbjs/lib/warning.js:33
Warning: `value` prop on `input` should not be null. Consider using an empty string to clear the component or `undefined` for uncontrolled components.
These are all legitimate errors that we should fix, yet our automated tests pass. If Jest could be made to fail, that would encourage developers to keep the tests clean.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 7
- Comments: 24 (7 by maintainers)
Here’s the variant I landed on, using Node’s
Util.format
function to preserve console string formatting via arguments in the error output:cc @akx, re:
This can already be achieved with a couple lines of code.
@apaatsio how would you suggest someone implement this so that it runs across all test suites without having to include it in each individual test suite?
To get it working I’ve ended up with this ugly thing
requirements:
Is there a preferred way to do this globally that doesn’t involve adding a
beforeEach/All
orafterEach/All
to every test file? I want all tests to fail if anyconsole.error
is called.I’m looking at the solution from here, which seems to do the job:
@lukeapage Thank you for contributing that solution (https://github.com/facebook/jest/issues/6121#issuecomment-412063935) – just a note about it:
You’re going to get
Maximum Call Stack Size Exceeded
when you call originalError/originalWarning, because you’re still spying on the parentglobal.console
object, so it recurses.May I suggest either using console.log in their place if you want the whole stack trace, or just letting jest spit out the original error/warning to stdout, and throwing the error as usual?
See below for revised code:
When I tried this option I got an error on executing because of Create React App:
So I wrote a bash script to for manually looking for consoles and warnings (it can be found here).
Later I discovered that options not supported by React Create App on jest configurations can be used via the command line. So one could run:
instead of changing package.json, and the solution pointed by @stevula would work.
Thanks, @stevula – that’s close, but the problem is that the
%s
interpolation supported by the console isn’t supported by thrown messages:You can see the interpolated message too, but it’s a little cumbersome. 😃
setupFiles
, you can throw instead of assertBuilding up on @axelboc’s variant:
@stevula For what it’s worth, that’s exactly what works for me