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

133
Vistas
JavaScript wait for dynamic modules in Jest tests

Q: How can I wait for all my dynamic modules to be finished before my assertions in Jest ?

Scenario:

// normal code
export default {
  a() {
     import('b', () => {
        window.hello = 'there'
     })
  }
}
// test
import normalCode from 'normalCode'
it('should add to window', () => {
  normalCode.a()
  expect(window.hello).toBe('there')
})

The execution of the code above is:

  1. runs normalCode.a()
  2. runs expect(window.hello).toBe('there')
  3. import finished and is running window.hello = 'there'
  4. it fails because assertions were wrong at step 2, but they would pass after step 3.
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I believe you'll want to return the import promise from function a.

// normal code
export default {
  a() {
     return import('b', () => {
        window.hello = 'there'
     })
  }
}

Then await it in your test.

// test
import normalCode from 'normalCode'
it('should add to window', async () => {
  await normalCode.a()
  expect(window.hello).toBe('there')
})
about 4 years ago · Juan Pablo Isaza Denunciar
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