I have this simple middle layer code which will be called by someone and it is calling some other api which is returning Promise
function getSomething() {
return api.fetchSomething()
.then(response => response)
}
To test this I wrote this test which is passing.
it('should make a call to api method ', () => {
const someResponse = {};
const fetchSomethingStub = sandbox.stub(api, 'fetchSomething').returns(Promise.resolve(someResponse));
const someResult = getSomething();
someResult.then((result) => {
expect(fetchSomethingStub ).to.have.callCount(1);
expect(result).to.eventually.equal(someResponse);
});
});
But since my api.fetchSomething()
can return reject too I changed the stub to return Promise.reject
but test is passing but with message (node:14688) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): [object Object]
. Is that mean I have to have catch
block in this layer to.? I am planning to just pass on the value from api. Also I am doing test correctly for promise. Any suggestion with resource to look for Promise test with mocha is appreciated.