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

304
Visualizações
How do I mock a primitive value for a single test?

I have a test that requires the value of isBanana to be false.

The test works (and isBanana is false) when I mock the function in the top level index.test.js. However this breaks other tests (as they require isBanana to be true).

jest.mock("myapp-api-functions", () => {
  console.log(`executing mock function`);
  return {
    ...jest.requireActual("myapp-api-functions"),
    isBanana: false,
  };
});

If I move the jest.mock() into the body of the test, isBanana is true and the test doesn't work.

    it(`should error when someone tries to use the mock account in production`, async () => {
      jest.mock("myapp-api-functions", () => {
        console.log(`executing mock function`);
        return {
          ...jest.requireActual("myapp-api-functions"),
          isBanana: false,
        };
      });

      ...same test function that previously passed...
    });

The mock doesn't work and the test fails.

How can I mock the primitive value for a single test?

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

0

Calls to jest.mock get hoisted to the top of the code block.

To avoid this behaviour you can instead use jest.doMock e.g.

it(`should error when someone tries to use the mock account in 
production`, async () => {
  jest.doMock("myapp-api-functions", () => {
    console.log(`executing mock function`);
    return {
      ...jest.requireActual("myapp-api-functions"),
      isBanana: false,
    };
  });

  // Same test function that previously passed...
});

This will allow you to specify mock behaviour for a specific test.

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