Skip to content

Instantly share code, notes, and snippets.

@thanpolas
Created April 11, 2013 22:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thanpolas/5367556 to your computer and use it in GitHub Desktop.
Save thanpolas/5367556 to your computer and use it in GitHub Desktop.
Testing promises with mocha
var assert = require('chai').assert;
var when = require('when');
var def1 = when.defer();
function getProm() {
return def1.promise;
}
suite('Test a promise', function() {
test('an async test with a promise', function(done){
function onResolve(val) {
assert.equal('bar', val, 'Value should be "bar"');
done();
}
// the trick is the final .otherwise that will catch
// the error thrown in the 'onResolve' function
getProm().then(onResolve, done).otherwise(done);
});
});
def1.resolve('foo');
@thanpolas
Copy link
Author

this will allow for properly testing with promises.

You simply need to add the .otherwise(done) tail on each promise that assertions are happening. Typically that's chained second.

/cc @briancavalier @domenic

@thanpolas
Copy link
Author

This is what mocha outputs:

mocha output

@domenic
Copy link

domenic commented Apr 11, 2013

Seems less good than with Mocha as Promised:

test('an async test with a promise', function(){
  return getProm().then(function (val) {
    assert.equal('bar', val, 'Value should be "bar"');
  });
});

@thanpolas
Copy link
Author

@domenic yes, mocha-as-promised is better for one scope level.

I now understand that i have been setting up my tests in a wrong way. My test cases were to check on a promise that was produced from another promise. This second level of nested callbacks forced me to add chai-as-promised in the mix and the verbosity went up the roof.

This test case will pass when only mocha-as-promised is used (it shouldn't):

var assert = require('chai').assert;
require("mocha-as-promised")();
var when = require('when');

var def1 = when.defer();
var def2 = when.defer();

function getProm() {
  return def1.promise;
}

suite('Test a promise', function() {

  test('an async test with a promise', function(done){
    function onResolve(obj) {
      obj.prom.then(function(val){
        assert.equal('bar', val, 'Value should be "bar"');
        done();
      });
    }
    return getProm().then(onResolve);
  });
});

def1.resolve({prom:def2.promise});

def2.resolve('foo');

@thanpolas
Copy link
Author

So that's where .otherwise(done) was an epiphany for me:

  test('an async test with a promise', function(done){
    function onResolve(obj) {
      obj.prom.then(function(val){
        assert.equal('bar', val, 'Value should be "bar"');
        done();
      }).otherwise(done);
    }

    getProm().then(onResolve, done);
  });

From now on i'll make sure my tests only have 1 level of callbacks

@domenic
Copy link

domenic commented Apr 11, 2013

Yeah, if you return a promise that is fulfilled (e.g. by implicitly returning undefined, as you do above), Mocha as Promised will make your test succeed. You generally don't want to mix callbacks (via done) with promises. you want to return promises from inside promises, e.g.

test('an async test with a promise', function(){
  function onResolve(obj) {
    return obj.prom.then(function(val){
      assert.equal('bar', val, 'Value should be "bar"');
    });
  }
  return getProm().then(onResolve);
});

@briancavalier
Copy link

I didn't know that mocha/chai-as-promised supported returning promises from tests, or at least that's what I gather from your latest example @domenic. We love that feature of BusterJS, so it's nice to see other unit test platforms support it too.

@thanpolas: Glad you have a working solution!

@briancavalier
Copy link

In case you don't want to return a promise or find a reason that you can't, then, if you preserve the promise chain by returning promises from inside handlers, you'll likely only need .then(done, done) at the end of the chain, since any thrown exception will be translated into a rejection and will propagate through the chain. Here's a refactor of one of your earlier examples, @thanpolas:

  test('an async test with a promise', function(done){
    function onResolve(obj) {
      return obj.prom.then(function(val){
        assert.equal('bar', val, 'Value should be "bar"');
      });
    }

    getProm().then(onResolve).then(done, done);
  });

The key is the added return inside onResolve, and adding the then(done, done) to the end of the promise chain. I fully admit that I haven't tried this in mocha, but we do it all the time in BusterJS, and just knowing how promise error propagation works, I can't see why this wouldn't be equivalent.

EDIT: Oops: probably need .then(done, done) in this case since done() needs to be called regardless of promise outcome. This isn't as nice as being able to return promises, though :)

@ORESoftware
Copy link

wouldnt be cool to adopt the given-when-then of BDD into mocha with something like this:

it('test', function(){

return given().when().then()

});

does anybody know what I am talking about?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment