Jwqq
Jwqq

Reputation: 1097

How to unit test promise with sinon in my case?

I am trying to start a stub in unit test for my app.

I have something like this in my file.

var sinon = require('sinon'),
    should = require('should');

require('sinon-stub-promise');

describe('test unit Tests', function(){
    describe('My first test', function(){
        it('should test a promise', function(){
            sinon.stub().resolves('foo')().then(function (value) {
            console.log('test');

            assert.equal(value, 'not foooo')
        })
    })
});

My problem is I can't seem to trigger the assert.equal error. I can see the 'test' in the output when run the test. However, the test should fail because the value should be foo and not 'not foooo'. For some reason it passed. I am not sure the reason. Can someone help me about it? Thanks a lot!

Upvotes: 0

Views: 428

Answers (1)

Johannes Merz
Johannes Merz

Reputation: 3340

you need to return the promise so mocha waits:

describe('test unit Tests', function(){
    describe('My first test', function(){
        it('should test a promise', function(){
            return sinon.stub().resolves('foo')().then(function (value) {
              console.log('test');

              assert.equal(value, 'not foooo')
            })
        })
    })
});

Upvotes: 2

Related Questions