I am trying to stub a required function in my file. For example:
fileA.js
function doSomething() {
return 5;
}
modules.export = { doSomething }
fileB.js
const { doSomething } = require('./fileA');
function doAnotherThing() {
let anotherThing = 10;
return anotherThing + doSomething();
}
modules.export = { doAnotherThing }
And in my test file: fileBTest.js
const fileA = require('./fileA');
const fileB = require('./fileB');
describe('Test File B', function(){
it('Example of failing stub', function() {
const fileAStub = sinon.stub(fileA, 'doSomething');
fileAStub.returns(15);
expect(fileB.doAnotherThing()).to.equal(25);
})
})
However, my fileAStub is not working because in fileB.js, it is required as a function and not the entire fileA being required.
If I change the way I require fileA in fileB.js to the following, my stub will work.
updatedFileB.js
const fileA = require('./fileA');
function doAnotherThing() {
let anotherThing = 10;
return anotherThing + fileA.doSomething();
}
Question: How can I stub { doSomething } instead of changing the way I do require in fileB?