Sometimes, in a module, functions call other functions (for factorization, code abstraction, etc...), and we may want to test the caller function without testing the inside function.
However, this code does not work as is:
// src/my-module.js
function externalFunction() {
return internalFunction();
}
function internalFunction() {
return { omg: 'this is real' };
}
module.exports = {
externalFunction,
};
// test/my-module.spec.js
const { assert } = require('chai');
const {
describe,
it,
before,
beforeEach,
} = require('mocha');
const sinon = require('sinon');
let myModule;
describe('externalFunction', () => {
before(() => {
myModule = require('../src/my-module');
});
beforeEach(() => {
// partial stubbing not working
sinon.stub(myModule, 'internalFunction').callsFake(() => ({ omg: 'this is fake' }));
})
it('returns the result of internalFunction', () => { // FAILING
const result = myModule.externalFunction();
assert.deepEqual(result, { omg: 'this is fake' });
});
});
The reason why it does not work is actually a simple problem of references on the function.
As we used
return internalFunction(), the referenced function targets the real one, whilesinon.stub(myModule, 'internalFunction')actually creates a reference in the exported functions.Then to have it work, we need to export
internalFunctionand explicitly usemodule.exports.internalFunction()inexternalFunction