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

213
Visualizações
Unable to mock module in jest

I am unable to mock the module in jest
when I use the below code test case fails

jest.mock("./module", () => ({
 method1: jest.fn().mockImplementation(() => "method1"),
}));

but when I use the arrow function tets case passes.

jest.mock("./module", () => ({
  method1: () => "method1",
}));

Is there any difference between the two approaches or I am doing something wrong.

Below is the implementation.

//utils.js
export const method1 = () => "method 1";

//App.js
import { method1 } from "./utils.js";
export const showMsg = () => method1();




//App.test.js ( Test case fails)
import { showMsg } from "./App";

jest.mock("./utils.js", () => ({
  method1: jest.fn().mockImplementation(() => "method1"),
}));

describe("test mock", () => {
  it("returns the correct value for Method 1", () => {
    expect(showMsg()).toBe("method1");
  });
});


//App.test.js (Test case success)
import { showMsg } from "./App";

jest.mock("./utils.js", () => ({
  method1: () => "method1",
}));

describe("test mock", () => {
  it("returns the correct value for Method 1", () => {
    expect(showMsg()).toBe("method1");
  });
});

Thanks in advance

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I was able to figure out the issue. There was nothing wrong with the implementation. Just a config was missing in the package.json file.

"jest": {
    "resetMocks": false,
    "restoreMocks": false
  }
about 4 years ago · Juan Pablo Isaza Relatório
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