diff --git a/src/cli/program/config-guard.test.ts b/src/cli/program/config-guard.test.ts index 8886ddaafd8..6cc0f21512f 100644 --- a/src/cli/program/config-guard.test.ts +++ b/src/cli/program/config-guard.test.ts @@ -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): Promise { } describe("ensureConfigReady", () => { - async function loadEnsureConfigReady() { - vi.resetModules(); - return await import("./config-guard.js"); - } + let ensureConfigReady: (params: { + runtime: unknown; + commandPath: string[]; + suppressDoctorStdout?: boolean; + }) => Promise; + 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"] }); diff --git a/src/cli/program/config-guard.ts b/src/cli/program/config-guard.ts index 42d56ff35be..06c45a8ea58 100644 --- a/src/cli/program/config-guard.ts +++ b/src/cli/program/config-guard.ts @@ -23,6 +23,11 @@ let didRunDoctorConfigFlow = false; let configSnapshotPromise: Promise>> | null = null; +function resetConfigGuardStateForTests() { + didRunDoctorConfigFlow = false; + configSnapshotPromise = null; +} + function formatConfigIssues(issues: Array<{ path: string; message: string }>): string[] { return issues.map((issue) => `- ${issue.path || ""}: ${issue.message}`); } @@ -113,3 +118,7 @@ export async function ensureConfigReady(params: { params.runtime.exit(1); } } + +export const __test__ = { + resetConfigGuardStateForTests, +};