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 1 commit
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
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -104,7 +104,7 @@
"minimist": "1.2.0",
"mocha": "9.1.3",
"ncp": "2.0.0",
"pixelmatch": "4.0.2",
"pixelmatch": "5.2.1",
jschfflr marked this conversation as resolved.
Show resolved Hide resolved
"pngjs": "6.0.0",
"prettier": "2.3.0",
"sinon": "9.2.4",
Expand Down
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
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
3 changes: 2 additions & 1 deletion test/launcher.spec.ts
Expand Up @@ -611,7 +611,8 @@ describe('Launcher specs', function () {

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
20 changes: 9 additions & 11 deletions test/network.spec.ts
Expand Up @@ -69,27 +69,25 @@ describe('network', function () {
});

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

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

let error;
await page.setRequestInterception(true);
page.on('request', async (request) => {
await request
.continue({
headers: {
'X-Test-Header': 'a\nb',
'X-Invalid-Header': 'a\nb',
},
})
.catch(resolve);
.catch((error_) => {
error = error_;
});
await request.continue();
});
page.goto(server.PREFIX + '/empty.html');
const error = await errorPromise;
expect(error).toBeTruthy();
await page.goto(server.PREFIX + '/empty.html');
expect(error.message).toMatch(/Invalid header/);
});
});
describe('Request.frame', function () {
Expand Down