test(perf): avoid module reload churn in config guard tests

This commit is contained in:
Peter Steinberger
2026-03-02 15:56:10 +00:00
parent 93b0724025
commit d01e82d54a
2 changed files with 24 additions and 7 deletions

View File

@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
const loadAndMaybeMigrateDoctorConfigMock = vi.hoisted(() => vi.fn());
const readConfigFileSnapshotMock = vi.hoisted(() => vi.fn());
@@ -43,14 +43,15 @@ async function withCapturedStdout(run: () => Promise<void>): Promise<string> {
}
describe("ensureConfigReady", () => {
async function loadEnsureConfigReady() {
vi.resetModules();
return await import("./config-guard.js");
}
let ensureConfigReady: (params: {
runtime: unknown;
commandPath: string[];
suppressDoctorStdout?: boolean;
}) => Promise<void>;
let resetConfigGuardStateForTests: () => void;
async function runEnsureConfigReady(commandPath: string[], suppressDoctorStdout = false) {
const runtime = makeRuntime();
const { ensureConfigReady } = await loadEnsureConfigReady();
await ensureConfigReady({ runtime: runtime as never, commandPath, suppressDoctorStdout });
return runtime;
}
@@ -65,8 +66,16 @@ describe("ensureConfigReady", () => {
});
}
beforeAll(async () => {
({
ensureConfigReady,
__test__: { resetConfigGuardStateForTests },
} = await import("./config-guard.js"));
});
beforeEach(() => {
vi.clearAllMocks();
resetConfigGuardStateForTests();
readConfigFileSnapshotMock.mockResolvedValue(makeSnapshot());
});
@@ -107,7 +116,6 @@ describe("ensureConfigReady", () => {
it("runs doctor migration flow only once per module instance", async () => {
const runtimeA = makeRuntime();
const runtimeB = makeRuntime();
const { ensureConfigReady } = await loadEnsureConfigReady();
await ensureConfigReady({ runtime: runtimeA as never, commandPath: ["message"] });
await ensureConfigReady({ runtime: runtimeB as never, commandPath: ["message"] });

View File

@@ -23,6 +23,11 @@ let didRunDoctorConfigFlow = false;
let configSnapshotPromise: Promise<Awaited<ReturnType<typeof readConfigFileSnapshot>>> | null =
null;
function resetConfigGuardStateForTests() {
didRunDoctorConfigFlow = false;
configSnapshotPromise = null;
}
function formatConfigIssues(issues: Array<{ path: string; message: string }>): string[] {
return issues.map((issue) => `- ${issue.path || "<root>"}: ${issue.message}`);
}
@@ -113,3 +118,7 @@ export async function ensureConfigReady(params: {
params.runtime.exit(1);
}
}
export const __test__ = {
resetConfigGuardStateForTests,
};