Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
Jest not finding my function and complains of a TypeError

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The module.exports works fine. The problem was related to another import which wasn't resolved in the validator.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!