Skip to content

Commit ee498e9

Browse files
committed
fixed mocked used in test
1 parent 82b1ad5 commit ee498e9

2 files changed

Lines changed: 58 additions & 60 deletions

File tree

‎tests/commands/resume.spec.ts‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@
66

77
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
88

9-
// Track mock function across hoisted vi.mock calls
10-
let mockPromptFn: any = null;
11-
12-
// Mock enquirer - vi.mock is hoisted, so we must define everything inside the factory
13-
vi.mock('enquirer', () => {
14-
// Create the mock function when the factory runs
15-
mockPromptFn = vi.fn();
16-
return {
17-
default: {
18-
prompt: mockPromptFn
19-
}
20-
};
21-
});
9+
// Use vi.hoisted to define mock function before vi.mock runs (both are hoisted together)
10+
const { mockPromptFn } = vi.hoisted(() => ({
11+
mockPromptFn: vi.fn()
12+
}));
13+
14+
// Mock enquirer - vi.mock is hoisted, so we use vi.hoisted variable
15+
vi.mock('enquirer', () => ({
16+
default: {
17+
prompt: mockPromptFn
18+
}
19+
}));
2220

2321
// Mock chalk
2422
vi.mock('chalk', () => ({

‎tests/onboarding/setupWizard.test.ts‎

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,30 @@ import { describe, it, expect, beforeEach, vi } from 'vitest';
88
import * as fsExtra from 'fs-extra';
99
import type { LoadedConfig } from '../../src/types';
1010

11-
// Create a mutable object to hold the mock function
12-
// This allows vi.mock to reference it before it's assigned
13-
const mocks = {
14-
prompt: {
15-
mock: ((...args: any[]) => Promise.resolve(args)) as any
11+
// Use vi.hoisted to define mocks before vi.mock runs (both are hoisted together)
12+
const { mockPromptFn, fsMocks } = vi.hoisted(() => ({
13+
mockPromptFn: vi.fn(),
14+
fsMocks: {
15+
pathExists: vi.fn(),
16+
readJson: vi.fn(),
17+
readFile: vi.fn(),
18+
writeFile: vi.fn()
1619
}
17-
};
20+
}));
1821

1922
// Mock fs-extra
20-
vi.mock('fs-extra', () => ({
21-
pathExists: vi.fn(),
22-
readJson: vi.fn(),
23-
readFile: vi.fn(),
24-
writeFile: vi.fn()
25-
}));
23+
vi.mock('fs-extra', () => fsMocks);
2624

27-
const mockPathExists = fsExtra.pathExists as ReturnType<typeof vi.fn>;
28-
const mockReadJson = fsExtra.readJson as ReturnType<typeof vi.fn>;
29-
const mockReadFile = fsExtra.readFile as ReturnType<typeof vi.fn>;
30-
const mockWriteFile = fsExtra.writeFile as ReturnType<typeof vi.fn>;
25+
const mockPathExists = fsMocks.pathExists;
26+
const mockReadJson = fsMocks.readJson;
27+
const mockReadFile = fsMocks.readFile;
28+
const mockWriteFile = fsMocks.writeFile;
3129

32-
// Mock enquirer - must define mock inside factory since vi.mock is hoisted
30+
// Mock enquirer - uses vi.hoisted variable
3331
vi.mock('enquirer', () => ({
34-
default: mocks.prompt
32+
default: {
33+
prompt: mockPromptFn
34+
}
3535
}));
3636

3737
// Mock chalk (to avoid terminal color issues in tests)
@@ -61,7 +61,7 @@ describe('SetupWizard', () => {
6161
beforeEach(() => {
6262
vi.clearAllMocks();
6363
// Reset the mock function
64-
mocks.prompt.mock = vi.fn();
64+
mockPromptFn.mockReset();
6565
mockPathExists.mockResolvedValue(false);
6666
mockWriteFile.mockResolvedValue(undefined);
6767
});
@@ -71,7 +71,7 @@ describe('SetupWizard', () => {
7171
const wizard = new SetupWizard(testWorkspace);
7272

7373
// Mock all prompts to simulate user flow
74-
mocks.prompt.mock
74+
mockPromptFn
7575
.mockResolvedValueOnce({ provider: 'openrouter' })
7676
.mockResolvedValueOnce({ apiKey: 'sk-test-key' })
7777
.mockResolvedValueOnce({ model: 'anthropic/claude-3.5-sonnet' })
@@ -100,7 +100,7 @@ describe('SetupWizard', () => {
100100
expect(result.success).toBe(true);
101101
expect(result.skippedSteps).toContain('welcome');
102102
expect(result.skippedSteps).toContain('provider');
103-
expect(mocks.prompt.mock).not.toHaveBeenCalled();
103+
expect(mockPromptFn).not.toHaveBeenCalled();
104104
});
105105

106106
it('should run wizard when config exists but provider not configured', async () => {
@@ -112,7 +112,7 @@ describe('SetupWizard', () => {
112112

113113
const wizard = new SetupWizard(testWorkspace, incompleteConfig);
114114

115-
mocks.prompt.mock
115+
mockPromptFn
116116
.mockResolvedValueOnce({ provider: 'openrouter' })
117117
.mockResolvedValueOnce({ apiKey: 'sk-new-key' })
118118
.mockResolvedValueOnce({ model: 'anthropic/claude-3.5-sonnet' })
@@ -123,15 +123,15 @@ describe('SetupWizard', () => {
123123
const result = await wizard.run({ skipWelcome: true });
124124

125125
expect(result.success).toBe(true);
126-
expect(mocks.prompt.mock).toHaveBeenCalled();
126+
expect(mockPromptFn).toHaveBeenCalled();
127127
});
128128
});
129129

130130
describe('Provider Selection', () => {
131131
it('should set provider in result config', async () => {
132132
const wizard = new SetupWizard(testWorkspace);
133133

134-
mocks.prompt.mock
134+
mockPromptFn
135135
.mockResolvedValueOnce({ provider: 'ollama' })
136136
.mockResolvedValueOnce({ model: 'llama3.2:latest' })
137137
.mockResolvedValueOnce({ telemetryEnabled: true })
@@ -147,7 +147,7 @@ describe('SetupWizard', () => {
147147
it('should not prompt for API key for local providers', async () => {
148148
const wizard = new SetupWizard(testWorkspace);
149149

150-
mocks.prompt.mock
150+
mockPromptFn
151151
.mockResolvedValueOnce({ provider: 'ollama' })
152152
.mockResolvedValueOnce({ model: 'llama3.2:latest' })
153153
.mockResolvedValueOnce({ telemetryEnabled: true })
@@ -158,13 +158,13 @@ describe('SetupWizard', () => {
158158

159159
expect(result.success).toBe(true);
160160
// Should not have prompted for API key
161-
expect(mocks.prompt.mock).toHaveBeenCalledTimes(5); // provider, model, telemetry, prefs, agents
161+
expect(mockPromptFn).toHaveBeenCalledTimes(5); // provider, model, telemetry, prefs, agents
162162
});
163163

164164
it('should prompt for API key for cloud providers', async () => {
165165
const wizard = new SetupWizard(testWorkspace);
166166

167-
mocks.prompt.mock
167+
mockPromptFn
168168
.mockResolvedValueOnce({ provider: 'openrouter' })
169169
.mockResolvedValueOnce({ apiKey: 'sk-test-key' })
170170
.mockResolvedValueOnce({ model: 'anthropic/claude-3.5-sonnet' })
@@ -176,15 +176,15 @@ describe('SetupWizard', () => {
176176

177177
expect(result.success).toBe(true);
178178
// Should have prompted for API key
179-
expect(mocks.prompt.mock).toHaveBeenCalledTimes(6); // provider, apiKey, model, telemetry, prefs, agents
179+
expect(mockPromptFn).toHaveBeenCalledTimes(6); // provider, apiKey, model, telemetry, prefs, agents
180180
});
181181
});
182182

183183
describe('API Key Handling', () => {
184184
it('should save API key for OpenRouter', async () => {
185185
const wizard = new SetupWizard(testWorkspace);
186186

187-
mocks.prompt.mock
187+
mockPromptFn
188188
.mockResolvedValueOnce({ provider: 'openrouter' })
189189
.mockResolvedValueOnce({ apiKey: 'sk-or-test-key' })
190190
.mockResolvedValueOnce({ model: 'anthropic/claude-3.5-sonnet' })
@@ -200,7 +200,7 @@ describe('SetupWizard', () => {
200200
it('should save API key for OpenAI', async () => {
201201
const wizard = new SetupWizard(testWorkspace);
202202

203-
mocks.prompt.mock
203+
mockPromptFn
204204
.mockResolvedValueOnce({ provider: 'openai' })
205205
.mockResolvedValueOnce({ apiKey: 'sk-openai-test-key' })
206206
.mockResolvedValueOnce({ model: 'gpt-4o' })
@@ -225,7 +225,7 @@ describe('SetupWizard', () => {
225225

226226
const wizard = new SetupWizard(testWorkspace, existingConfig);
227227

228-
mocks.prompt.mock
228+
mockPromptFn
229229
.mockResolvedValueOnce({ provider: 'openrouter' })
230230
.mockResolvedValueOnce({ useExisting: true }) // Use existing key
231231
.mockResolvedValueOnce({ model: 'anthropic/claude-3.5-sonnet' })
@@ -243,7 +243,7 @@ describe('SetupWizard', () => {
243243
it('should save selected model', async () => {
244244
const wizard = new SetupWizard(testWorkspace);
245245

246-
mocks.prompt.mock
246+
mockPromptFn
247247
.mockResolvedValueOnce({ provider: 'openrouter' })
248248
.mockResolvedValueOnce({ apiKey: 'sk-test' })
249249
.mockResolvedValueOnce({ model: 'anthropic/claude-sonnet-4-20250514' })
@@ -261,7 +261,7 @@ describe('SetupWizard', () => {
261261
it('should save telemetry enabled preference', async () => {
262262
const wizard = new SetupWizard(testWorkspace);
263263

264-
mocks.prompt.mock
264+
mockPromptFn
265265
.mockResolvedValueOnce({ provider: 'ollama' })
266266
.mockResolvedValueOnce({ model: 'llama3.2:latest' })
267267
.mockResolvedValueOnce({ telemetryEnabled: true })
@@ -276,7 +276,7 @@ describe('SetupWizard', () => {
276276
it('should save telemetry disabled preference', async () => {
277277
const wizard = new SetupWizard(testWorkspace);
278278

279-
mocks.prompt.mock
279+
mockPromptFn
280280
.mockResolvedValueOnce({ provider: 'ollama' })
281281
.mockResolvedValueOnce({ model: 'llama3.2:latest' })
282282
.mockResolvedValueOnce({ telemetryEnabled: false })
@@ -293,7 +293,7 @@ describe('SetupWizard', () => {
293293
it('should skip preferences when user declines', async () => {
294294
const wizard = new SetupWizard(testWorkspace);
295295

296-
mocks.prompt.mock
296+
mockPromptFn
297297
.mockResolvedValueOnce({ provider: 'ollama' })
298298
.mockResolvedValueOnce({ model: 'llama3.2:latest' })
299299
.mockResolvedValueOnce({ telemetryEnabled: true })
@@ -309,7 +309,7 @@ describe('SetupWizard', () => {
309309
it('should save preferences when user configures them', async () => {
310310
const wizard = new SetupWizard(testWorkspace);
311311

312-
mocks.prompt.mock
312+
mockPromptFn
313313
.mockResolvedValueOnce({ provider: 'ollama' })
314314
.mockResolvedValueOnce({ model: 'llama3.2:latest' })
315315
.mockResolvedValueOnce({ telemetryEnabled: true })
@@ -329,7 +329,7 @@ describe('SetupWizard', () => {
329329
it('should skip preferences in quick setup mode', async () => {
330330
const wizard = new SetupWizard(testWorkspace);
331331

332-
mocks.prompt.mock
332+
mockPromptFn
333333
.mockResolvedValueOnce({ provider: 'ollama' })
334334
.mockResolvedValueOnce({ model: 'llama3.2:latest' })
335335
.mockResolvedValueOnce({ telemetryEnabled: true })
@@ -356,7 +356,7 @@ describe('SetupWizard', () => {
356356

357357
const wizard = new SetupWizard(testWorkspace);
358358

359-
mocks.prompt.mock
359+
mockPromptFn
360360
.mockResolvedValueOnce({ provider: 'ollama' })
361361
.mockResolvedValueOnce({ model: 'llama3.2:latest' })
362362
.mockResolvedValueOnce({ telemetryEnabled: true })
@@ -373,7 +373,7 @@ describe('SetupWizard', () => {
373373
it('should skip AGENTS.md when user declines', async () => {
374374
const wizard = new SetupWizard(testWorkspace);
375375

376-
mocks.prompt.mock
376+
mockPromptFn
377377
.mockResolvedValueOnce({ provider: 'ollama' })
378378
.mockResolvedValueOnce({ model: 'llama3.2:latest' })
379379
.mockResolvedValueOnce({ telemetryEnabled: true })
@@ -398,7 +398,7 @@ describe('SetupWizard', () => {
398398

399399
const wizard = new SetupWizard(testWorkspace);
400400

401-
mocks.prompt.mock
401+
mockPromptFn
402402
.mockResolvedValueOnce({ provider: 'ollama' })
403403
.mockResolvedValueOnce({ model: 'llama3.2:latest' })
404404
.mockResolvedValueOnce({ telemetryEnabled: true })
@@ -417,7 +417,7 @@ describe('SetupWizard', () => {
417417
const wizard = new SetupWizard(testWorkspace);
418418

419419
// Simulate user pressing ESC
420-
mocks.prompt.mock.mockRejectedValueOnce({ message: 'cancelled' });
420+
mockPromptFn.mockRejectedValueOnce({ message: 'cancelled' });
421421

422422
const result = await wizard.run({ skipWelcome: true });
423423

@@ -430,7 +430,7 @@ describe('SetupWizard', () => {
430430

431431
const closeError = new Error('readline was closed');
432432
(closeError as any).code = 'ERR_USE_AFTER_CLOSE';
433-
mocks.prompt.mock.mockRejectedValueOnce(closeError);
433+
mockPromptFn.mockRejectedValueOnce(closeError);
434434

435435
const result = await wizard.run({ skipWelcome: true });
436436

@@ -452,7 +452,7 @@ describe('SetupWizard', () => {
452452

453453
const wizard = new SetupWizard(testWorkspace, existingConfig);
454454

455-
mocks.prompt.mock
455+
mockPromptFn
456456
.mockResolvedValueOnce({ provider: 'ollama' })
457457
.mockResolvedValueOnce({ model: 'llama3.2:latest' })
458458
.mockResolvedValueOnce({ telemetryEnabled: true })
@@ -463,15 +463,15 @@ describe('SetupWizard', () => {
463463

464464
expect(result.success).toBe(true);
465465
expect(result.config.provider).toBe('ollama');
466-
expect(mocks.prompt.mock).toHaveBeenCalled();
466+
expect(mockPromptFn).toHaveBeenCalled();
467467
});
468468
});
469469

470470
describe('Provider-Specific Base URLs', () => {
471471
it('should set correct base URL for OpenRouter', async () => {
472472
const wizard = new SetupWizard(testWorkspace);
473473

474-
mocks.prompt.mock
474+
mockPromptFn
475475
.mockResolvedValueOnce({ provider: 'openrouter' })
476476
.mockResolvedValueOnce({ apiKey: 'sk-test' })
477477
.mockResolvedValueOnce({ model: 'test' })
@@ -487,7 +487,7 @@ describe('SetupWizard', () => {
487487
it('should set correct base URL for OpenAI', async () => {
488488
const wizard = new SetupWizard(testWorkspace);
489489

490-
mocks.prompt.mock
490+
mockPromptFn
491491
.mockResolvedValueOnce({ provider: 'openai' })
492492
.mockResolvedValueOnce({ apiKey: 'sk-test' })
493493
.mockResolvedValueOnce({ model: 'gpt-4o' })
@@ -503,7 +503,7 @@ describe('SetupWizard', () => {
503503
it('should set correct base URL for Ollama', async () => {
504504
const wizard = new SetupWizard(testWorkspace);
505505

506-
mocks.prompt.mock
506+
mockPromptFn
507507
.mockResolvedValueOnce({ provider: 'ollama' })
508508
.mockResolvedValueOnce({ model: 'llama3.2:latest' })
509509
.mockResolvedValueOnce({ telemetryEnabled: true })

0 commit comments

Comments
 (0)