Sinon spy with a Promise not being called

4.1k views Asked by At

The test (below) for this piece of code is failing:

module.exports = function(User, jwt) {
  'use strict';

  return function(req, res) {
    User.create(req.body)
    .then(function(id) {
      var token = jwt.sign({id: id}); 
      res.json({token: token});
    });
  };
};

Here is the test:

'use strict';
var chai = require('chai');
var sinon = require('sinon');
require('sinon-as-promised');

chai.should();
chai.use(require('sinon-chai'));

describe('routes/signup', function() {

  var User;
  var request;
  var response;
  var jwt;
  var signup;

  beforeEach(function() {
    User = {create: sinon.stub()};
    request = {body: 'body'};
    response = {json: sinon.spy()};
    jwt = {sign: sinon.stub().withArgs({id:'id'}).returns('token')};
    signup = require('../../../routes/signup')(User, jwt);
  });

  it('returns token when resolving', function() {
    User.create.resolves('id');
    signup(request, response);
    return response.json.should.have.been.calledWith({token: 'token'});
  });
});

If I interrogate the response.json method it appears to have never been called. response.args is an empty array.

I'm guessing I'm missing something fundamental with testing promises. Help!

1

There are 1 answers

1
Travis Kaufman On

Remember promises are async, so you need to wait until the promise resolves and then call then method to do your assertions. fr0609's answer will give you a false positive because it will return before the then block is ever called.

Try this:

it('returns token when resolving', function(done) {

  User.create.resolves('id');

  signup(request, response)
    .then(function() {
      response.json.should.have.been.calledWith({token: 'token'});
      done();
    })
    .catch(done);
});