How to test promise with Mocha28 Apr 2016
The first approach might be to unit test promises in the same way as we do with callbacks: by calling the
done() function, passed by Mocha, when the promise is completed.
It works fine if the test passes but if it fails, then we don't get any error report from mocha, at best a timeout error.
This is because we don't handle the case when the promise is rejected. This is where it gets tedious and adds more boilerplate: every time we test a promise, we end up having several
done() calls in our test. (which bothers me.)
Built in Promises support
Hopefully, Mocha comes with a nice built in support. Instead of having to handle manually failing tests, we can directly return the promise.
And voilà! Now we don't need to think about where we need to put the
If you want to directly assert your promises, I recommend you to look over Chai promises plugin.