question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When a `fail` occurs before a done() call, the error message is wrong

See original GitHub issue

Given 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:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
keithamuscommented, Jan 5, 2016

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:

 Promise.all(typePromises).then(results => {
     console.log(results)
     expect(results).not.to.contain(false);
 }).then(() => done(), error => done(error));

Which can be shortened to

 Promise.all(typePromises).then(results => {
     console.log(results)
     expect(results).not.to.contain(false);
 }).then(done, done);

Alternatively, if you’re using Mocha, you can actually return the Promise in your it call, and Mocha will handle it all for you:

it('retrieves all results', () => {
 return Promise.all(typePromises).then(results => {
     console.log(results)
     expect(results).not.to.contain(false);
 });
});

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 😄 👍

2reactions
keithamuscommented, Dec 2, 2017

@michaelgallaghertw I would avoid using finally to call done. This would make tests pass, even if the expect() throws an error, as finally is given no arguments. .then(done, done) correctly passes the error back to mocha.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found