Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
Why doesn't Jest.useFakeTimers work in beforeAll blocks?

I wanted to mock timeout functions globally so I added a call to jest.useFakeTimers in a beforeAll block. When I ran my tests, jest.advanceTimersByTime didn't execute the scheduled code. I used spies to confirm that the mock was indeed being called. I noticed however, that if I moved the jest.advanceTimersByTime call into the test function, or into a beforeEach block, it worked.

Here's a simple example that reproduces the problem.

describe('test', () => {

    beforeEach(() => {
        // Only beforeEach block works, NOT beforeAll
        jest.useFakeTimers();
    })

    it('setTimeout calls callback', () => {
        const callback = jest.fn();

        setTimeout(() => {
            callback()
        }, 3000);

        expect(callback).not.toHaveBeenCalled();

        jest.advanceTimersByTime(3000);

        expect(callback).toHaveBeenCalledTimes(1);
    })

})

I didn't see anything in the docs that explains this. I'm using Jest 26.6.2.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda