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

343
Views
How to flush promises since Jest 26+

Jest 26 shipped a new implementation of fake timers based on @sinonjs/fake-timers.

Previously I used const flushPromises = () => new Promise(setImmediate);

and now I'm trying to replace it with

await new Promise(process.nextTick);

as described here: How to make Jest wait for all asynchronous code to finish execution before expecting an assertion but it doesn't work in some cases.

Example code when it doesn't work (jest version: 27.5.1):

class SuperFetch {
    public static async get(): Promise<string> {
        return 'never mind';
    }
}

class ClassForTest {
    public async get(): Promise<string> {
        return SuperFetch.get();
    }
}

Test1 with legacy fake timers (passed):

jest.useFakeTimers('legacy');
describe('Test', () => {

    const flushPromises = (): Promise<void> => new Promise(setImmediate);
    
    test('should flush promise', async () => {
        SuperFetch.get = jest.fn().mockResolvedValue('test');
        const instance = new ClassForTest();
        let result;
        instance.get().then(x => {
            result = x;
        });

        jest.advanceTimersByTime(1000);
        await flushPromises();

        expect(result).toBe('test');
    });
});

Test2 with modern fake timer (failed)

jest.useFakeTimers('modern');
describe('Test', () => {
    test('should flush promise', async () => {
        SuperFetch.get = jest.fn().mockResolvedValue('test');
        const instance = new ClassForTest();
        let result;
        instance.get().then(x => {
            result = x;
        });

        jest.advanceTimersByTime(1000);
        await new Promise(process.nextTick);

        expect(result).toBe('test');
    });
});

Failed with thrown: "Exceeded timeout of 5000 ms for a test. During debugging it freezes on await new Promise(process.nextTick)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

For my test I am using the flushPromises method as you described

const flushPromises = () => new Promise(setImmediate);

but to make this work I had to import setImmediate

import { setImmediate } from 'timers';

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!