Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle unhandled promise rejections in tests #7722

Merged
merged 7 commits into from Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/common/Browser.ts
Expand Up @@ -240,6 +240,7 @@ export class Browser extends EventEmitter {
private _defaultContext: BrowserContext;
private _contexts: Map<string, BrowserContext>;
private _screenshotTaskQueue: TaskQueue;
private _ignoredTargets = new Set<string>();
/**
* @internal
* Used in Target.ts directly so cannot be marked private.
Expand Down Expand Up @@ -374,6 +375,7 @@ export class Browser extends EventEmitter {

const shouldAttachToTarget = this._targetFilterCallback(targetInfo);
if (!shouldAttachToTarget) {
this._ignoredTargets.add(targetInfo.targetId);
return;
}

Expand All @@ -398,6 +400,7 @@ export class Browser extends EventEmitter {
}

private async _targetDestroyed(event: { targetId: string }): Promise<void> {
if (this._ignoredTargets.has(event.targetId)) return;
const target = this._targets.get(event.targetId);
target._initializedCallback(false);
this._targets.delete(event.targetId);
Expand All @@ -413,6 +416,7 @@ export class Browser extends EventEmitter {
private _targetInfoChanged(
event: Protocol.Target.TargetInfoChangedEvent
): void {
if (this._ignoredTargets.has(event.targetInfo.targetId)) return;
const target = this._targets.get(event.targetInfo.targetId);
assert(target, 'target should exist before targetInfoChanged');
const previousURL = target.url();
Expand Down
4 changes: 4 additions & 0 deletions src/common/DOMWorld.ts
Expand Up @@ -115,6 +115,10 @@ export class DOMWorld {

async _setContext(context?: ExecutionContext): Promise<void> {
if (context) {
assert(
this._contextResolveCallback,
'Execution Context has already been set.'
);
this._ctxBindings.clear();
this._contextResolveCallback.call(null, context);
this._contextResolveCallback = null;
Expand Down
13 changes: 9 additions & 4 deletions src/common/HTTPRequest.ts
Expand Up @@ -229,8 +229,7 @@ export class HTTPRequest {
*/
async finalizeInterceptions(): Promise<void> {
await this._interceptActions.reduce(
(promiseChain, interceptAction) =>
promiseChain.then(interceptAction).catch(handleError),
(promiseChain, interceptAction) => promiseChain.then(interceptAction),
Promise.resolve()
);
const [resolution] = this.interceptResolution();
Expand Down Expand Up @@ -440,7 +439,10 @@ export class HTTPRequest {
postData: postDataBinaryBase64,
headers: headers ? headersArray(headers) : undefined,
})
.catch(handleError);
.catch((error) => {
this._interceptionHandled = false;
return handleError(error);
});
}

/**
Expand Down Expand Up @@ -532,7 +534,10 @@ export class HTTPRequest {
responseHeaders: headersArray(responseHeaders),
body: responseBody ? responseBody.toString('base64') : undefined,
})
.catch(handleError);
.catch((error) => {
this._interceptionHandled = false;
return handleError(error);
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/frame.spec.ts
Expand Up @@ -88,7 +88,7 @@ describe('Frame specs', function () {
// This test checks if Frame.evaluate allows a readonly array to be an argument.
// See https://github.com/puppeteer/puppeteer/issues/6953.
const readonlyArray: readonly string[] = ['a', 'b', 'c'];
mainFrame.evaluate((arr) => arr, readonlyArray);
await mainFrame.evaluate((arr) => arr, readonlyArray);
});
});

Expand Down
8 changes: 5 additions & 3 deletions test/launcher.spec.ts
Expand Up @@ -589,7 +589,8 @@ describe('Launcher specs', function () {
await page.close();
await browser.close();
});
it('should support targetFilter option', async () => {
// @see https://github.com/puppeteer/puppeteer/issues/4197
itFailsFirefox('should support targetFilter option', async () => {
const { server, puppeteer, defaultBrowserOptions } = getTestState();

const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
Expand All @@ -604,14 +605,15 @@ describe('Launcher specs', function () {
const browser = await puppeteer.connect({
browserWSEndpoint,
targetFilter: (targetInfo: Protocol.Target.TargetInfo) =>
!targetInfo.url.includes('should-be-ignored'),
!targetInfo.url?.includes('should-be-ignored'),
});

const pages = await browser.pages();

await page2.close();
await page1.close();
await browser.close();
await browser.disconnect();
await originalBrowser.close();

expect(pages.map((p: Page) => p.url()).sort()).toEqual([
'about:blank',
Expand Down
4 changes: 4 additions & 0 deletions test/mocha-utils.ts
Expand Up @@ -221,6 +221,10 @@ console.log(
}`
);

process.on('unhandledRejection', (reason) => {
throw reason;
});

export const setupTestBrowserHooks = (): void => {
before(async () => {
const browser = await puppeteer.launch(defaultBrowserOptions);
Expand Down
25 changes: 0 additions & 25 deletions test/network.spec.ts
Expand Up @@ -67,31 +67,6 @@ describe('network', function () {
expect(requests.length).toBe(2);
});
});

describeFailsFirefox('Request.continue', () => {
it('should split a request header at new line characters and add the header multiple times instead', async () => {
const { page, server } = getTestState();

let resolve;
const errorPromise = new Promise((r) => {
resolve = r;
});

await page.setRequestInterception(true);
page.on('request', async (request) => {
await request
.continue({
headers: {
'X-Test-Header': 'a\nb',
},
})
.catch(resolve);
});
page.goto(server.PREFIX + '/empty.html');
const error = await errorPromise;
expect(error).toBeTruthy();
});
});
describe('Request.frame', function () {
it('should work for main frame navigation request', async () => {
const { page, server } = getTestState();
Expand Down
43 changes: 43 additions & 0 deletions test/requestinterception.spec.ts
Expand Up @@ -626,6 +626,26 @@ describe('request interception', function () {
expect(serverRequest.method).toBe('POST');
expect(await serverRequest.postBody).toBe('doggo');
});
it('should fail if the header value is invalid', async () => {
const { page, server } = getTestState();

let error;
await page.setRequestInterception(true);
page.on('request', async (request) => {
await request
.continue({
headers: {
'X-Invalid-Header': 'a\nb',
},
})
.catch((error_) => {
error = error_;
});
await request.continue();
});
await page.goto(server.PREFIX + '/empty.html');
expect(error.message).toMatch(/Invalid header/);
});
});

describeFailsFirefox('Request.respond', function () {
Expand Down Expand Up @@ -732,6 +752,29 @@ describe('request interception', function () {
'Yo, page!'
);
});
it('should fail if the header value is invalid', async () => {
const { page, server } = getTestState();

let error;
await page.setRequestInterception(true);
page.on('request', async (request) => {
await request
.respond({
headers: {
'X-Invalid-Header': 'a\nb',
},
})
.catch((error_) => {
error = error_;
});
await request.respond({
status: 200,
body: 'Hello World',
});
});
await page.goto(server.PREFIX + '/empty.html');
expect(error.message).toMatch(/Invalid header/);
});
});
});

Expand Down