There is a file helperFunction.js, which looks like:
module.exports = (arg1, arg2) => {
\\function body
}
Now, in file.js, this function can be simply called by:
let helperFunction = require('./helperFunction.js');
//some code here
let a=1, b=2;
let val = helperFunction(a,b);
//some code here
In order to test file.js, I want to stub the helperFunction. However, the syntax for sinon.stub looks like:
let functionStub = sinon.stub(file, "functionName");
And here in my case the fileName itself is the function name. How do I create a stub for helperFunction now? Or is there anything else I can do?
You can use a library like proxyquire which can be used to override dependencies during testing.
That would mean you would end up with something like this:
const helper = sinon.stub();
const moduleToTest = proxyquire('./your-file-name’, {
'./helperFunction': helper,
});
Although if you do not want to add a new library, you can always switch to refactoring the helperFunction.js
file and exporting your function as a named export instead of default export. That will give you an object having the method you need to stub and that would fit in nicely with your current approach