test: catch unhandled in a network test (#10242)
This commit is contained in:
parent
1e5865c911
commit
73b72b31b2
@ -371,50 +371,70 @@ describe('navigation', function () {
|
|||||||
// Ignore Error that arise from test server during hooks
|
// 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.
|
// Track when the navigation gets completed.
|
||||||
let navigationFinished = false;
|
let navigationFinished = false;
|
||||||
navigationPromise.then(() => {
|
let navigationError: Error | undefined;
|
||||||
return (navigationFinished = true);
|
// 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);
|
||||||
|
|
||||||
|
// Wait for the initial three resources to be requested.
|
||||||
|
await initialFetchResourcesRequested;
|
||||||
|
|
||||||
|
// Expect navigation still to be not finished.
|
||||||
|
expect(navigationFinished).toBe(false);
|
||||||
|
|
||||||
|
// Respond to initial requests.
|
||||||
|
for (const response of responses) {
|
||||||
|
response.statusCode = 404;
|
||||||
|
response.end(`File not found`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset responses array
|
||||||
|
responses = [];
|
||||||
|
|
||||||
|
// Wait for the second round to be requested.
|
||||||
|
await secondFetchResourceRequested;
|
||||||
|
// Expect navigation still to be not finished.
|
||||||
|
expect(navigationFinished).toBe(false);
|
||||||
|
|
||||||
|
// Respond to requests.
|
||||||
|
for (const response of responses) {
|
||||||
|
response.statusCode = 404;
|
||||||
|
response.end(`File not found`);
|
||||||
|
}
|
||||||
|
})().catch(error => {
|
||||||
|
afterNavigationError = error;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Wait for the page's 'load' event.
|
await Promise.race([navigationPromise, afterNavigationPromise]);
|
||||||
await waitEvent(page, 'load');
|
if (navigationError) {
|
||||||
expect(navigationFinished).toBe(false);
|
throw navigationError;
|
||||||
|
|
||||||
// Wait for the initial three resources to be requested.
|
|
||||||
await initialFetchResourcesRequested;
|
|
||||||
|
|
||||||
// Expect navigation still to be not finished.
|
|
||||||
expect(navigationFinished).toBe(false);
|
|
||||||
|
|
||||||
// Respond to initial requests.
|
|
||||||
for (const response of responses) {
|
|
||||||
response.statusCode = 404;
|
|
||||||
response.end(`File not found`);
|
|
||||||
}
|
}
|
||||||
|
await Promise.all([navigationPromise, afterNavigationPromise]);
|
||||||
// Reset responses array
|
if (afterNavigationError) {
|
||||||
responses = [];
|
throw afterNavigationError;
|
||||||
|
|
||||||
// Wait for the second round to be requested.
|
|
||||||
await secondFetchResourceRequested;
|
|
||||||
// Expect navigation still to be not finished.
|
|
||||||
expect(navigationFinished).toBe(false);
|
|
||||||
|
|
||||||
// Respond to requests.
|
|
||||||
for (const response of responses) {
|
|
||||||
response.statusCode = 404;
|
|
||||||
response.end(`File not found`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = (await navigationPromise)!;
|
|
||||||
// Expect navigation to succeed.
|
// 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 () {
|
it('should not leak listeners during navigation', async function () {
|
||||||
this.timeout(25_000);
|
this.timeout(25_000);
|
||||||
|
Loading…
Reference in New Issue
Block a user