Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda