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:
normalCode.a()expect(window.hello).toBe('there')window.hello = 'there'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')
})