Skip to content

Commit e58f2e7

Browse files
committed
test: add bundle size test
1 parent 1eb317a commit e58f2e7

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/bundle.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, it, expect } from "vitest";
2+
import { build } from "esbuild";
3+
import { fileURLToPath } from "node:url";
4+
import zlib from "node:zlib";
5+
6+
describe("benchmark", () => {
7+
it("no side effects", async () => {
8+
const code = /* js */ `
9+
import { createHooks } from "../src/index.ts";
10+
`;
11+
const { bytes, output } = await getBundleSize(code);
12+
expect(output).toEqual("");
13+
expect(bytes).toBe(0);
14+
});
15+
16+
it("new Hookable()", async () => {
17+
const code = /* js */ `
18+
import { Hookable } from "../src/index.ts";
19+
export default new Hookable()
20+
`;
21+
const { bytes, gzipSize } = await getBundleSize(code);
22+
// console.log("new Hookable():", { bytes, gzipSize });
23+
expect(bytes).toBeLessThan(3000);
24+
expect(gzipSize).toBeLessThan(1200);
25+
});
26+
});
27+
28+
async function getBundleSize(code: string) {
29+
const res = await build({
30+
bundle: true,
31+
metafile: true,
32+
write: false,
33+
minify: true,
34+
format: "esm",
35+
platform: "node",
36+
outfile: "index.mjs",
37+
stdin: {
38+
contents: code,
39+
resolveDir: fileURLToPath(new URL(".", import.meta.url)),
40+
sourcefile: "index.mjs",
41+
loader: "js",
42+
},
43+
});
44+
45+
const { bytes } = res.metafile.outputs["index.mjs"];
46+
const gzipSize = zlib.gzipSync(res.outputFiles[0].text).byteLength;
47+
return {
48+
bytes,
49+
gzipSize,
50+
output: new TextDecoder().decode(res.outputFiles[0].contents),
51+
};
52+
}

0 commit comments

Comments
 (0)