test: clean test in after hook (#12325)

This commit is contained in:
Nikolay Vitkov 2024-05-03 10:14:41 +02:00 committed by GitHub
parent 461a8ff92f
commit dc303f061b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 9 deletions

View File

@ -100,10 +100,10 @@ export class CdpBrowser extends BrowserBase {
this.#defaultViewport = defaultViewport; this.#defaultViewport = defaultViewport;
this.#process = process; this.#process = process;
this.#connection = connection; this.#connection = connection;
this.#closeCallback = closeCallback || function (): void {}; this.#closeCallback = closeCallback || (() => {});
this.#targetFilterCallback = this.#targetFilterCallback =
targetFilterCallback || targetFilterCallback ||
((): boolean => { (() => {
return true; return true;
}); });
this.#setIsPageTargetCallback(isPageTargetCallback); this.#setIsPageTargetCallback(isPageTargetCallback);

View File

@ -96,7 +96,7 @@ const defaultBrowserOptions = Object.assign(
headless: headless === 'shell' ? ('shell' as const) : isHeadless, headless: headless === 'shell' ? ('shell' as const) : isHeadless,
dumpio: !!process.env['DUMPIO'], dumpio: !!process.env['DUMPIO'],
protocol, protocol,
}, } satisfies PuppeteerLaunchOptions,
extraLaunchOptions extraLaunchOptions
); );
@ -171,7 +171,7 @@ export const setupTestBrowserHooks = (): void => {
} }
}); });
after(() => { after(async () => {
if (typeof gc !== 'undefined') { if (typeof gc !== 'undefined') {
gc(); gc();
const memory = process.memoryUsage(); const memory = process.memoryUsage();
@ -185,6 +185,14 @@ export const setupTestBrowserHooks = (): void => {
} }
} }
}); });
afterEach(async () => {
if (state.context) {
await state.context.close();
state.context = undefined;
state.page = undefined;
}
});
}; };
export const getTestState = async ( export const getTestState = async (
@ -211,15 +219,12 @@ export const getTestState = async (
} else if (!state.browser.connected) { } else if (!state.browser.connected) {
throw new Error('Browser has disconnected!'); throw new Error('Browser has disconnected!');
} }
if (state.context) { if (state.context) {
await state.context.close(); throw new Error('Previous state was not cleared');
state.context = undefined;
state.page = undefined;
} }
if (!skipContextCreation) { if (!skipContextCreation) {
state.context = await state.browser!.createBrowserContext(); state.context = await state.browser.createBrowserContext();
state.page = await state.context.newPage(); state.page = await state.context.newPage();
} }
return state as PuppeteerTestState; return state as PuppeteerTestState;