test: catch unhandled in a network test (#10242)

This commit is contained in:
Alex Rudenko 2023-05-30 10:03:06 +02:00 committed by GitHub
parent 1e5865c911
commit 73b72b31b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -371,17 +371,26 @@ describe('navigation', function () {
// Ignore Error that arise from test server during hooks
});
// Navigate to a page which loads immediately and then does a bunch of
// requests via javascript's fetch method.
const navigationPromise = page.goto(server.PREFIX + '/networkidle.html', {
waitUntil: 'networkidle0',
});
// Track when the navigation gets completed.
let navigationFinished = false;
navigationPromise.then(() => {
return (navigationFinished = true);
let navigationError: Error | undefined;
// Navigate to a page which loads immediately and then does a bunch of
// requests via javascript's fetch method.
const navigationPromise = page
.goto(server.PREFIX + '/networkidle.html', {
waitUntil: 'networkidle0',
})
.then(response => {
navigationFinished = true;
return response;
})
.catch(error => {
navigationError = error;
return null;
});
let afterNavigationError: Error | undefined;
const afterNavigationPromise = (async () => {
// Wait for the page's 'load' event.
await waitEvent(page, 'load');
expect(navigationFinished).toBe(false);
@ -411,10 +420,21 @@ describe('navigation', function () {
response.statusCode = 404;
response.end(`File not found`);
}
})().catch(error => {
afterNavigationError = error;
});
const response = (await navigationPromise)!;
await Promise.race([navigationPromise, afterNavigationPromise]);
if (navigationError) {
throw navigationError;
}
await Promise.all([navigationPromise, afterNavigationPromise]);
if (afterNavigationError) {
throw afterNavigationError;
}
// Expect navigation to succeed.
expect(response.ok()).toBe(true);
expect(navigationFinished).toBeTruthy();
expect((await navigationPromise)?.ok()).toBe(true);
});
it('should not leak listeners during navigation', async function () {
this.timeout(25_000);