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.

Unhandled promise rejection: false positive

See original GitHub issue

With 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:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
devoto13commented, Jun 6, 2021

Actually this has nothing to do with updating karma. You can reproduce this issue by only updating karma-jasmine, which in turn would update jasmine-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 by karma 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.

1reaction
devoto13commented, Jun 7, 2021

@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 have await 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 move const rejectingPromise = Promise.reject(new Error("Operation failed.")); to after the timeout, then everything should work correctly.

Read more comments on GitHub >

github_iconTop 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 >

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