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.

Jest gives no indication that test fails because of unhandled promise rejection.

See original GitHub issue

šŸ› Bug Report

When Jest fails a test for what it thinks - wrongly - is an unhandled rejection, it does not indicate this at all.

To Reproduce

This code in a test module causes Jest to wrongly think a promise rejection isnā€™t going to be handled.

describe("whatever", () => {
  it("fails for no reason", async () => {
    const p = Promise.reject(new Error("Shouldn't fail the test"));
    await new Promise(r => setTimeout(r, 100));
    await expect(p).rejects.toMatchObject({
      message: "Shouldn't fail the test",
    });
  });
});

Output:

 FAIL  src/test/plugging-in.test.ts
  DeviceManager - plugging and unplugging
    ā—‹ skipped becomes useful after being plugged in
  whatever
    Ɨ fails for no reason (12ms)

  ā— whatever ā€ŗ fails for no reason

    an error message

      90 | describe("whatever", () => {
      91 |   it("fails for no reason", async () => {
    > 92 |     const p = Promise.reject(new Error("an error message"));
         |                              ^
      93 |     await new Promise(r => setTimeout(r, 100));
      94 |     await expect(p).rejects.toMatchObject({
      95 |       message: "an error message",

      at Object.it (src/test/plugging-in.test.ts:92:30)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 skipped, 2 total
Snapshots:   0 total
Time:        4.355s, estimated 6s
Ran all test suites matching /plugging-in/i with tests matching "whatever".

Note that nothing in the output conveys the real reason this test failed.

Expected behavior

Something in the output says that Jest failed the test because it canā€™t guarantee that the promise rejection will be handled. For instance, instead of:


     an error message

something like:

    Unhandled Promise Rejection:
    an error message

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:23
  • Comments:19 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
korobochkacommented, Jul 13, 2020

I would also like to add that the problem here is not only ā€œno indicationā€, but also the fact that the test fails: I was writing a test for utility function and I need to test that this function handles both successful and failing callbacks passed to it. Seems like the ā€œnegativeā€ test fails for me anyway, even with expect(promise).rejects...., try/catch.

P.S. Also if I add const promise = Promise.reject('aaaa') to the top level in the file (outside describe) Jest will randomly fail first two tests. Very weirdā€¦

7reactions
timoxleycommented, Jul 7, 2020

@JoeLangewayClearā€™s workaround will suppress these errors, but itā€™s kind of like disabling the engine fault light in your car and claiming you ā€œfixed itā€

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to ensure Jest fails on "unhandledRejection"?
Unhandled rejections from promises that reject when there is no test running will end the process. This is probably what you want and...
Read more >
Jest Unhandled Promise Rejection - Guide Fari
I was mocking an async function, & was testing for what would happen if that promise got rejected, ie an unsuccessful call.
Read more >
Handling those unhandled promise rejections with JS async ...
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which wasĀ ......
Read more >
Jest with failed expectation inside a Promise causes error but ...
What steps will reproduce the problem? ... What is the expected result? The test should be marked as failing. What happens instead? The...
Read more >
Configuring Jest
__tests__/**", "**/*.js"] will not exclude __tests__ because the ... If the file specified by path is not found, an error is returned.
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