I want to unit-test the following simplified module:
const Logger = require('logplease');
const logger = Logger.create('utils');
const tester = {
one: () => {
logger.log('called real one()');
tester.two();
},
two: () => {
logger.log('called real two()');
},
};
module.exports = {
one: tester.one,
two: tester.two
};
I'm replacing the external dependency logplease
using Proxyquire, which works very well. However I need to stub two()
because I want to unit-test one()
while eliminating the side-effects two()
produces when it runs in real code.
it.only('stubbing functions on the "proxyquired" object under test', function(done) {
const loggerStub = {
create: () => {
return { log: (msg) => { console.log('fake logger: ', msg); } };
}
};
let tester = proxyquire('../tester', { 'logplease': loggerStub });
let stub2 = sinon.stub(
tester,
'two',
() => {
console.log('called fake stub of two()');
}
);
tester.one();
console.log('call count 2: ', stub2.callCount);
done();
});
Output I get:
fake logger: called real one()
fake logger: called real two()
call count 2: 0
Output I expect:
fake logger: called real one()
called fake stub of two()
call count 2: 1
Why doesn't my stub function run?