-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
unhandledRejection #2640
Comments
This is definitely something we should add, since the current timeout behavior is quite uninformative and generally promise implementations support this hook; but let's make sure the resulting test failure message indicates that these errors would have been swallowed by the promise in production and should be somehow handled or communicated to the caller for handling (at which point it's easy for the test to communicate the failure back to Mocha without relying on unhandled rejection functions) -- a lot of the questions we get about the current behavior come from lack of awareness of the need for |
One clarification to my earlier comment: I was thinking specifically of unhandled promise rejections -- I believe Mocha already catches uncaught non-promise-swallowed exceptions? |
Yes, I think that's true. |
+1 We were completely caught by surprise that mocha catches thrown errors but not unhandled rejections. Our tests passed when actually they should have failed.
This is a synchronous test which happens to result in an unhandled rejection (suppose e.g. the code under test had an unintended floating promise). In this case, testing results in "foo" and "bar" succeeding and exit code 0. You cannot prevent "foo" from succeeding since node will need a couple of ticks to decide that a promise is unhandled. However I would never expect exit code 0. If necessary, manually go through a couple of node cycles at the end of a mocha execution to wait for any unhandled rejections and then exit with an error message and non-zero code Current work-around:
|
This is what I think needs to happen:
Since Mocha doesn't use There are likely some edge cases here I'm not considering, so this is best-case scenario. 😇 |
Not sure if this is a separate issue or not, but I see similar behaviour without promises when deferring an error with setImmediate or process.nextTick. // swallow.js
it("swallows deferred error", function() {
setImmediate(() => { throw new Error("thrown error"); });
}); Running the test I get success with exit code 0.
Whereas running the same code with node v8.9.0 gives prints the stack trace and exits non-zero:
|
@lrowe Your example is expected behaviour - if you have an a-synchonous test then you should use a callback function or a promise-returning function. When you do, it will be ok. |
@rogierschouten even as an async test I see the same behaviour since the error thrown is not part of the promise chain: it("swallows deferred error", function(done) {
setImmediate(() => { throw new Error("thrown error"); });
done();
}); Modifying my example to log when the error is thrown and with logging added to Runner.prototype.run's uncaughtException handler: // swallow.js
it("swallows deferred error", function() {
setImmediate(() => {
console.log("throwing error");
throw new Error("thrown error");
});
}); We see:
I understand why the test would pass in this instance, but it seems wrong for the mocha My example is a minimal reproducer from using TC39 proposal Observables in a Mocha test. Exceptions raised by the supplied observer methods are specified to report errors asynchronously to the host so that cleanup logic may first be run. |
@lrowe that is because it's your responsibility to call the done callback only when the test is finished. You're supposed to call it with the error object from within the setimmediate. |
@lrowe I re-read your post and now I get your point. Sorry for my too quick response |
Here is another example of an UnhandledPromiseRejectionWarning that didn't fail the build. Using a combination of Mocha, Chai, and Sinon, we had an Once I added the return, I got an
should have been:
Not sure how long we had an invalid test, but it was only by looking through the test log that we came upon UnhandledPromiseRejectionWarning. For now, I've added this small library in the test suite to exit the process for UnhandledPromiseRejectionWarning. https://github.com/mcollina/make-promises-safe It would be nice if the test suite continued running, so that other test failures would be shown, while still failing the build. Thanks! |
I have a WIP commit that implements unhandled rejection support: ofrobots@712678c. @boneskull If this is in line with what you had in mind in this comment I can go ahead and finish this off, write tests and open a PR. This would be a semver major change IMO, so perhaps a |
@ofrobots yes, please. |
Based on my node-project-template. To work around mochajs/mocha#1926 and mochajs/mocha#2640. Signed-off-by: Kevin Locke <[email protected]>
In 1 week this shouldn't matter as this is the default behavior in Node 15 🎉 :] |
@benjamingr and web browsers? |
@stevenvachon it doesn't make sense for browsers to exit the process on unhandled rejections in my opinion just like browsers don't exit on uncaught exceptions. I would assume (and hope!) that the browser counterpart will never happen and browsers will not crash on unhandled rejections. That said: browsers don't (and never did) support the |
@benjamingr but Mocha should still fail the same within a web browser; per this thread's original comment. |
In Mocha v8.2.0 we added an |
@juergba To get unhandledRejection in the xxxx.test.ts at the beginning I need to add the following rows in order to grab the Exception raised in any async function.
E.G. In the following example mocha doesn't print anything. [email protected]
It seems Mocha doesn't handle this type of event and silently hides them. Is the expected behavior? |
`mocha` has been updated to v9. The breaking changes made for v8 and v9 don't affect us [1][2]. Most notably they include dropping support for Node.js v10. The "unhandled rejection" handler setup in the test helper was removed, as this is now default behaviour in `mocha` as of v8.2.0 [3]. [1]: https://github.com/mochajs/mocha/releases/tag/v9.0.0 [2]: https://github.com/mochajs/mocha/releases/tag/v8.0.0 [3]: mochajs/mocha#2640 (comment)
`mocha` has been updated to v9. The breaking changes made for v8 and v9 don't affect us [1][2]. Most notably they include dropping support for Node.js v10. The "unhandled rejection" handler setup in the test helper was removed, as this is now default behaviour in `mocha` as of v8.2.0 [3]. [1]: https://github.com/mochajs/mocha/releases/tag/v9.0.0 [2]: https://github.com/mochajs/mocha/releases/tag/v8.0.0 [3]: mochajs/mocha#2640 (comment)
@eyalroth You would have to be more precise in order to know what is your "faulty behavior". |
@juergba The same faulty/unwanted behavior described in this issue and fixed in 8.2.0 or earlier; i.e, that mocha silently ignores unhandled promise rejections that occur in tests instead of failing the tests. |
Was debugging this issue. All Mocha does on unhandled rejections is just process.emit('unhandledRejection', reason, promise); This is useless and the issue must be reopen. I could try to mitigate this by throwing myself in |
I seem to have this same issue in 10.2.0 |
Use this env |
These should be handled automatically, as to reduce boilerplate. A common issue as a result of this is #1128.
For web browsers, we could use
window.addEventListener
.The text was updated successfully, but these errors were encountered: