Skip to content

Commit

Permalink
:arrow-up: upgrade puppeteer to 23.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
frinyvonnick committed Sep 3, 2024
1 parent d8800c6 commit 2c05eaf
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 114 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"license": "Apache-2.0",
"dependencies": {
"handlebars": "4.7.8",
"puppeteer": "22.8.2",
"puppeteer": "23.2.2",
"puppeteer-cluster": "0.24.0"
},
"scripts": {
Expand All @@ -34,7 +34,7 @@
"gitmoji-changelog": "2.3.0",
"jest": "^28.1.2",
"jest-circus": "^28.1.2",
"puppeteer-core": "22.8.2",
"puppeteer-core": "23.2.2",
"rimraf": "^3.0.2",
"tesseract.js": "4.1.1",
"typescript": "^4.7.4"
Expand Down
72 changes: 43 additions & 29 deletions src/screenshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Screenshot } from "./models/Screenshot";

describe("beforeScreenshot", () => {
let page;
const buffer = Symbol("Buffer");
const buffer = new ArrayBuffer();

beforeEach(() => {
page = {
Expand Down Expand Up @@ -34,7 +34,7 @@ describe("beforeScreenshot", () => {
}),
});

expect(screenshot.buffer).toEqual(buffer);
expect(screenshot.buffer).toEqual(Buffer.from(buffer));
});

it("should compile a screenshot if there is content", async () => {
Expand All @@ -47,7 +47,7 @@ describe("beforeScreenshot", () => {

expect(page.setContent).toHaveBeenCalledWith(
"<html><body>Hello world!</body></html>",
expect.anything()
expect.anything(),
);
});

Expand Down Expand Up @@ -75,7 +75,7 @@ describe("beforeScreenshot", () => {

expect(page.setContent).toHaveBeenCalledWith(
"<html><body>{{message}}</body></html>",
expect.anything()
expect.anything(),
);
});

Expand Down Expand Up @@ -107,7 +107,7 @@ describe("beforeScreenshot", () => {

describe("handlebarsHelpers", () => {
let page;
const buffer = Symbol("Buffer");
const buffer = new ArrayBuffer();

beforeEach(() => {
page = {
Expand All @@ -123,8 +123,7 @@ describe("handlebarsHelpers", () => {
}
});

const compactHtml = (htmlString) =>
htmlString.replace(/((^|\n)\s+)/gm, "");
const compactHtml = (htmlString) => htmlString.replace(/((^|\n)\s+)/gm, "");

describe("if no logic is given in the template", () => {
const html = "<html><body><h1>Hello world!</h1></body></html>";
Expand Down Expand Up @@ -160,7 +159,8 @@ describe("handlebarsHelpers", () => {
},
},
{
label: "all helpers are functions but content has not the sought variable",
label:
"all helpers are functions but content has not the sought variable",
options: {
content: { myOtherVar: "bar" },
handlebarsHelpers: {
Expand All @@ -174,17 +174,20 @@ describe("handlebarsHelpers", () => {
for (const test of cleanTests) {
it(`if no logic is given in the template, it should not throw error when ${test.label}`, async () => {
await expect(
makeScreenshot(page, { screenshot: new Screenshot(test.options), handlebarsHelpers: test.options.handlebarsHelpers })
makeScreenshot(page, {
screenshot: new Screenshot(test.options),
handlebarsHelpers: test.options.handlebarsHelpers,
}),
).resolves.not.toThrow();
});

it(`if no logic is given in the template, it should render the original template when ${test.label}`, async () => {
const p = jest.fn(() => page);
await makeScreenshot(p(), { screenshot: new Screenshot(test.options), handlebarsHelpers: test.options.handlebarsHelpers });
expect(p().setContent).toHaveBeenCalledWith(
html,
expect.anything()
);
await makeScreenshot(p(), {
screenshot: new Screenshot(test.options),
handlebarsHelpers: test.options.handlebarsHelpers,
});
expect(p().setContent).toHaveBeenCalledWith(html, expect.anything());
});
}

Expand All @@ -198,8 +201,8 @@ describe("handlebarsHelpers", () => {
screenshot: new Screenshot({
content: { myVar: "foo" },
html: html,
})
})
}),
}),
).rejects.toThrow(/Some helper is not a valid function/);
});
});
Expand Down Expand Up @@ -239,7 +242,8 @@ describe("handlebarsHelpers", () => {
error: /Missing helper: "equals"/,
},
{
label: "handlebarsHelpers is an object, but some helper is not a function",
label:
"handlebarsHelpers is an object, but some helper is not a function",
options: {
handlebarsHelpers: {
equals: (a, b) => a === b,
Expand All @@ -254,7 +258,10 @@ describe("handlebarsHelpers", () => {
for (const test of errorTests) {
it(`if logic is given in the template, it should throw error when ${test.label}`, async () => {
await expect(
makeScreenshot(page, { screenshot: new Screenshot(test.options), handlebarsHelpers: test.options.handlebarsHelpers })
makeScreenshot(page, {
screenshot: new Screenshot(test.options),
handlebarsHelpers: test.options.handlebarsHelpers,
}),
).rejects.toThrow(test.error);
});
}
Expand Down Expand Up @@ -292,7 +299,8 @@ describe("handlebarsHelpers", () => {
expectedHtml: emptyHtml,
},
{
label: "all helpers are functions but content has not the sought variable",
label:
"all helpers are functions but content has not the sought variable",
options: {
content: { myOtherVar: "bar" },
handlebarsHelpers: {
Expand Down Expand Up @@ -336,16 +344,22 @@ describe("handlebarsHelpers", () => {
for (const test of validTests) {
it(`if logic is given in the template, it should not throw error when ${test.label}`, async () => {
await expect(
makeScreenshot(page, { screenshot: new Screenshot(test.options), handlebarsHelpers: test.options.handlebarsHelpers })
makeScreenshot(page, {
screenshot: new Screenshot(test.options),
handlebarsHelpers: test.options.handlebarsHelpers,
}),
).resolves.not.toThrow();
});

it(`if logic is given in the template, it should render the expected template when ${test.label}`, async () => {
const p = jest.fn(() => page);
await makeScreenshot(p(), { screenshot: new Screenshot(test.options), handlebarsHelpers: test.options.handlebarsHelpers });
await makeScreenshot(p(), {
screenshot: new Screenshot(test.options),
handlebarsHelpers: test.options.handlebarsHelpers,
});
expect(p().setContent).toHaveBeenCalledWith(
test.expectedHtml,
expect.anything()
expect.anything(),
);
});
}
Expand All @@ -365,7 +379,7 @@ describe("handlebarsHelpers", () => {

expect(page.setContent).toHaveBeenCalledWith(
"<html><body><div>Hello world!</div></body></html>",
expect.anything()
expect.anything(),
);
});

Expand All @@ -388,7 +402,7 @@ describe("handlebarsHelpers", () => {

expect(page.setContent).toHaveBeenCalledWith(
"<html><body><div>Hi!</div><div>Hello!</div></body></html>",
expect.anything()
expect.anything(),
);
});

Expand All @@ -405,7 +419,7 @@ describe("handlebarsHelpers", () => {

expect(page.setContent).toHaveBeenCalledWith(
"<html><body><div>Not hello world!</div></body></html>",
expect.anything()
expect.anything(),
);
});

Expand All @@ -428,7 +442,7 @@ describe("handlebarsHelpers", () => {

expect(page.setContent).toHaveBeenCalledWith(
"<html><body><div>I'm not hidden!</div></body></html>",
expect.anything()
expect.anything(),
);
});

Expand All @@ -447,7 +461,7 @@ describe("handlebarsHelpers", () => {

expect(page.setContent).toHaveBeenCalledWith(
"<html><body><div>HI THERE</div></body></html>",
expect.anything()
expect.anything(),
);
});

Expand All @@ -466,7 +480,7 @@ describe("handlebarsHelpers", () => {

expect(page.setContent).toHaveBeenCalledWith(
"<html><body><div>7</div></body></html>",
expect.anything()
expect.anything(),
);
});

Expand All @@ -485,7 +499,7 @@ describe("handlebarsHelpers", () => {

expect(page.setContent).toHaveBeenCalledWith(
"<html><body><div>This is bar</div></body></html>",
expect.anything()
expect.anything(),
);
});
});
Expand Down
11 changes: 4 additions & 7 deletions src/screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,20 @@ export async function makeScreenshot(
waitUntil = "networkidle0",
timeout,
handlebarsHelpers,
}: MakeScreenshotParams
}: MakeScreenshotParams,
) {
page.setDefaultTimeout(timeout);
const hasHelpers = handlebarsHelpers && typeof handlebarsHelpers === "object";
if (hasHelpers) {
if (
Object.values(handlebarsHelpers).every(
(h) => typeof h === "function"
)
Object.values(handlebarsHelpers).every((h) => typeof h === "function")
) {
handlebars.registerHelper(handlebarsHelpers);
} else {
throw Error("Some helper is not a valid function");
}
}


if (screenshot?.content || hasHelpers) {
const template = compile(screenshot.html);
screenshot.setHTML(template(screenshot.content));
Expand All @@ -43,15 +40,15 @@ export async function makeScreenshot(
await beforeScreenshot(page);
}

const buffer = await element.screenshot({
const result = await element.screenshot({
path: screenshot.output,
type: screenshot.type,
omitBackground: screenshot.transparent,
encoding: screenshot.encoding,
quality: screenshot.quality,
});

screenshot.setBuffer(buffer);
screenshot.setBuffer(Buffer.from(result));

return screenshot;
}
Expand Down
Loading

0 comments on commit 2c05eaf

Please sign in to comment.