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

185
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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