When a `fail` occurs before a done() call, the error message is wrong
See original GitHub issueGiven the following code:
Promise.all(typePromises).then(results => {
console.log(results)
expect(results).not.to.contain(false);
done();
});
I would expect chai to give me an error saying it expected the array not to contain false, if it does contain false.
However, when this is the case I instead get the following error after 2 seconds:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
Clearly my code contains a done() call, and it is called correctly when my test passes. However, when the .not.to.contain
detects a failure it is not called.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Error handling with promises - The Modern JavaScript Tutorial
In the task an error is thrown form inside the promise and promise is never settled neither with a resolve nor reject. The...
Read more >Error Messages: Examples, Best Practices & Common Mistakes
That's what error messages are for—but so many companies fail to follow best ... It can help prevent form errors before they ever...
Read more >A mostly complete guide to error handling in JavaScript.
Learn how to deal with errors and exceptions in synchronous and asynchronous JavaScript code.
Read more >When is .then(success, fail) considered an antipattern for ...
The .then() call will return a promise that will be rejected in case the callback throws an error. This means, when your success...
Read more >VBA Error Handling - A Complete Guide - Excel Macro Mastery
“Abort, Retry, Fail? ... On Error Goto [Label], Goes to a specific label when an error occurs. ... Calling a Sub or Function...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the issue @DannyBrown. Thanks for helping out there @lucasfcosta
The reason this is not doing what you want is because Chai throws errors when an assertion fails. So your code never reaches
done()
, and the Promise gets turned into an error state - but because you’re doing nothing else with the Promise, it never goes anywhere, and subsequently your test is timing out.You probably want to do the following instead:
Which can be shortened to
Alternatively, if you’re using Mocha, you can actually return the Promise in your
it
call, and Mocha will handle it all for you:I’ll close this, because I’m pretty certain the above examples will fix your problems. If they don’t, or you have any more questions, feel free to comment and we can continue discussing this. Have a great day 😄 👍
@michaelgallaghertw I would avoid using
finally
to calldone
. This would make tests pass, even if theexpect()
throws an error, asfinally
is given no arguments..then(done, done)
correctly passes the error back to mocha.