testing failed promises with mocha's built-in promise support

javascript, mocha.js, node.js, promise

Solution

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');
  });

});

Problem

How should I be testing, with mocha and chai, that my promise has failed? I am confused, because I initially thought I should be using 'mocha-as-promised', but that package is now deprecated (I'm using mocha 2.1.0), with the advice to just use the promise testing that's now built into mocha. see: https://github.com/domenic/mocha-as-promised Another post recommends doing away with the 'done' argument to the it() callback - not sure I understand why, since my understanding that passing in the 'done' parameter was the way to signal that a test was being tested asynchronously. see: How do I properly test promises with mocha and chai? Anyway, I've tried to reduce my issue to the below code - please help me modify this so that I can test that my promise indeed fails. ``` 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() .then(done, done); }); ```

Original source

Related problems