Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch unhandled promise rejections during tests #4032

Closed
wants to merge 1 commit into from

Conversation

Zirak
Copy link
Contributor

@Zirak Zirak commented Sep 26, 2019

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:

  • Handling unhandled rejections outside spec tests (e.g. testhooks)
  • Making it work in a browser
  • Amend error messages which reference unhandled rejections
  • Testing the new signature of the Runner.prototype.uncaught method
  • A sister flag to allow-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.

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.

This is only part of handling unhandled rejections, some things not yet included
are:

- Handling unhandled rejections outside spec tests (e.g. testhooks)
- Making it work in a browser
- Amend error messages which reference unhandled rejections
- Testing the new signature of the `Runner.prototype.uncaught` method

If this commit looks nice enough, a future commit will amend these shortcomings.
@jsf-clabot
Copy link

jsf-clabot commented Sep 26, 2019

CLA assistant check
All committers have signed the CLA.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.08%) to 92.815% when pulling 06f89c2 on Zirak:unhandled-rejection into 5f8df08 on mochajs:master.

@juergba
Copy link
Contributor

juergba commented Jan 29, 2021

@Zirak thanks.
In Mocha v8.2.0 we added an unhandledRejection listener.

@juergba juergba closed this Jan 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants