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.#process = process;
this.#connection = connection;
this.#closeCallback = closeCallback || function (): void {};
this.#closeCallback = closeCallback || (() => {});
this.#targetFilterCallback =
targetFilterCallback ||
((): boolean => {
(() => {
return true;
});
this.#setIsPageTargetCallback(isPageTargetCallback);

View File

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