Using proxyquire, sinon, and mocha.
I am able to stub fetch on the first call of fetch. But on the second fetch call, I am not able to stub it because it is in the separate file alerts.js
and has it's own require('node-fetch');
How do I stub both of these fetch calls so I can count them?
index.js
sendAlert = require('alerts').sendAlert;
function init() {
fetch(darkWeatherUrl)
.then(()> {
sendAlert();
});
}
alerts.js
function sendAlert() {
fetch(request)
.then(...)
}
test:
describe('lifx alert test', ()=> {
var fetchStub1;
beforeEach(() => {
fetchStub1 = sinon.stub();
});
it('fetch should of called twice', ()=> {
var body = {
"hourly": {
data:
[ { time: 1493413200,
icon: 'clear-day',
precipIntensity: 0,
precipProbability: 0,
ozone: 297.17 }]
}
};
var response = { json: () => { return body } };
fetchStub1.returns(Promise.resolve(response));
proxy('../index.js', {
'node-fetch': fetchStub1
},
{'../alerts' : {
'node-fetch': fetchStub1
}
});
fetchStub1.should.have.been.callCount(2);
});
});
1) lifx alert test fetch should of called twice:
expected stub to have been called exactly twice, but it was called once
stub(https://api.darksky.net/forecast/580c/37.267,-12.433) => [Promise] { } at init (/home/one/github/lifx-weather/index.js:23:3)
AssertionError: expected stub to have been called exactly twice, but it was called once
stub(https://api.darksky.net/forecast/5/3.87,-22.43) => [Promise] { } at init (index.js:23:3)
at Context.it (test/foo.js:45:33)
via dman
No comments:
Post a Comment