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

Improving puppeteer usage for testing to avoid spawning many browsers #641

Merged
merged 11 commits into from
Jun 13, 2024
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"test": "turbo test",
"build": "turbo build",
"check": "turbo check",
"lint": "turbo lint --concurrency=4",
"lint": "NODE_OPTIONS='--max-old-space-size=4096' turbo lint --concurrency=2",
"lint:autofix": "turbo lint:autofix",
"format:check": "turbo format:check",
"format:write": "turbo format:write",
Expand Down
2 changes: 1 addition & 1 deletion packages/agreement-process/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"pagopa-interop-commons-test": "workspace:*",
"pg-promise": "11.8.0",
"prettier": "2.8.8",
"puppeteer": "22.10.0",
"puppeteer": "22.11.0",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but also fixing the fact that we were using different versions of puppeteer in different packages

"testcontainers": "10.9.0",
"ts-node": "10.9.2",
"typescript": "5.4.5",
Expand Down
8 changes: 6 additions & 2 deletions packages/agreement-process/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import {
Attribute,
toReadModelAttribute,
} from "pagopa-interop-models";
import { genericLogger, initPDFGenerator } from "pagopa-interop-commons";
import {
genericLogger,
initPDFGenerator,
launchBrowser,
} from "pagopa-interop-commons";
import { SelfcareV2Client } from "pagopa-interop-selfcare-v2-client";
import puppeteer, { Browser } from "puppeteer";
import { agreementServiceBuilder } from "../src/services/agreementService.js";
Expand All @@ -43,7 +47,7 @@ export const { cleanup, readModelRepository, postgresDB, fileManager } =

afterEach(cleanup);

const testBrowserInstance: Browser = await puppeteer.launch();
const testBrowserInstance: Browser = await launchBrowser({ pipe: true });
const closeTestBrowserInstance = async (): Promise<void> =>
await testBrowserInstance.close();

Expand Down
2 changes: 1 addition & 1 deletion packages/commons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"pagopa-interop-models": "workspace:*",
"pg-promise": "11.8.0",
"ts-pattern": "5.1.2",
"puppeteer": "22.10.1",
"puppeteer": "22.11.0",
"uuid": "10.0.0",
"winston": "3.13.0",
"zod": "3.23.8",
Expand Down
23 changes: 15 additions & 8 deletions packages/commons/src/pdf-generator/pdfGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ export interface PDFGenerator {
) => Promise<Buffer>;
}

export async function initPDFGenerator(): Promise<PDFGenerator> {
const templateService = buildHTMLTemplateService();
let browserInstance = await puppeteer.launch({
/* NOTE
those configurations allow link (file://) usages for
resources files in template's folder
export const launchBrowser = (
launchoptions: puppeteer.LaunchOptions = {}
): Promise<Browser> =>
puppeteer.launch({
...launchoptions,

/* NOTE
those configurations allow link (file://) usages for
resources files in template's folder
*/
args: [
"--no-sandbox",
Expand All @@ -29,18 +32,22 @@ export async function initPDFGenerator(): Promise<PDFGenerator> {
],
});

export async function initPDFGenerator(): Promise<PDFGenerator> {
const templateService = buildHTMLTemplateService();
let browserInstance = await launchBrowser();

const getBrowser = async (): Promise<Browser> => {
if (browserInstance?.connected) {
return browserInstance;
} else {
browserInstance = await puppeteer.launch();
browserInstance = await launchBrowser();
return browserInstance;
}
};

// During unexpected browser crash restarts browser handling "disconnected" event
browserInstance.on("disconnected", async () => {
browserInstance = await puppeteer.launch();
browserInstance = await launchBrowser();
});

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/purpose-process/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"cpx2": "7.0.1",
"pagopa-interop-commons-test": "workspace:*",
"prettier": "2.8.8",
"puppeteer": "22.10.1",
"puppeteer": "22.11.0",
"testcontainers": "10.9.0",
"ts-node": "10.9.2",
"typescript": "5.4.5",
Expand Down
3 changes: 2 additions & 1 deletion packages/purpose-process/test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import {
initPDFGenerator,
launchBrowser,
riskAnalysisFormToRiskAnalysisFormToValidate,
} from "pagopa-interop-commons";
import {
Expand Down Expand Up @@ -52,7 +53,7 @@ export const purposes = readModelRepository.purposes;

export const readModelService = readModelServiceBuilder(readModelRepository);

const testBrowserInstance: Browser = await puppeteer.launch({ pipe: true });
const testBrowserInstance: Browser = await launchBrowser({ pipe: true });
const closeTestBrowserInstance = async (): Promise<void> =>
await testBrowserInstance.close();

Expand Down
Loading
Loading