Anil Namde
Anil Namde

Reputation: 6608

How to test promise scenario with sinon and mocha

Wanted to understand how to write test for the following scenario with promises

Note: code below is pseudo code

class Service{
  get(){
    return Promise.resolve('hi');
  }
}

class otherObj{
  trigger(a){
    console.log(a);
  }
}

class Caller{
  getData(){
    new Service()
      .get()
      .then((a)=>{console.log('in resolve') otherObj.trigger(a)}, 
            (r)=>{console.log('in reject') otherObj.trigger(r)}
           )
  }
}

While writing test I realized that even after stubbing Service.get() call to return resolved promise console logs inside then is not getting called. How to test scenario like this?

descibe('test', ()=>{
  it('test resolve', ()=>{
    let stub = stub(Service.prototype, 'get').returns(Promise.resove('hi'))
   new Caller().getData();    
    stub.restore();
  })
  it('test reject', ()=>{
   let stub = stub(Service.prototype, 'get').returns(Promise.reject('error'))
   new Caller().getData();
   stub.restore();
  })
})

Upvotes: 0

Views: 580

Answers (1)

chrysanthos
chrysanthos

Reputation: 1418

I've refactored your code a bit so that it passes the tests.

'use strict';

const chai = require('chai');
const sinon = require('sinon');
const SinonChai = require('sinon-chai');

chai.use(SinonChai);
chai.should();

class Service {
  get() {
    return Promise.resolve('hi');
  }
}

class OtherObj {

  constructor() {

  }

  trigger(a) {
    console.log(a);
  }
}

class Caller {

  constructor(){
    this.otherObj = new OtherObj();
  }

  getData() {
    new Service()
      .get()
      .then((a) => {
        console.log('in resolve');
        this.otherObj.trigger(a);
      }).catch((e) => {
        console.log('in reject');
        this.otherObj.trigger(e);
      });
  }
}


context('test', function() {

  beforeEach(() => {
    if (!this.sandbox) {
      this.sandbox = sinon.sandbox.create();
    } else {
      this.sandbox.restore();
    }
  });

  it('test resolve', () => {
    this.sandbox.stub(Service.prototype, 'get').returns(Promise.resolve('hi'));
    new Caller().getData();

  });

  it('test reject', () => {
    this.sandbox.stub(Service.prototype, 'get').returns(Promise.reject('error'));
    new Caller().getData();

  });

});

There were a few errors in your snippet preventing it from running smoothly. The way you handled the chaining of the Service() promise was wrong. Instead of this

new Service()
      .get()
      .then((a)=>{console.log('in resolve') otherObj.trigger(a)}, 
            (r)=>{console.log('in reject') otherObj.trigger(r)}
           )

you should have gone for that

new Service()
      .get()
      .then((a) => {
        console.log('in resolve');
        this.otherObj.trigger(a);
      }).catch((e) => {
        console.log('in reject');
        this.otherObj.trigger(e.message);
      });

that handles both happy and sad path. In your version, you never caught the exception thrown by the stub in the second test.

Upvotes: 1

Related Questions