question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

add option to fail tests that print errors and warnings

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:24 (6 by maintainers)

github_iconTop GitHub Comments

45reactions
greypantscommented, Sep 9, 2019

Here’s the variant I landed on, using Node’s Util.format function to preserve console string formatting via arguments in the error output:

// setupTests.js
import { format } from "util";

const error = global.console.error;

global.console.error = function(...args) {
  error(...args);
  throw new Error(format(...args));
};

cc @akx, re:

Error: Warning: Received %s for a non-boolean attribute %s.

45reactions
apaatsiocommented, May 3, 2018

This can already be achieved with a couple lines of code.

const spy = jest.spyOn(global.console, 'error')
// ...
expect(spy).not.toHaveBeenCalled();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a jest config that will fail tests on console.warn?
Now, jest will fail when messages are passed to console.warn or console.error . create-react-app Docs - Initializing Test Environment.
Read more >
Warnings as errors and tests - Dashbit Blog
Let's discuss the --warnings-as-errors option, how we can use it during compilation and tests, and finally our recommendations for using it ...
Read more >
Catch warnings in Jest tests | Ben Ilegbodu
How to configure Jest to fail tests on console warnings and errors.
Read more >
C# Compiler Options - errors and warnings - Microsoft Learn
C# Compiler Options to report errors and warnings · To get information about an error or warning, you can look up the error...
Read more >
How to test a function that's expected to throw error in jest…
This caused the error I was getting. Here are the correct ways to write the unit tests: Based on the warning on the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found