Catch unhandled promise rejections during tests #4032
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of the Change
Listen to the unhandledRejection event when running tests, catching promises who
got rejected without being cared for, and failing the test in which said
promises were left unhandled.
Alternate Designs
AFAIK, this is the cleanest way to gracefully capture unhandled rejections.
Why should this be in core?
This changes how tests are run in mocha, and provides a nice parallel to the
existing
allowUncaught
option.Benefits
Async tests which now trigger, by themselves or down their call tree,
promises left unresolved now silently pass, with a scary stack trace printed in
the console. For existing issues, see #2640 and #2785 (among others).
Possible Drawbacks
This is only part of handling unhandled rejections, some things not yet included
are:
Runner.prototype.uncaught
methodallow-uncaught
If this commit looks interesting enough, a future commit will amend these shortcomings.
Applicable issues
This may be a breaking change. Tests passing with unhandled rejections will fail starting with this change.