I have the following scenario, first the class under test, called utils.js:
var exports = module.exports = {};
[...]
var internalHelper = exports.internalHelper = function internalHelper(params) {
[...]
return A.library.promise();
}
[...]
var methodUnderTest = exports.methodUnderTest = function methodUnderTest(params) {
[...]
internalHelper(theParams)
.then(result => {
[...]
});
[...]
}
[...]
The relevant test snippet looks like this:
var internalHelperStub = sinon.stub(utils, 'internalHelper');
internalHelperStub.resolves({ any: 'response' });
The problem is that the actual method gets called, not the stub.
There are a couple of very similar questions, not satisfied with the answers though:
- Move methods to another class, sounds like a patch to me.
- Re-structuring the exports, does not work for me.
Why cannot this be done???
If there is a reason for it, what is the proper structure of the code for a utility class?
I don't see the reason why I should be splitting my code with no need and I do see the reason why this should work.
EDIT: James answered this question here, I am keeping my answer as it provides explanation and code snippet solution. Leaving to SO moderators to mark as duplicate, merge or whatever is done in this cases ;)