Skip to content
Source
Sign inSign up
Open History to see the last commit

render-metrics.test.ts

66 lines2.3 KB

1// @effect-diagnostics nodeBuiltinImport:off - runOnce is a synchronous script, tested on a real temp dir.
2import * as NodeFS from "node:fs";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5​
6import { describe, expect, it, vi } from "vite-plus/test";
7​
8import { nondeterministic, renderComparison, runOnce } from "./render-metrics.ts";
9​
10const mountReport = { commits: 2, renders: 10, components: { Row: 10 } };
11​
12vi.mock("node:child_process", () => ({
13 spawnSync: (
14 _command: string,
15 _args: string[],
16 options: { env: { T3CODE_RENDER_REPORT_DIR: string } },
17 ) => {
18 NodeFS.writeFileSync(
19 NodePath.join(options.env.T3CODE_RENDER_REPORT_DIR, "mount.json"),
20 JSON.stringify(mountReport),
21 );
22 return { status: 0 };
23 },
24}));
25​
26describe("runOnce", () => {
27 it("collects the scenario reports when HOME has no .cache, then removes its report dir", () => {
28 const home = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-render-metrics-"));
29 vi.stubEnv("HOME", home);
30 vi.stubEnv("TMPDIR", home);
31 vi.stubEnv("USERPROFILE", home);
32 vi.stubEnv("TEMP", home);
33 vi.stubEnv("TMP", home);
34 try {
35 expect(runOnce()).toEqual({ mount: mountReport });
36 expect(NodeFS.readdirSync(home)).toEqual([]);
37 } finally {
38 vi.unstubAllEnvs();
39 NodeFS.rmSync(home, { recursive: true, force: true });
40 }
41 });
42});
43​
44describe("nondeterministic", () => {
45 it("names only the scenarios whose counts differ between runs", () => {
46 const stable = { commits: 2, renders: 10, components: { Row: 10 } };
47 expect(
48 nondeterministic([
49 { mount: stable, type: { commits: 1, renders: 4, components: { Editor: 4 } } },
50 { mount: stable, type: { commits: 1, renders: 5, components: { Editor: 5 } } },
51 ]),
52 ).toEqual(["type"]);
53 });
54});
55​
56describe("renderComparison", () => {
57 it("prints per-scenario deltas and the components that moved most", () => {
58 const output = renderComparison(
59 { stream: { commits: 40, renders: 400, components: { Row: 300, Header: 100 } } },
60 { stream: { commits: 30, renders: 250, components: { Row: 150, Header: 100 } } },
61 );
62 expect(output).toContain("| stream | 40 → 30 (-10, -25%) | 400 → 250 (-150, -37%) |");
63 expect(output).toContain("| `Row` | 300 → 150 (-150, -50%) |");
64 expect(output).not.toContain("`Header`");
65 });
66});