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

✅ Add a test to guarantee buffers order #157

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
55 changes: 55 additions & 0 deletions src/main.unit.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { nodeHtmlToImage } from "./main";
import { Cluster } from "puppeteer-cluster";

import { Screenshot } from "./models/Screenshot";

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

describe("node-html-to-image | Unit", () => {
let mockExit;
const buffer1 = Buffer.alloc(1);
const buffer2 = Buffer.alloc(1);
const html = "<html><body>{{message}}</body></html>";

beforeEach(() => {
jest.spyOn(Cluster, "launch").mockImplementation(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
jest.fn(() => ({
execute: jest
.fn()
.mockImplementationOnce(async () => {
const screenshot = new Screenshot({ html });
screenshot.setBuffer(buffer1);
await sleep(10);
return screenshot;
})
.mockImplementationOnce(() => {
const screenshot = new Screenshot({ html });
screenshot.setBuffer(buffer2);
return screenshot;
}),
idle: jest.fn(),
close: jest.fn(),
}))
);
mockExit = jest.spyOn(process, "exit").mockImplementation((number) => {
throw new Error("process.exit: " + number);
});
});

afterEach(() => {
mockExit.mockRestore();
});

it("should sort buffer in the right order", async () => {
const result = await nodeHtmlToImage({
html,
content: [{ message: "Hello world!" }, { message: "Bonjour monde!" }],
});

expect(result).toEqual([buffer1, buffer2]);
});
});

jest.mock("puppeteer-cluster");