Skip to content

Commit

Permalink
chore: create a test helper for launching browser (#10152)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning00Blade authored May 11, 2023
1 parent e313b05 commit c00cf45
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
16 changes: 16 additions & 0 deletions test/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,20 @@ module.exports = {
},
],
},
overrides: [
{
files: ['*.spec.ts'],
rules: {
'no-restricted-syntax': [
'warn',
{
message:
'Use helper command `launch` to make sure the browsers get cleaned',
selector:
'MemberExpression[object.name="puppeteer"][property.name="launch"]',
},
],
},
},
],
};
54 changes: 54 additions & 0 deletions test/src/mocha-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ export const mochaHooks = {
],

afterEach: (): void => {
if (browserCleanups.length > 0) {
throw new Error('A manually launched browser was not closed!');
}

sinon.restore();
},
};
Expand Down Expand Up @@ -328,3 +332,53 @@ export const createTimeout = <T>(
}, n);
});
};

const browserCleanups: Array<() => Promise<void>> = [];

export const launch = async (
options: PuppeteerLaunchOptions
): Promise<
PuppeteerTestState & {
close: () => Promise<void>;
}
> => {
const close = async () => {
let cleanup = browserCleanups.pop();
while (cleanup) {
await cleanup();
cleanup = browserCleanups.pop();
}
};

try {
const browser = await puppeteer.launch({
...defaultBrowserOptions,
...options,
});
browserCleanups.push(async () => {
browser.close();
});

const context = await browser.createIncognitoBrowserContext();
browserCleanups.push(async () => {
context.close();
});

const page = await context.newPage();
browserCleanups.push(async () => {
page.close();
});

return {
...getTestState(),
browser,
context,
page,
close,
};
} catch (error) {
await close();

throw error;
}
};
12 changes: 3 additions & 9 deletions test/src/network.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {HTTPResponse} from 'puppeteer-core/internal/api/HTTPResponse.js';

import {
getTestState,
launch,
setupTestBrowserHooks,
setupTestPageAndContextHooks,
} from './mocha-utils.js';
Expand Down Expand Up @@ -823,15 +824,9 @@ describe('network', function () {
});

it('Cross-origin set-cookie', async () => {
const {httpsServer, puppeteer, defaultBrowserOptions} = getTestState();

const browser = await puppeteer.launch({
...defaultBrowserOptions,
const {page, httpsServer, close} = await launch({
ignoreHTTPSErrors: true,
});

const page = await browser.newPage();

try {
await page.goto(httpsServer.PREFIX + '/empty.html');

Expand All @@ -855,8 +850,7 @@ describe('network', function () {
]);
expect(response.headers()['set-cookie']).toBe(setCookieString);
} finally {
await page.close();
await browser.close();
await close();
}
});
});
Expand Down

0 comments on commit c00cf45

Please sign in to comment.