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

310
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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