'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)
Solution 1:[1]
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';
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Robin Elvin |