You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 catch.
One clarification to my earlier comment: I was thinking specifically of unhandled promise rejections -- I believe Mocha already catches uncaught non-promise-swallowed exceptions?
+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
Reacted by Vitaliy Potapov, Nathan Youngman, davidnorthetal, Orlando Del Aguila, Tomasz Duda, Ciemarr Software, Dr. Jan-Philip Gehrcke, Abbey Hawk Sparrow and Eyal Roth
An unhandled rejection needs to result in the same (or similar) behavior as when an uncaught exception is thrown
A new flag, --allow-unhandled, should be added (if we piggybacked on --allow-uncaught, it would be more difficult to pin down either one in particular)
Plenty of tests.
Since Mocha doesn't use Promises internally, this shouldn't be a particularly severe change to the codebase. I think we can get away with basically copy-and-pasting the majority of the "uncaught exception" behavior, as well as the test suites.
There are likely some edge cases here I'm not considering, so this is best-case scenario. 😇
Reacted by Nathan Youngman and Dr. Jan-Philip Gehrcke
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.
@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.
I understand why the test would pass in this instance, but it seems wrong for the mocha Runner.prototype.uncaught to silently // Ignore errors if complete or pending so I would expect the error to be reported and Mocha to exit with a non-zero status.
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.
@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 unhandledRejection specification - they support a separate event that is in line with the browser event model (on window and not process, with an Event and so forth).
@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.
@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.
Reacted by Mark, Peter Flynn and Guilherme C. Souza
I could try to mitigate this by throwing myself in process.on('unhandledRejection'), which will lead to Mocha going into uncaught route, but it is a hack.
Reacted by Peter Flynn, Guilherme C. Souza, Anna Henningsen and Cristiano Aguzzi
Activity
ScottFreeCode commentedon May 4, 2017
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
catch.ScottFreeCode commentedon May 4, 2017
One clarification to my earlier comment: I was thinking specifically of unhandled promise rejections -- I believe Mocha already catches uncaught non-promise-swallowed exceptions?
stevenvachon commentedon Jun 5, 2017
Yes, I think that's true.
[-]unhandledRejection and uncaughtException[/-][+]unhandledRejection[/+]rogierschouten commentedon Dec 4, 2017
+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:
boneskull commentedon Dec 16, 2017
This is what I think needs to happen:
--allow-unhandled, should be added (if we piggybacked on--allow-uncaught, it would be more difficult to pin down either one in particular)Since Mocha doesn't use
Promises internally, this shouldn't be a particularly severe change to the codebase. I think we can get away with basically copy-and-pasting the majority of the "uncaught exception" behavior, as well as the test suites.There are likely some edge cases here I'm not considering, so this is best-case scenario. 😇
lrowe commentedon Jan 20, 2018
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.
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:
rogierschouten commentedon Jan 21, 2018
@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.
lrowe commentedon Jan 21, 2018
@rogierschouten even as an async test I see the same behaviour since the error thrown is not part of the promise chain:
Modifying my example to log when the error is thrown and with logging added to Runner.prototype.run's uncaughtException handler:
We see:
I understand why the test would pass in this instance, but it seems wrong for the mocha
Runner.prototype.uncaughtto silently// Ignore errors if complete or pendingso I would expect the error to be reported and Mocha to exit with a non-zero status.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.
rogierschouten commentedon Jan 21, 2018
@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.
rogierschouten commentedon Jan 22, 2018
@lrowe I re-read your post and now I get your point. Sorry for my too quick response
9 remaining items
stevenvachon commentedon Oct 15, 2020
@benjamingr and web browsers?
benjamingr commentedon Oct 15, 2020
@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
unhandledRejectionspecification - they support a separate event that is in line with the browser event model (on window and not process, with anEventand so forth).stevenvachon commentedon Oct 15, 2020
@benjamingr but Mocha should still fail the same within a web browser; per this thread's original comment.
juergba commentedon Jan 29, 2021
In Mocha v8.2.0 we added an
unhandledRejectionlistener.Going to close this issue.
pie-r commentedon Mar 3, 2021
@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. Mocha@8.3.0
It seems Mocha doesn't handle this type of event and silently hides them. Is the expected behavior?
Update `mocha` from v7 to v9
Update `mocha` from v7 to v9 (#100)
eyalroth commentedon Oct 20, 2021
@juergba Please reopen this issue. The faulty behavior was reintroduced in 8.2.1 - #4489
juergba commentedon Oct 20, 2021
@eyalroth You would have to be more precise in order to know what is your "faulty behavior".
I have not implemented PR4489.
eyalroth commentedon Oct 20, 2021
@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.
the-spyke commentedon Nov 11, 2021
Was debugging this issue. All Mocha does on unhandled rejections is just
This is useless and the issue must be reopen.
I could try to mitigate this by throwing myself in
process.on('unhandledRejection'), which will lead to Mocha going into uncaught route, but it is a hack.GCSBOSS commentedon May 18, 2023
I seem to have this same issue in 10.2.0
killagu commentedon Aug 3, 2023
Use this env
NODE_OPTIONS=--unhandled-rejections=strictcan convertunhandledRejectionevent touncaughtException.