Skip to content

Commit f797df7

Browse files
committed
fix(test): Fix Vitest 4 compatibility issues in test mocks
Fixed TypeScript errors and mock implementation issues caused by Vitest 4 upgrade: - Updated mock type definitions to use explicit 'any' types with biome-ignore comments - Changed vi.fn() implementations to use function syntax instead of arrow functions - Fixed Spinner mock to use constructor function pattern - Fixed TokenCounter mock to use proper type casting - Fixed RegExp mock to use function syntax - Fixed processConcurrency mocks to use function syntax This resolves lint errors and reduces test failures from 27 to 16. Remaining failures are related to ReadableStream behavior changes in Vitest 4.
1 parent 1aa3447 commit f797df7

File tree

6 files changed

+43
-27
lines changed

6 files changed

+43
-27
lines changed

tests/cli/actions/defaultAction.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const mockSpinner = {
3131
} as unknown as Spinner;
3232

3333
vi.mock('../../../src/cli/cliSpinner', () => ({
34-
Spinner: vi.fn().mockImplementation(() => mockSpinner),
34+
Spinner: vi.fn(),
3535
}));
3636
vi.mock('../../../src/cli/cliReport');
3737

tests/cli/actions/workers/defaultActionWorker.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ vi.mock('../../../../src/shared/logger.js', () => ({
1919
setLogLevelByWorkerData: vi.fn(),
2020
}));
2121
vi.mock('../../../../src/cli/cliSpinner.js', () => ({
22-
Spinner: vi.fn().mockImplementation(() => ({
23-
start: vi.fn(),
24-
update: vi.fn(),
25-
succeed: vi.fn(),
26-
fail: vi.fn(),
27-
})),
22+
// biome-ignore lint/suspicious/noExplicitAny: Test mocks require flexible types
23+
Spinner: vi.fn(function (this: any) {
24+
this.start = vi.fn();
25+
this.update = vi.fn();
26+
this.succeed = vi.fn();
27+
this.fail = vi.fn();
28+
}),
2829
}));
2930

3031
const mockPack = vi.mocked(pack);

tests/core/git/gitHubArchive.test.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ const mockFs = {
2828
const mockZipData = new Uint8Array([0x50, 0x4b, 0x03, 0x04]); // Simple ZIP header
2929

3030
describe('gitHubArchive', () => {
31-
let mockFetch: ReturnType<typeof vi.fn>;
32-
let mockPipeline: ReturnType<typeof vi.fn>;
33-
let mockTransform: ReturnType<typeof vi.fn>;
34-
let mockCreateWriteStream: ReturnType<typeof vi.fn>;
35-
let mockUnzip: ReturnType<typeof vi.fn>;
31+
// biome-ignore lint/suspicious/noExplicitAny: Test mocks require flexible types
32+
let mockFetch: any;
33+
// biome-ignore lint/suspicious/noExplicitAny: Test mocks require flexible types
34+
let mockPipeline: any;
35+
// biome-ignore lint/suspicious/noExplicitAny: Test mocks require flexible types
36+
let mockTransform: any;
37+
// biome-ignore lint/suspicious/noExplicitAny: Test mocks require flexible types
38+
let mockCreateWriteStream: any;
39+
// biome-ignore lint/suspicious/noExplicitAny: Test mocks require flexible types
40+
let mockUnzip: any;
3641

3742
beforeEach(async () => {
3843
vi.clearAllMocks();
@@ -94,7 +99,8 @@ describe('gitHubArchive', () => {
9499
});
95100

96101
// Mock unzip to extract files
97-
mockUnzip.mockImplementation((_data, callback) => {
102+
// biome-ignore lint/suspicious/noExplicitAny: Test mock callback parameters
103+
mockUnzip.mockImplementation((_data: any, callback: any) => {
98104
const extracted = {
99105
'repomix-main/': new Uint8Array(0), // Directory
100106
'repomix-main/test.txt': new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f]), // "hello"
@@ -150,7 +156,8 @@ describe('gitHubArchive', () => {
150156
body: mockStream,
151157
});
152158

153-
mockUnzip.mockImplementation((_data, callback) => {
159+
// biome-ignore lint/suspicious/noExplicitAny: Test mock callback parameters
160+
mockUnzip.mockImplementation((_data: any, callback: any) => {
154161
callback(null, {});
155162
});
156163

@@ -187,7 +194,8 @@ describe('gitHubArchive', () => {
187194
}),
188195
});
189196

190-
mockUnzip.mockImplementation((_data, callback) => {
197+
// biome-ignore lint/suspicious/noExplicitAny: Test mock callback parameters
198+
mockUnzip.mockImplementation((_data: any, callback: any) => {
191199
callback(null, {});
192200
});
193201

@@ -226,7 +234,8 @@ describe('gitHubArchive', () => {
226234
}),
227235
});
228236

229-
mockUnzip.mockImplementation((_data, callback) => {
237+
// biome-ignore lint/suspicious/noExplicitAny: Test mock callback parameters
238+
mockUnzip.mockImplementation((_data: any, callback: any) => {
230239
callback(null, {});
231240
});
232241

@@ -287,7 +296,8 @@ describe('gitHubArchive', () => {
287296
});
288297

289298
// Mock unzip to fail
290-
mockUnzip.mockImplementation((_data, callback) => {
299+
// biome-ignore lint/suspicious/noExplicitAny: Test mock callback parameters
300+
mockUnzip.mockImplementation((_data: any, callback: any) => {
291301
callback(new Error('Invalid ZIP file'));
292302
});
293303

@@ -318,7 +328,8 @@ describe('gitHubArchive', () => {
318328
});
319329

320330
// Mock unzip with dangerous paths
321-
mockUnzip.mockImplementation((_data, callback) => {
331+
// biome-ignore lint/suspicious/noExplicitAny: Test mock callback parameters
332+
mockUnzip.mockImplementation((_data: any, callback: any) => {
322333
const extracted = {
323334
'repomix-main/../../../etc/passwd': new Uint8Array([0x65, 0x76, 0x69, 0x6c]), // "evil"
324335
'repomix-main/safe.txt': new Uint8Array([0x73, 0x61, 0x66, 0x65]), // "safe"
@@ -360,7 +371,8 @@ describe('gitHubArchive', () => {
360371
});
361372

362373
// Mock unzip with absolute path
363-
mockUnzip.mockImplementation((_data, callback) => {
374+
// biome-ignore lint/suspicious/noExplicitAny: Test mock callback parameters
375+
mockUnzip.mockImplementation((_data: any, callback: any) => {
364376
const extracted = {
365377
'/etc/passwd': new Uint8Array([0x65, 0x76, 0x69, 0x6c]), // "evil"
366378
'repomix-main/safe.txt': new Uint8Array([0x73, 0x61, 0x66, 0x65]), // "safe"
@@ -402,7 +414,8 @@ describe('gitHubArchive', () => {
402414
});
403415

404416
// Mock unzip to fail
405-
mockUnzip.mockImplementation((_data, callback) => {
417+
// biome-ignore lint/suspicious/noExplicitAny: Test mock callback parameters
418+
mockUnzip.mockImplementation((_data: any, callback: any) => {
406419
callback(new Error('Extraction failed'));
407420
});
408421

tests/core/metrics/diffTokenCount.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ import { createMockConfig } from '../../testing/testUtils.js';
77

88
// Mock the TokenCounter
99
vi.mock('../../../src/core/metrics/TokenCounter.js', () => ({
10-
TokenCounter: vi.fn(),
10+
// biome-ignore lint/suspicious/noExplicitAny: Test mocks require flexible types
11+
TokenCounter: vi.fn() as any,
1112
}));
1213

1314
describe('Diff Token Count Calculation', () => {
1415
beforeEach(() => {
1516
vi.resetAllMocks();
1617

1718
// Setup TokenCounter mock
18-
vi.mocked(TokenCounter).mockReturnValue({
19+
// biome-ignore lint/suspicious/noExplicitAny: Test mocks require flexible types
20+
(TokenCounter as any).mockReturnValue({
1921
countTokens: vi.fn((content) => {
2022
// Simple token counting for testing
2123
return content.split(/\s+/).length;

tests/mcp/tools/grepRepomixOutputTool.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('grepRepomixOutputTool', () => {
3939
});
4040

4141
it('should use dependency injection for RegExp', () => {
42-
const mockRegExp = vi.fn().mockReturnValue(/test/g) as unknown as RegExpConstructor;
42+
const mockRegExp = vi.fn(() => /test/g) as unknown as RegExpConstructor;
4343
createRegexPattern('test', false, { RegExp: mockRegExp });
4444
expect(mockRegExp).toHaveBeenCalledWith('test', 'g');
4545
});

tests/shared/processConcurrency.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ vi.mock('tinypool');
1414
describe('processConcurrency', () => {
1515
describe('getProcessConcurrency', () => {
1616
it('should use os.availableParallelism when available', () => {
17-
const mockAvailableParallelism = vi.fn().mockReturnValue(4);
17+
const mockAvailableParallelism = vi.fn(() => 4);
1818
vi.mocked(os).availableParallelism = mockAvailableParallelism;
1919

2020
const result = getProcessConcurrency();
@@ -26,7 +26,7 @@ describe('processConcurrency', () => {
2626

2727
describe('getWorkerThreadCount', () => {
2828
beforeEach(() => {
29-
vi.mocked(os).availableParallelism = vi.fn().mockReturnValue(8);
29+
vi.mocked(os).availableParallelism = vi.fn(() => 8);
3030
});
3131

3232
it('should return minimum 1 thread', () => {
@@ -67,7 +67,7 @@ describe('processConcurrency', () => {
6767

6868
describe('initWorker', () => {
6969
beforeEach(() => {
70-
vi.mocked(os).availableParallelism = vi.fn().mockReturnValue(4);
70+
vi.mocked(os).availableParallelism = vi.fn(() => 4);
7171
vi.mocked(Tinypool).mockImplementation(() => ({}) as Tinypool);
7272
});
7373

@@ -115,7 +115,7 @@ describe('processConcurrency', () => {
115115

116116
describe('initTaskRunner', () => {
117117
beforeEach(() => {
118-
vi.mocked(os).availableParallelism = vi.fn().mockReturnValue(4);
118+
vi.mocked(os).availableParallelism = vi.fn(() => 4);
119119
vi.mocked(Tinypool).mockImplementation(
120120
() =>
121121
({

0 commit comments

Comments
 (0)