thanpolas // web development as it happens

The mocha + Promises issue has been going strong for over 2 years now, Domenic has authored a helper library that you can use, Mocha As Promised. But after a few itterations a pattern emerged that works for Mocha and Promises, or any test framework for that matter.

What happens here is that you can perform all you assertions, and if any fail they will throw an error that will be cought by the error handler defined in the .then(done, done) part, second done being the error handler. The thrown error will naturally have an argument, the error, that will be piped to Mocha's done callback thus making the test fail with the proper error message.

If all assertions pass then the success callback will be invoked, the first done and the test will finish asynchronously.

For cases where you just want to test the successful invocation of a promise returning function, and function provides an argument when resolving, it will cause the test to fail as the argument will be passed to the done callback. To tackle that inconvenience stub the callback by parially applying null as the first argument.

test('test case', function(done) {
    .then(done.bind(null, null), done);

You're welcome.

blog comments powered by Disqus