Search code examples
javascriptnode.jsmocha.jssinonproxyquire

stub doesn't return the resolve data in proxyquire getting undefined


i am not able get the resolved value in the response the promise stub returns undefined

   otpHelper.sendOtp = (number) => {
       console.log('number', number);
  return Promise.resolve({ status: '0', requestId: 'hdgfhsgdfdsgfjsgdfj' });
};

sendOtpStub = sinon.stub(otpHelper, 'sendOtp');
const otpHelperStub = proxyquire('../routes/register', {
  '../utils/otpHelper': {
    sendOtp: sendOtpStub,
    // checkOtp: checkOtpStub,
  },
});

sendOtpStub is called but the value that i get is undefined i need to get the resolved value from the promise.its resolving to undefined am using if am not using the stub its working as expected. if i put sendOtp: otpHelper.sendOtp its working as expected but when i make it as a stub its not returning the resolved value.


Solution

  • A stub does not call the original method, that's how it's supposed to work. What you want is a spy, which wraps the original method and invokes it when called.

    sinon.spy(otpHelper, 'sendOtp')