Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 409
woocash19 Avatar asked Sep 06 '25 23:09

woocash19


1 Answers

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';
like image 111
Robin Elvin Avatar answered Sep 11 '25 06:09

Robin Elvin