My function under test looks roughly like this;
function doThing(data, callback) {
externalService.post('send').request(data)
.then(() => {
if (callback) { callback(); }
})
.catch((message) => {
logger.warn('warning message');
if (callback) { callback(); }
});
}
And I am trying to test this using Chai and Sinon.
I've tried following different guides, my current incantation looks like;
const thingBeingTested = require('thing-being-tested');
const chai = require('chai');
const sinon = require('sinon');
require('sinon-as-promised');
const sinonChai = require('sinon-chai');
const expect = chai.expect;
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
chai.use(sinonChai);
describe('The Thing', () => {
it('should run a callback when requested and successful', done => {
const externalService = { post: { request: sinon.stub() } };
const callback = sinon.spy();
externalService.post.request.resolves(callback);
doThing({...}, callback);
expect(callback).to.have.been.called;
done();
});
});
I cannot get externalService.post
stubbed out correctly. Any help would be greatly appreciated.
I am completely new to Chai and Sinon – so fully expect to be doing something stupid.