diff --git a/scripts/render-metrics.test.ts b/scripts/render-metrics.test.ts index 919ba77e5c366..9ce66d27a9bde 100644 --- a/scripts/render-metrics.test.ts +++ b/scripts/render-metrics.test.ts @@ -1,6 +1,45 @@ -import { describe, expect, it } from "vite-plus/test"; +// @effect-diagnostics nodeBuiltinImport:off - runOnce is a synchronous script, tested on a real temp dir. +import * as NodeFS from "node:fs"; +import * as NodeOS from "node:os"; +import * as NodePath from "node:path"; -import { nondeterministic, renderComparison } from "./render-metrics.ts"; +import { describe, expect, it, vi } from "vite-plus/test"; + +import { nondeterministic, renderComparison, runOnce } from "./render-metrics.ts"; + +const mountReport = { commits: 2, renders: 10, components: { Row: 10 } }; + +vi.mock("node:child_process", () => ({ + spawnSync: ( + _command: string, + _args: string[], + options: { env: { T3CODE_RENDER_REPORT_DIR: string } }, + ) => { + NodeFS.writeFileSync( + NodePath.join(options.env.T3CODE_RENDER_REPORT_DIR, "mount.json"), + JSON.stringify(mountReport), + ); + return { status: 0 }; + }, +})); + +describe("runOnce", () => { + it("collects the scenario reports when HOME has no .cache, then removes its report dir", () => { + const home = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-render-metrics-")); + vi.stubEnv("HOME", home); + vi.stubEnv("TMPDIR", home); + vi.stubEnv("USERPROFILE", home); + vi.stubEnv("TEMP", home); + vi.stubEnv("TMP", home); + try { + expect(runOnce()).toEqual({ mount: mountReport }); + expect(NodeFS.readdirSync(home)).toEqual([]); + } finally { + vi.unstubAllEnvs(); + NodeFS.rmSync(home, { recursive: true, force: true }); + } + }); +}); describe("nondeterministic", () => { it("names only the scenarios whose counts differ between runs", () => { diff --git a/scripts/render-metrics.ts b/scripts/render-metrics.ts index a49154ff5c788..4c9a81c2d8994 100644 --- a/scripts/render-metrics.ts +++ b/scripts/render-metrics.ts @@ -37,8 +37,8 @@ export type RenderMetrics = Readonly>; const root = NodePath.resolve(import.meta.dirname, ".."); -function runOnce(): RenderMetrics { - const directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.homedir(), ".cache", "t3-renders-")); +export function runOnce(): RenderMetrics { + const directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-renders-")); try { const result = NodeChildProcess.spawnSync("vp", ["test", "run", "--project", "renders"], { cwd: NodePath.join(root, "apps/web"),