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.

The `unhandledRejection` handler is not testable anymore.

See original GitHub issue

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

Regression

What is the current behavior?

Current versions of jest don’t allow testing unhandledRejections. The following test will never succeed:

const foo = require(".");

describe("foo", () => {
  test("bar", done => {
    process.removeAllListeners("unhandledRejection");
    process.on("unhandledRejection", function(error) {
      process.removeAllListeners("unhandledRejection");
      expect(error).toBeInstanceOf(Error);
      done();
    });

    foo();
  });
});

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

I made a repository with two commits with different jest versions showing that it works with an older version but not anymore with the current one. The README describes the repro steps in detail.

What is the expected behavior?

It should be possible to test unhandledRejection and uncaughtExceptions.

Btw I’m well aware that this is a case of https://xkcd.com/1172/ and that we could refactor the code to not rely on unhandledRejection for error reporting of unknown errors that happen. But it’s actually really convenient to use the unhandledRejection handler to report to rollbar and shutdown the process orderly.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

OS: Mac 10.12.6 Node: v8.9.4 Yarn: 1.3.2

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:38
  • Comments:22 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
kschatcommented, Feb 20, 2019

Has anyone found a workaround for this issue?

5reactions
jrnail23commented, Jan 24, 2020

Same problem here… trying to test my error handling (logging, Sentry reporting). It works fine in my other app that uses mocha for its tests, but Jest just won’t let me do it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Unit Test Uncaughtexception And Unhandledrejection ...
The 'unhandledRejection' event is emitted whenever a Promise is rejected and no ... Ask questionsThe unhandledRejection handler is not testable anymore.
Read more >
Window: unhandledrejection event - Web APIs | MDN
The unhandledrejection event is sent to the global scope of a script when a JavaScript Promise that has no rejection handler is rejected; ......
Read more >
Changelog - Cypress Documentation
Fixed an issue with Angular Component Testing where urls within SASS/SCSS files were not being correctly resolved which could result in incomplete styling....
Read more >
How to test a unhandledRejection / uncaughtException ...
After doing this, these methods have no side effects. ... mockImplementation((event, handler) => { if (event === 'unhandledRejection') ...
Read more >
pact-js
you?re not properly handling the promise, it seems ... the `PactWeb` constructor anymore (no need to duplicate what?s configured in the karma conf...
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