Unhandled promise rejection: false positive
See original GitHub issueWith karma 6.3.3, I’m getting an Unhandled promise rejection
error, which looks like a false positive to me. The same code worked fine with karma 4.4.1 (see commit before latest).
The problem can be reproduced as follows:
git clone https://github.com/andreashuber69/karma-false-positive-repro.git
cd karma-false-positive-repro
npm ci
npm run test
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Unhandled promise rejection: false positive #1917 - GitHub
The problem is that the JavaScript runtime can't predict which rejections will be handled in the future and which won't. So it applies...
Read more >Getting a UnhandledPromiseRejectionWarning when testing ...
The "Unhandled promise rejection" is also caused by the failed assertion, because if an error is thrown in a catch() handler, and there...
Read more >Error Explanations - Bluebird JS
This happens when a non object value or a promise that resolves with something that is not an object is being passed instead....
Read more >Flaky `karma` - `test errors has no unhandled Promise ... - GitLab
An error occurred while fetching the assigned iteration of the selected issue.
Read more >Demystifying Jest Async Testing Patterns - DEV Community
As it is, with somethingAsync rejecting the promise the test ... is never reached and you get a false positive with no indication...
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
Actually this has nothing to do with updating
karma
. You can reproduce this issue by only updatingkarma-jasmine
, which in turn would updatejasmine-core
to the version, which includes https://github.com/jasmine/jasmine/pull/1778. So you should probably track https://github.com/jasmine/jasmine/issues/1843, which also shows a workaround for the use cases where you need to trigger unhandled rejection. Unless you specifically testing the logic around error handling, it may be better to re-structure your test in a way that avoids unhandled promise rejection in the first place.Having said that I would like to see if we can solve
SourceMap position not found for trace
warnings produced bykarma
somehow. You see them because TypeScript has down-leveled the async/await syntax and some of the frames point to the locations in this down-leveled code, which don’t map to anything in the original TS code, so that’s why the warning. Maybe it would be better to drop such frames from the re-mapped stack trace instead.@andreashuber69 AFAIU the rejection detection logic works synchronously, e.g. if you don’t await the rejected
Promise
synchronously, then it will be considered unhandled. In your test case, you haveawait new Promise<void>((resolve) => setTimeout(resolve, 100));
, so you create a rejected (not awaited)Promise
in one tick, before that line, and you await it in another tick after that line when it’s too late. If you moveconst rejectingPromise = Promise.reject(new Error("Operation failed."));
to after the timeout, then everything should work correctly.