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

130
Views
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 answers
Answer question

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