Skip to content
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
43 changes: 41 additions & 2 deletions scripts/render-metrics.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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", () => {
Expand Down
4 changes: 2 additions & 2 deletions scripts/render-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export type RenderMetrics = Readonly<Record<string, ScenarioReport>>;

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"),
Expand Down
Loading