Skip to content

Commit

Permalink
chore: [#1515] Attempts to fix flaky unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 committed Aug 29, 2024
1 parent 8459c89 commit 34b5226
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions packages/happy-dom/src/browser/utilities/BrowserFrameNavigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ export default class BrowserFrameNavigator {
// Fixes issue where evaluating the response can throw an error.
// By using requestAnimationFrame() the error will not reject the promise.
// The error will be caught by process error level listener or a try and catch in the requestAnimationFrame().
frame.window.requestAnimationFrame(() => frame.window.eval(code));

// We need to wait for the next tick before resolving navigation listeners and ending the ready state task.
await new Promise((resolve) => frame.window.setTimeout(() => resolve(null)));
await new Promise((resolve) => {
frame.window.requestAnimationFrame(() => {
frame.window.requestAnimationFrame(resolve);
frame.window.eval(code);
});
});

readyStateManager.endTask();
resolveNavigationListeners();
Expand Down Expand Up @@ -235,15 +237,14 @@ export default class BrowserFrameNavigator {
// Fixes issue where evaluating the response can throw an error.
// By using requestAnimationFrame() the error will not reject the promise.
// The error will be caught by process error level listener or a try and catch in the requestAnimationFrame().
frame.window.requestAnimationFrame(() => (frame.content = responseText));

// Finalize the navigation
await new Promise((resolve) =>
frame.window.setTimeout(() => {
finalize();
resolve(null);
})
);
await new Promise((resolve) => {
frame.window.requestAnimationFrame(() => {
frame.window.requestAnimationFrame(resolve);
frame.content = responseText;
});
});

finalize();

return response;
}
Expand Down

0 comments on commit 34b5226

Please sign in to comment.