From 18acf6fc26b098c1e11b3a099d4a42643d2c0794 Mon Sep 17 00:00:00 2001 From: Nate Sesti <33237525+sestinj@users.noreply.github.com> Date: Mon, 15 Jun 2026 13:32:58 -0700 Subject: [PATCH] test(cli): isolate GlobalContext to fix flaky model-persistence tests (#12639) * test(cli): isolate GlobalContext to fix flaky model-persistence tests The model-persistence test files set process.env.CONTINUE_GLOBAL_DIR in beforeEach to isolate their GlobalContext store, but core/util/paths.ts resolves CONTINUE_GLOBAL_DIR into a module-level constant at import time, so those per-test reassignments had no effect. As a result every test file (across parallel Vitest worker processes) read/wrote the same globalContext.json on disk, racing each other and intermittently failing "should clear model selection when set to null". Add a dedicated first setup file (vitest.global-dir-setup.ts) that creates a unique temp dir via fs.mkdtemp and sets CONTINUE_GLOBAL_DIR before any module imports paths.ts, giving each worker process its own isolated GlobalContext. Wire it in as the first entry in setupFiles and stop hardcoding the shared /tmp/continue-test dir in vitest.setup.ts. Co-authored-by: Cursor * style(cli): remove blank line in array to fix prettier-check Pre-existing prettier-check failure on main: a blank line inside the `screens` array literal in NavigationContext.test.tsx is removed by the Prettier version CI runs. Drop the blank line so the file is formatted. Co-authored-by: Cursor * chore: retrigger CI Co-authored-by: Cursor --------- Co-authored-by: Cursor --- .../__tests__/NavigationContext.test.tsx | 7 +---- extensions/cli/tsconfig.json | 2 +- extensions/cli/vitest.config.ts | 2 +- extensions/cli/vitest.global-dir-setup.ts | 26 +++++++++++++++++++ extensions/cli/vitest.setup.ts | 5 ++-- 5 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 extensions/cli/vitest.global-dir-setup.ts diff --git a/extensions/cli/src/ui/context/__tests__/NavigationContext.test.tsx b/extensions/cli/src/ui/context/__tests__/NavigationContext.test.tsx index d3fee7854..d09fbc15f 100644 --- a/extensions/cli/src/ui/context/__tests__/NavigationContext.test.tsx +++ b/extensions/cli/src/ui/context/__tests__/NavigationContext.test.tsx @@ -74,12 +74,7 @@ describe("NavigationContext", () => { it("can navigate to all valid screens", () => { const { result } = renderHook(() => useNavigation(), { wrapper }); - const screens: NavigationScreen[] = [ - "chat", - "config", - "model", - "mcp", - ]; + const screens: NavigationScreen[] = ["chat", "config", "model", "mcp"]; screens.forEach((screen) => { act(() => { diff --git a/extensions/cli/tsconfig.json b/extensions/cli/tsconfig.json index 334aa7137..68f006718 100644 --- a/extensions/cli/tsconfig.json +++ b/extensions/cli/tsconfig.json @@ -26,5 +26,5 @@ "core": ["../../core/dist/index.js"] } }, - "include": ["src/**/*", "vitest.setup.ts"] + "include": ["src/**/*", "vitest.setup.ts", "vitest.global-dir-setup.ts"] } diff --git a/extensions/cli/vitest.config.ts b/extensions/cli/vitest.config.ts index 7bf2f2b72..bcd0af577 100644 --- a/extensions/cli/vitest.config.ts +++ b/extensions/cli/vitest.config.ts @@ -5,7 +5,7 @@ export default defineConfig({ test: { globals: true, environment: "node", - setupFiles: ["./vitest.setup.ts"], + setupFiles: ["./vitest.global-dir-setup.ts", "./vitest.setup.ts"], exclude: ["**/node_modules/**", "**/dist/**", "**/*.e2e.*", "**/e2e/**"], coverage: { reporter: ["text", "json", "html"], diff --git a/extensions/cli/vitest.global-dir-setup.ts b/extensions/cli/vitest.global-dir-setup.ts new file mode 100644 index 000000000..b7dccec38 --- /dev/null +++ b/extensions/cli/vitest.global-dir-setup.ts @@ -0,0 +1,26 @@ +// IMPORTANT: This file must run BEFORE any module that imports +// `core/util/paths.ts`, because that module resolves `CONTINUE_GLOBAL_DIR` +// into a constant at import time. If multiple test files (which run in +// parallel worker processes) all share the same global dir, they race on the +// same `globalContext.json` file on disk, causing flaky failures in the +// model-persistence tests. +// +// To guarantee isolation, give each worker process its own unique global dir. +// This file is intentionally dependency-free (no imports that transitively +// load `paths.ts`) and is listed first in `setupFiles` so the env var is set +// before any other module captures it. +import * as fs from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; + +const globalDir = fs.mkdtempSync(path.join(os.tmpdir(), "continue-cli-test-")); +process.env.CONTINUE_GLOBAL_DIR = globalDir; + +// Best-effort cleanup of this worker's temp dir when the process exits. +process.on("exit", () => { + try { + fs.rmSync(globalDir, { recursive: true, force: true }); + } catch { + // Ignore cleanup errors on exit. + } +}); diff --git a/extensions/cli/vitest.setup.ts b/extensions/cli/vitest.setup.ts index 7487990f6..efbec2ab8 100644 --- a/extensions/cli/vitest.setup.ts +++ b/extensions/cli/vitest.setup.ts @@ -58,8 +58,9 @@ vi.mock("./src/systemMessage.js", () => ({ loadMarkdownRulesWithMetadata: vi.fn().mockReturnValue([]), })); -// Mock environment for tests -process.env.CONTINUE_GLOBAL_DIR = "/tmp/continue-test"; +// NOTE: CONTINUE_GLOBAL_DIR is set to a unique per-worker temp dir in +// ./vitest.global-dir-setup.ts (which runs first) to isolate the shared +// GlobalContext store across parallel test files. Do not override it here. // Set up global afterEach hook to clear all timers and reset console afterEach(() => {