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

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

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