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

186
Vistas
Testing async methods in jest gets mock resolved values mixed up

I'm writing some jest tests to test an async method. If I run each test individually, it works! However, when I run the whole suite, they fail. The mock values from one test end up in another test because I suspect that jest is not waiting for one to finish before moving onto another.

describe('testing method', () => {
test('test case 1', async () => {
dependentService.method.mockResolvedValueOnce(mockResult1);
expect(await testSubject(input)).toBe(expectedResult);
});
 test('test case 2', async () => {
dependentService.method.mockResolvedValueOnce(mockResult2);
expect(await testSubject(input)).toBe(expectedResult);
});
});

In the above sample code snippet, the mockResult2 is used in the test case 1, instead of mockResult1. How do I get this to behave as I expect?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Jest executes all testcases in parallel but you can execute serially by adding this flag

jest --runInBand
about 4 years ago · Juan Pablo Isaza Denunciar

0

Changing up the expect syntax will solve the problem.

await expect(testSubject(input)).resolves.toBe(expectedResult)

The toBe part of that line is mandatory - without it jest wont wait for the promise to resolve and the framework will mix up promises across tests.

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