Currently, I am conducting frontend tests utilizing Jest with the jsdom
environment to simulate a DOM tree and manually trigger actions such as button.click()
.
My goal is to be able to await button.click()
, which in my expectations should wait for all of the button's event listeners to resolve their promises, but unfortunately that's not the case.
Consider this code snippet:
class Test {
constructor() {
const button = document.getElementById('button');
button.addEventListener('click', async () => this.fetch());
}
async fetch(): Promise<void> {
await this.sleep();
}
sleep() {
return new Promise(resolve => setTimeout(resolve, 2000))
}
}
Imagine having an async Jest test where I manually trigger button.click()
anticipating it to pause for 2000ms:
async fun(): Promise<void> {
const button = document.getElementById('button');
await button.click(); //I assume this await would wait for 2000ms, however, it resolves instantly
}