Wednesday, 24 May 2017

sinon-chai not catching promise rejection

//foo.js

const api = require('someApi');
exports.get = obj => {
  if (!obj.id) {
     return Promise.reject('no id');
  }

  api.get(obj.id)... 
}

//foo.spec.js

 let getStub;

  beforeEach(() => {
    getStub = sinon.stub(api, 'get');
  });

   it('should fail to retrieve location on insufficient data', () => {
    let obj = {};
    foo.get(obj)
      .catch(err => {
        getStub.should.have.not.been.called();
        expect(err).to.not.equal('undefined');
      })

  });

When i execute the test I receive this error:

(node:73773) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Cannot read property 'have' of undefined

There is no other stack trace to the error. As far as i can see I have handling the promise rejection by catching the error in the catch block.

Is this a good way to test and how should I fix this ?



via Bubble Trouble

No comments:

Post a Comment