Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda