Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!