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

178
Views
How to write tests for a class using jest so that I am able to mock the constructor initialization?

I have a class in my project which has some code like below.

myRepo.js

class MyRepo{
    constructor(){
        this.fnType={
            "Member":this.getMemberDetails,
            "Claim":this.getClaimDetails
        }
    }
    getMemberDetails=async({parameters})=>{
        //some operation
        return someData;
    }
    getClaimDetails=async({parameters})=>{
        //some operation
        return someData;
    }

}

This class is being used in one other service file which dynamically calls one of the functions of myRepo class based on some parameter.

service.js

    const service=(myRepo)=>{
    const queryType=getQueryType(); // Will return either 'Member' or 'Claim'
    let reponse=myrepo.fnType[queryType](someParameters); // how to mock this line?  

}

Using jest, how can write tests for service.js so that I am able to mock the dynamic method selection behaviour and not receive an undefined error on below line.

let reponse=myrepo.fnType[queryType](someParameters); 
about 4 years ago · Juan Pablo Isaza
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!