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

350
Views
Cómo eliminar promesas desde Jest 26+

Jest 26 envió una nueva implementación de temporizadores falsos basados en @sinonjs/fake-timers.

Previamente usé const flushPromises = () => new Promise(setImmediate);

y ahora estoy tratando de reemplazarlo con

await new Promise(process.nextTick);

como se describe aquí: Cómo hacer que Jest espere a que todo el código asíncrono termine de ejecutarse antes de esperar una afirmación , pero en algunos casos no funciona.

Código de ejemplo cuando no funciona (versión de broma: 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 con temporizadores falsos heredados (aprobado):

 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 con temporizador falso moderno (fallido)

 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'); }); });

Error al arrojar: "Se excedió el tiempo de espera de 5000 ms para una prueba. Durante la depuración, se congela en await new Promise(process.nextTick)

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

0

Para mi prueba, estoy usando el método flushPromises como lo describiste

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

pero para que esto funcione tuve que importar 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!