Skip to content

Commit 5316643

Browse files
committed
fix(tests): fix npm-config mock constructor to work with 'new' operator
- Change mock function from arrow function to regular function - Ensure MockNpmConfig can be called with 'new' operator - Fix test that was overriding implementation to use function syntax This fixes 12 failing tests where vitest mocks weren't working as constructors due to arrow function syntax.
1 parent 19d6ea8 commit 5316643

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ const { MockNpmConfig, mockNpmConfigInstance } = vi.hoisted(() => {
1111
},
1212
}
1313

14-
const MockNpmConfig = vi.fn().mockImplementation(() => mockNpmConfigInstance)
14+
// Create a proper constructor function that vitest can mock
15+
function MockNpmConfig() {
16+
return mockNpmConfigInstance
17+
}
1518

16-
return { MockNpmConfig, mockNpmConfigInstance }
19+
return { MockNpmConfig: vi.fn(MockNpmConfig), mockNpmConfigInstance }
1720
})
1821

1922
// Mock @npmcli/config.
@@ -37,8 +40,11 @@ vi.mock('./paths.mts', () => ({
3740

3841
describe('npm-config utilities', () => {
3942
beforeEach(() => {
40-
// Clear only the mock calls, not the implementation
43+
// Clear mock calls and restore original implementation
4144
MockNpmConfig.mockClear()
45+
MockNpmConfig.mockImplementation(function () {
46+
return mockNpmConfigInstance
47+
})
4248
vi.mocked(mockNpmConfigInstance.load).mockClear()
4349
})
4450

@@ -134,11 +140,14 @@ describe('npm-config utilities', () => {
134140

135141
it('calls config.load()', async () => {
136142
const mockLoad = vi.fn().mockResolvedValue(undefined)
143+
const mockConfigInstance = {
144+
load: mockLoad,
145+
flat: { test: 'value' },
146+
}
137147
vi.mocked((await import('@npmcli/config')).default).mockImplementation(
138-
(() => ({
139-
load: mockLoad,
140-
flat: { test: 'value' },
141-
})) as any,
148+
function () {
149+
return mockConfigInstance
150+
},
142151
)
143152

144153
await getNpmConfig()

0 commit comments

Comments
 (0)