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:
- Created 4 years ago
- Reactions:23
- Comments:19 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 (outsidedescribe
) Jest will randomly fail first two tests. Very weirdā¦@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ā