Skip to content

Commit 1b2645a

Browse files
author
Laurent Guitton
committed
test(desktop): hoist test spies with vi.hoisted to avoid TDZ
`vi.mock` is hoisted by Vitest, so plain `const` spy declarations below it hit a Temporal Dead Zone at module load. Wrapping the spies in `vi.hoisted` guarantees they exist before the mock runs. 🪄 Commit via GitWand
1 parent 32051bf commit 1b2645a

1 file changed

Lines changed: 28 additions & 18 deletions

File tree

apps/desktop/src/__tests__/useAIProvider-opencode.test.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,37 @@
1919
import { describe, it, expect, vi, beforeEach } from "vitest";
2020

2121
// ─── Mock the backend CLI wrappers ─────────────────────────────
22-
const claudeCliPrompt = vi.fn(async () => "ok-claude");
23-
const codexCliPrompt = vi.fn(async () => "ok-codex");
24-
const opencodeCliPrompt = vi.fn(async () => "ok-opencode");
25-
const listOpencodeModels = vi.fn(async () => ["anthropic/claude-x", "openai/gpt-y"]);
26-
// useAIProvider runs `detectClaudeCli()` once at module load — keep the
27-
// auto-fallback disabled so it never hijacks the explicit provider.
28-
const detectClaudeCli = vi.fn(async () => ({
29-
found: false,
30-
path: "",
31-
version: "",
32-
logged_in: false,
33-
status: "not_found",
34-
detail: "",
22+
// Declared via `vi.hoisted` so the spies exist before the hoisted
23+
// `vi.mock` factory runs — `useAIProvider` calls `detectClaudeCli()` at
24+
// module load, which would otherwise hit a TDZ on plain `const` spies.
25+
const {
26+
claudeCliPrompt,
27+
codexCliPrompt,
28+
opencodeCliPrompt,
29+
listOpencodeModels,
30+
detectClaudeCli,
31+
} = vi.hoisted(() => ({
32+
claudeCliPrompt: vi.fn(async () => "ok-claude"),
33+
codexCliPrompt: vi.fn(async () => "ok-codex"),
34+
opencodeCliPrompt: vi.fn(async () => "ok-opencode"),
35+
listOpencodeModels: vi.fn(async () => ["anthropic/claude-x", "openai/gpt-y"]),
36+
// Keep the auto-fallback disabled so it never hijacks the explicit provider.
37+
detectClaudeCli: vi.fn(async () => ({
38+
found: false,
39+
path: "",
40+
version: "",
41+
logged_in: false,
42+
status: "not_found",
43+
detail: "",
44+
})),
3545
}));
3646

3747
vi.mock("../utils/backend", () => ({
38-
claudeCliPrompt: (...a: unknown[]) => claudeCliPrompt(...(a as [])),
39-
codexCliPrompt: (...a: unknown[]) => codexCliPrompt(...(a as [])),
40-
opencodeCliPrompt: (...a: unknown[]) => opencodeCliPrompt(...(a as [])),
41-
listOpencodeModels: () => listOpencodeModels(),
42-
detectClaudeCli: () => detectClaudeCli(),
48+
claudeCliPrompt,
49+
codexCliPrompt,
50+
opencodeCliPrompt,
51+
listOpencodeModels,
52+
detectClaudeCli,
4353
}));
4454

4555
import {

0 commit comments

Comments
 (0)