Skip to content

Commit

Permalink
test: clean test in after hook (#12325)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning00Blade committed May 3, 2024
1 parent 461a8ff commit dc303f0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/puppeteer-core/src/cdp/Browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 12 additions & 7 deletions test/src/mocha-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const defaultBrowserOptions = Object.assign(
headless: headless === 'shell' ? ('shell' as const) : isHeadless,
dumpio: !!process.env['DUMPIO'],
protocol,
},
} satisfies PuppeteerLaunchOptions,
extraLaunchOptions
);

Expand Down Expand Up @@ -171,7 +171,7 @@ export const setupTestBrowserHooks = (): void => {
}
});

after(() => {
after(async () => {
if (typeof gc !== 'undefined') {
gc();
const memory = process.memoryUsage();
Expand All @@ -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 (
Expand All @@ -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;
Expand Down

0 comments on commit dc303f0

Please sign in to comment.