I'm using Jest in order to test a js function. In my function I have a flag variable. If flag is true I require another js file (which is into the same folder of function to test) otherwise I get the path of the file to include. For this in my function I have:
var utility;
if(flag) {
utility = require('./utility');
} else {
utility = require(Runtime.giveFunctions()['utility'].path);
}
const variable = utility.getValue(someParameters);
It's enough cover 'else' branch and for this I want to return from path: './utility'. In my test I write:
const Runtime = {
giveFunctions: jest.fn().mockReturnValue({
utility: jest.fn().mockReturnValue({
path: jest.fn().mockReturnValue('./utility')
})
})
};
window.Runtime = Runtime;
When I launch the test I receive the error: utility.getValue is not a function (getValue is the function defined in utility.js). More, if in my function i use console.log in order to print 'Runtime' I read: Runtime.giveFunctions()['utility'].path undefined. I think I didn't mock path property well. How can I solve this issue? Thank's to all.