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.

Make a passing `.toThrow()` not dump to `console.error`

See original GitHub issue

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

Writing a test for an exception still dumps that exception to the output, which is very confusing: It took me a while to realize that buried after screen and screens of red stack traces, my tests were showing as PASS…

I don’t want to use --silent during development, as I’d use the ability to quickly console.log the shape of objects I’m trying to expect, etc…

As a work-around, I’m creating a spy on console.error, but that of course also swallows and exceptions that I’m not yet testing for:

beforeAll(() => {
  jest.spyOn(console, 'error').mockImplementation(() => undefined);
});

afterAll(() => {
  console.error.mockRestore();
});

What is the expected behavior?

It would be great if exceptions happening inside .toThrow would not get dumped to console IF they PASS the assertion. (I guess that would take some kind of buffering of the output?)

Please provide your exact Jest configuration

const path = require('path');

const isCI = process.argv.includes('--ci');

module.exports = {
  rootDir: '../packages',
  testMatch: ['**/__tests__/*.{js,jsx}', '**/*.{spec,test}?(s).{js,jsx}'],

  collectCoverage: isCI,
  collectCoverageFrom: [
    '**/src/**?(/)*.{js,jsx}',
    // ignore:
    '!**/__stories__/**',
    '!**/*.stories.{js,jsx}',
  ],
  coverageDirectory: path.resolve(__dirname, '..', 'coverage'),
  coverageThreshold: isCI
    ? {
        global: {
          branches: 100,
          functions: 100,
          lines: 100,
          statements: 0,
        },
      }
    : undefined,
};

Run npx envinfo --preset jest in your project directory and paste the results here

  System:
    OS: macOS High Sierra 10.13.3
    CPU: x64 Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz
  Binaries:
    Node: 8.8.1
    Yarn: 1.5.1
    npm: 5.4.2
  npmPackages:
    jest:
      wanted: ^22.4.2
      installed: 22.4.2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:15
  • Comments:10

github_iconTop GitHub Comments

13reactions
davidgilbertsoncommented, Feb 24, 2020

I’m doing this, thanks @kevinbarabash for the link.

export const expectToThrow = func => {
  // Even though the error is caught, it still gets printed to the console
  // so we mock that out to avoid the wall of red text.
  jest.spyOn(console, 'error');
  console.error.mockImplementation(() => {});

  expect(func).toThrow();

  console.error.mockRestore();
};
9reactions
kevinbarabashcommented, Jul 22, 2018

I’m experiencing this when using enzyme’s mount() to render a component that throws in its render() function. I’m guess it has something to do with the way that jsdom’s error handling works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest Test logs an error when I try to use toThrow() on an Async ...
So far, I have three tests which seem to be passing. However, after the tests finish running I get an error UnhandledPromiseRejectionWarning: ...
Read more >
Error handling - Express.js
Error Handling refers to how Express catches and processes errors that occur ... If you pass an error to next() and you do...
Read more >
A Definitive Guide to Handling Errors in JavaScript - Kinsta
Trying to create an array of illegal lengths via the Array constructor. Passing bad values to numeric methods like toExponential() , toPrecision ...
Read more >
How to write a test which expects an Error to be thrown in ...
In my Node module I have the following code: throw new Error("Parsing is not possible");. Now I try to write a test which...
Read more >
throw - JavaScript - MDN Web Docs - Mozilla
If no catch block exists among caller functions, the program will terminate. ... console.error(e.message, e.name); // pass exception object to err handler }...
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 Hashnode Post

No results found