I created a Node.js application which exports a validate function. When I use the validator in a Jest test. Jest complains the validate is not a function. the function is clearly exported and when I click on it in VS Code, it goes right to the function. Here is the validate function exported in a file called validator.js:
// validator.js
module.exports = {
validate: (req) => {
let policyType = req.body.policy_type;
let length = req.body.term_length;
if (!policyType) {
return false;
}
if (!length) {
return false;
}
return true;
}
};
I then pull in the function in the Jest test as follows:
const validate = require("./validator.js");
describe(“Validator Test", ()=> {
test('Ensure expected return ', ()=>{
const req = {};
req.body.policy_type= '';
req.body.term_length= '';
//TypeError: validate is not a function
expect(validate(req)).toEqual(expectedOut);
})
Jest complains that validate is not a function. I simply don't know why it is complaining about not finding the function which can be found in other functions in the node application. I searched online but could not find a use case like mine. As I am quite new to Jest, I would appreciate any help in resolving this. Thank you.
The module.exports works fine. The problem was related to another import which wasn't resolved in the validator.