Skip to content

Instantly share code, notes, and snippets.

@robballou
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robballou/fc85794aa15167fa83c5 to your computer and use it in GitHub Desktop.
Save robballou/fc85794aa15167fa83c5 to your computer and use it in GitHub Desktop.
Promise testing with mocha
node_modules

Install

  1. Clone this gist.
  2. Run npm install in the gist folder
  3. Run npm test

One test will pass, the other will fail but because it times out. The assertion inside of the .then() fails, stopping the code execution before the done() call. 😢

Huzzah!

Need to add .done() to the chain. See the updated test.js!

var Q = require('q');
function example() {
var deferred = Q.defer();
deferred.resolve(12);
return deferred.promise;
}
module.exports = example;
{
"name": "testing-with-promises",
"devDependencies": {
"mocha": "^2.1.0",
"should": "^4.6.5",
"should-promised": "0.0.4"
},
"dependencies": {
"q": "^1.1.2"
},
"main": "index.js",
"scripts": {
"test": "mocha test.js"
}
}
var should = require('should'),
shouldPromised = require('should-promised'),
example = require('./index.js');
describe('example()', function() {
it('should return 12', function(done) {
example().then(function(answer) {
answer.should.equal(12);
done();
});
}).done();
it('should return 42', function(done) {
example().then(function(answer) {
answer.should.equal(42);
done();
}).done();
});
});
@talon
Copy link

talon commented Feb 26, 2015

Vanilla ES6 promises

(also one of your .done() is on the test suite and not the promise.)

var should = require('should'),
  shouldPromised = require('should-promised'),
  example = require('./index.js');

describe('example()', function() {
  it('should return 12', function(done) {
    example().then(function(answer) {
      answer.should.equal(12);
      done();
    }).catch(done);
  });

  it('should return 42', function(done) {
    example().then(function(answer) {
      answer.should.equal(42);
      done();
    }).catch(done);
  });
});

@robballou
Copy link
Author

Huh, I thought I had tried that... but nonetheless, 🍻 for @LegitTalon!

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