continue-custom/extensions/cli/vitest.setup.ts
Nate Sesti 18acf6fc26
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 <cursoragent@cursor.com>

* 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 <cursoragent@cursor.com>

* chore: retrigger CI

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-15 13:32:58 -07:00

76 lines
2.2 KiB
TypeScript

import { vi } from "vitest";
import { resetConsoleOverrides } from "./src/init.js";
// Disable telemetry for tests
process.env.CONTINUE_CLI_ENABLE_TELEMETRY = "0";
process.env.CONTINUE_ALLOW_ANONYMOUS_TELEMETRY = "0";
// Mock fetch to prevent actual API calls in tests
const originalFetch = global.fetch;
global.fetch = vi
.fn()
.mockImplementation(async (url: string | URL, ...args) => {
const urlString = url.toString();
// Mock the default config API call
if (urlString.includes("get-assistant/continuedev/default-cli-config")) {
return {
ok: true,
status: 200,
json: async () => ({
configResult: {
config: {
name: "Default Config",
version: "0.0.1",
schema: "v1",
models: [
{
provider: "anthropic",
name: "claude-sonnet-4-5",
apiKey: "test-key",
roles: ["chat"],
},
],
rules: [],
mcpServers: [],
},
errors: [],
},
}),
};
}
return originalFetch(url, ...args);
});
// Mock the 'open' package to prevent browser opening during tests
vi.mock("open", () => ({
default: vi.fn().mockResolvedValue(undefined),
}));
// Mock constructSystemMessage to avoid service container issues in tests
vi.mock("./src/systemMessage.js", () => ({
constructSystemMessage: vi
.fn()
.mockResolvedValue(
"You are an agent in the Continue CLI. Given the user's prompt, you should use the tools available to you to answer the user's question.",
),
loadMarkdownRulesWithMetadata: vi.fn().mockReturnValue([]),
}));
// 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(() => {
// Clear vitest timers
vi.clearAllTimers();
// Reset console overrides to ensure clean state for each test
try {
resetConsoleOverrides();
} catch {
// Ignore errors when resetting console
}
});