Some more digging, and it appears the right way is to add an additional catch block, like so...
it.only("do something (negative test)", function (done) {
var Q = require('q');
function makePromise() {
var deferred = Q.defer();
deferred.reject(Error('fail'));
return deferred.promise;
};
makePromise()
.catch(function(e) {
expect(e.message).to.equal('fail');
})
.then(done, done);
});
I'm interested in alternative ideas, or confirmation that this is fine the way it is.. thanks.
UPDATE:
Ben - I now grok what you were saying, esp. after the terse but helpful comment from Benjamin G.
To summarize:
When you pass in a done
parameter, the test is expected to trigger it's 'done-ness' by calling the done()
function;
When you don't pass in a done
parameter, it normally only works for synchronous calls. However,
if you return a promise, the mocha framework (mocha >1.18) will catch any failures that normally would have been swallowed (per the promises spec). Here is an updated version:
it.only("standalone neg test for mocha+promises", function () {
var Q = require('q');
function makePromise() {
var deferred = Q.defer();
deferred.reject(Error('fail'));
return deferred.promise;
};
return makePromise()
.catch(function(e) {
expect(e.message).to.equal('fail');
});
});