-
-
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
Error thrown between it
clauses disappears
#3113
Comments
Source code, for the lazy ones (like me): "use strict"
process.on('unhandledRejection', () => {
console.log('this happens before AFTER')
throw new Error('tsk, tsk, tsk...')
})
describe('errors thrown in event handler', function () {
it('should let me know something went wrong', function (done) {
new Promise((res, rej) => {
setTimeout(() => {
Promise.reject()
res()
done()
}, 1000)
})
})
after(() => console.log('In AFTER: Should not get here?'))
}) |
I'm not quite sure what the intent of the test case is.
From my perspective mocha is doing what it is supposed to. If you remove the line that creates the unhandled rejected promise, things should work as you probably intended. I would recommend returning the first promise and dropping the |
+1 |
We have #2640 for Mocha failing on unhandled rejections, but it may not make a difference with this issue. The example is pretty much equivalent to: process.on('unhandledRejection', message => {
throw new Error(message)
})
it('should let me know something went wrong', function () {
Promise.reject('foobar')
}) Whereas this correctly fails the test: process.on('unhandledRejection', message => {
throw new Error(message)
})
it('should let me know something went wrong', function (done) {
Promise.reject('foobar')
setTimeout(done)
}) One key element here is that it('should let me know something went wrong', function () {
setTimeout(function () { throw new Error('foobar') })
}) ...whereas this does: it('should let me know something went wrong', function (done) {
setTimeout(function () { throw new Error('foobar') })
setTimeout(done)
}) So it's probably not a promise issue specifically. Typically, if the error occurs long enough after the test ends and Mocha has started running other tests or hooks, the error is misattributed (perhaps unavoidably; determining the origin of async errors is hard) to the currently running one: #967 In this case, I'd guess that it's still associated with the test, but that it's being suppressed once the test has passed; see discussion following #2890 (comment) re. suppressing errors after the test reaches some state. P.S.
|
@Munter I'm sorry if my example was too watered down. Our application has a lot of async communication between a variety of services (servers & webworkers). Specifically I encountered a situation where an error was thrown between Mocha caught the exception and did nothing with it, even with the I think a better example is what @ScottFreeCode (thanks!) presented: process.on('unhandledRejection', message => {
throw new Error(message)
})
it('should let me know something went wrong', function () {
Promise.reject('foobar')
}) I think that whether the rejection happens in the test, or somewhere down the code chain, and Mocha caught it, that tests shouldn't just pass without any notification that something bad happened. Please note that this is applies for exceptions between I hope this makes the issue clearer, and thanks for the fast response! |
I don't think this is actionable. Please comment if disagree |
@boneskull, I think it is actionable. When running a tests, I'd expect not only all tests to pass, but also for no unhandled rejections (or exceptions for that matter) to be occur. I think that getting an error message at the end of the test suite stating "Yo! You have unhandled rejections. We don't know where they came from, but this probably shouldn't have happened". Maybe add a flag to allow suppressing this message. wdyt? |
Prerequisites
common mistake
labelnode node_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend avoiding the use of globally installed Mocha.Description
Steps to Reproduce
Expected behavior:
Should get some indication to the error thrown from within the
unhandledRejection
error handler, preferably an exception and stopping the tests.Actual behavior:
I appears that everything works fine
Reproduces how often:
100%
Versions
Additional Information
In our large test suite, we catch
unhandledRejection
s since they .shouldn't happen. I expected throwing an error would somehow get propagated so that tests will fail, with an informative message/error.The text was updated successfully, but these errors were encountered: