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

113
Vistas
Mock the inner function in Jest

JS File

export const refreshInfo = () => {
   console.log("Users refreshed")
}
export const function2 = () => {
  refreshInfo();
}

Test File

import * as helper from './helper';

test("Test Case 1", ()=> {
jest.spyOn(helper, 'refreshInfo').mockImplementationOnce(() => console.log("1"));
helper.function2();
expect(helper.refreshInfo).toHaveBeenCalledTimes(1);
}

test("Test Case 2", ()=> {
jest.spyOn(helper, 'refreshInfo').mockImplementationOnce(() => console.log("2"));
helper.function2();
expect(helper.refreshInfo).toHaveBeenCalledTimes(1);
}

I am mocking the refreshInfo function from helper file, to check how many times it is getting called, while testing function2. Second test case is failing, it is giving 2. means refreshInfo is getting called 2 times, and the expected count should be 1. I think it is considering the call that takes place in test case 1 as well while checking for testcase 2. Is there a way to keep both the calls instances separate. Can they be called in any way such that both the test cases remain independent of each other and should return 1 as the answer for both?

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