Skip to content

Commit 5583cbd

Browse files
committed
fix(test): prevent setTimeout race condition in appload event handler
The previous implementation had a setTimeout with 0ms that would always resolve immediately, making the 'appload' event listener effectively useless. This could cause tests to continue before the app was actually ready. This fix: - Adds a resolved flag to prevent double resolution - Changes timeout from 0ms to 5000ms (reasonable fallback) - Only resolves via timeout if the event hasn't fired - Adds warning message when falling back to timeout - Properly cleans up event listener in both paths
1 parent 9d365e1 commit 5583cbd

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

test/bundler/jest-dom-utils.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,27 @@ export function setupDomTests(document: Document): DomTestUtilities {
4545

4646
// wait for app readiness similar to Karma helper
4747
await new Promise<void>((resolve) => {
48+
let resolved = false;
49+
4850
const onAppLoad = () => {
49-
window.removeEventListener('appload', onAppLoad);
50-
resolve();
51+
if (!resolved) {
52+
resolved = true;
53+
window.removeEventListener('appload', onAppLoad);
54+
resolve();
55+
}
5156
};
57+
5258
window.addEventListener('appload', onAppLoad);
53-
// if app already loaded synchronously, resolve on next tick
54-
setTimeout(() => resolve(), 0);
59+
60+
// Fallback timeout in case the 'appload' event never fires
61+
setTimeout(() => {
62+
if (!resolved) {
63+
console.warn('appload event did not fire within 5 seconds, continuing anyway');
64+
resolved = true;
65+
window.removeEventListener('appload', onAppLoad);
66+
resolve();
67+
}
68+
}, 5000);
5569
});
5670

5771
await allReady();

0 commit comments

Comments
 (0)