Skip to content

The unhandledRejection handler is not testable anymore. #5620

Open

Description

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions