Skip to content

Commit cd1479e

Browse files
committed
fix(test): use proper function syntax for Vitest constructor mocks
Vitest requires mocks used as constructors to use 'function' keyword instead of arrow functions. This fixes 15 failing unit tests across two test files. Changes: - providers.test.mts: Use vi.fn(function() {...}) for Gitlab mock - config.test.mts: Replace all arrow functions with proper function syntax in mock implementations All 25 tests in both files now pass successfully.
1 parent 26f016b commit cd1479e

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

packages/cli/src/utils/git/providers.test.mts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@ vi.mock('./operations.mts', () => ({
2424
}))
2525

2626
vi.mock('@gitbeaker/rest', () => ({
27-
Gitlab: vi.fn().mockImplementation(() => ({
28-
MergeRequests: {
29-
create: vi.fn(),
30-
show: vi.fn(),
31-
rebase: vi.fn(),
32-
all: vi.fn(),
33-
},
34-
MergeRequestNotes: {
35-
create: vi.fn(),
36-
},
37-
})),
27+
Gitlab: vi.fn(function () {
28+
return {
29+
MergeRequests: {
30+
create: vi.fn(),
31+
show: vi.fn(),
32+
rebase: vi.fn(),
33+
all: vi.fn(),
34+
},
35+
MergeRequestNotes: {
36+
create: vi.fn(),
37+
},
38+
}
39+
}),
3840
}))
3941

4042
describe('provider-factory', () => {

packages/cli/src/utils/npm/config.test.mts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
import { beforeEach, describe, expect, it, vi } from 'vitest'
22

3-
// Create mock constructor and instance in hoisted scope
4-
const { MockNpmConfig, mockNpmConfigInstance } = vi.hoisted(() => {
5-
const mockNpmConfigInstance = {
6-
load: vi.fn().mockResolvedValue(undefined),
7-
flat: {
8-
registry: 'https://registry.npmjs.org/',
9-
cache: '/home/user/.npm',
10-
prefix: '/usr/local',
11-
},
12-
}
13-
14-
// Create a proper constructor function that vitest can mock
15-
function MockNpmConfig() {
16-
return mockNpmConfigInstance
17-
}
18-
19-
return { MockNpmConfig: vi.fn(MockNpmConfig), mockNpmConfigInstance }
20-
})
3+
// Create mock instance that will be returned by constructor
4+
const mockNpmConfigInstance = {
5+
load: vi.fn().mockResolvedValue(undefined),
6+
flat: {
7+
registry: 'https://registry.npmjs.org/',
8+
cache: '/home/user/.npm',
9+
prefix: '/usr/local',
10+
},
11+
}
2112

2213
// Mock @npmcli/config.
2314
vi.mock('@npmcli/config', () => ({
24-
default: MockNpmConfig,
15+
default: vi.fn(function () {
16+
return mockNpmConfigInstance
17+
}),
2518
}))
2619

2720
import { getNpmConfig } from './config.mts'
21+
import NpmConfig from '@npmcli/config'
2822

2923
// Mock @npmcli/config/lib/definitions.
3024
vi.mock('@npmcli/config/lib/definitions', () => ({
@@ -38,11 +32,15 @@ vi.mock('./paths.mts', () => ({
3832
getNpmDirPath: vi.fn(() => '/usr/local/lib/node_modules/npm'),
3933
}))
4034

35+
const MockNpmConfig = vi.mocked(NpmConfig)
36+
4137
describe('npm-config utilities', () => {
4238
beforeEach(() => {
4339
// Clear mock calls and restore original implementation
4440
MockNpmConfig.mockClear()
45-
MockNpmConfig.mockImplementation(() => mockNpmConfigInstance)
41+
MockNpmConfig.mockImplementation(function () {
42+
return mockNpmConfigInstance
43+
})
4644
vi.mocked(mockNpmConfigInstance.load).mockClear()
4745
})
4846

@@ -143,7 +141,9 @@ describe('npm-config utilities', () => {
143141
flat: { test: 'value' },
144142
}
145143
vi.mocked((await import('@npmcli/config')).default).mockImplementation(
146-
() => mockConfigInstance,
144+
function () {
145+
return mockConfigInstance
146+
},
147147
)
148148

149149
await getNpmConfig()

0 commit comments

Comments
 (0)