Skip to content

Commit 31e0ed3

Browse files
committed
fix(tests): update test imports and fix NpmConfig mock
- Updated import paths in test files after file renames: - ecosystem/ecosystem.mts → ecosystem/types.mts - fs/fs.mts → fs/find-up.mts - git/git.mts → git/operations.mts - Fixed NpmConfig mock to use function keyword instead of arrow function for proper constructor support with new operator - Used vi.hoisted() for proper mock hoisting before imports
1 parent 8bb0f10 commit 31e0ed3

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

packages/cli/src/utils/ecosystem/types.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
getEcosystemChoicesForMeow,
77
isValidEcosystem,
88
parseEcosystems,
9-
} from './ecosystem.mts'
9+
} from './types.mts'
1010

1111
describe('ecosystem utilities', () => {
1212
describe('ALL_ECOSYSTEMS', () => {

packages/cli/src/utils/fs/fs.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from 'node:path'
44

55
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
66

7-
import { findUp } from './fs.mts'
7+
import { findUp } from './find-up.mts'
88

99
describe('fs utilities', () => {
1010
describe('findUp', () => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
gitPushBranch,
1313
gitResetHard,
1414
parseGitRemoteUrl,
15-
} from './git.mts'
15+
} from './operations.mts'
1616

1717
// Mock spawn.
1818
vi.mock('@socketsecurity/lib/spawn', () => ({

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

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

3-
import { getNpmConfig } from './config.mts'
4-
5-
// Mock @npmcli/config.
6-
vi.mock('@npmcli/config', () => {
7-
const MockNpmConfig = vi.fn().mockImplementation(() => ({
3+
// Create mock constructor and instance in hoisted scope
4+
const { MockNpmConfig, mockNpmConfigInstance } = vi.hoisted(() => {
5+
const mockNpmConfigInstance = {
86
load: vi.fn().mockResolvedValue(undefined),
97
flat: {
108
registry: 'https://registry.npmjs.org/',
119
cache: '/home/user/.npm',
1210
prefix: '/usr/local',
1311
},
14-
}))
15-
return {
16-
default: MockNpmConfig,
1712
}
13+
14+
const MockNpmConfig = vi.fn().mockImplementation(function () {
15+
return mockNpmConfigInstance
16+
})
17+
18+
return { MockNpmConfig, mockNpmConfigInstance }
1819
})
1920

21+
// Mock @npmcli/config.
22+
vi.mock('@npmcli/config', () => ({
23+
default: MockNpmConfig,
24+
}))
25+
26+
import { getNpmConfig } from './config.mts'
27+
2028
// Mock @npmcli/config/lib/definitions.
2129
vi.mock('@npmcli/config/lib/definitions', () => ({
2230
definitions: {},
@@ -31,7 +39,9 @@ vi.mock('./paths.mts', () => ({
3139

3240
describe('npm-config utilities', () => {
3341
beforeEach(() => {
34-
vi.clearAllMocks()
42+
// Clear only the mock calls, not the implementation
43+
MockNpmConfig.mockClear()
44+
vi.mocked(mockNpmConfigInstance.load).mockClear()
3545
})
3646

3747
describe('getNpmConfig', () => {
@@ -48,48 +58,48 @@ describe('npm-config utilities', () => {
4858
})
4959

5060
it('uses custom cwd option', async () => {
51-
const NpmConfig = (await import('@npmcli/config')).default
61+
// Use MockNpmConfig directly
5262

5363
await getNpmConfig({ cwd: '/custom/path' })
5464

55-
expect(NpmConfig).toHaveBeenCalledWith(
65+
expect(MockNpmConfig).toHaveBeenCalledWith(
5666
expect.objectContaining({
5767
cwd: '/custom/path',
5868
}),
5969
)
6070
})
6171

6272
it('uses custom env option', async () => {
63-
const NpmConfig = (await import('@npmcli/config')).default
73+
// Use MockNpmConfig directly
6474
const customEnv = { NODE_ENV: 'test', FOO: 'bar' }
6575

6676
await getNpmConfig({ env: customEnv })
6777

68-
expect(NpmConfig).toHaveBeenCalledWith(
78+
expect(MockNpmConfig).toHaveBeenCalledWith(
6979
expect.objectContaining({
7080
env: customEnv,
7181
}),
7282
)
7383
})
7484

7585
it('uses custom npmPath option', async () => {
76-
const NpmConfig = (await import('@npmcli/config')).default
86+
// Use MockNpmConfig directly
7787

7888
await getNpmConfig({ npmPath: '/custom/npm/path' })
7989

80-
expect(NpmConfig).toHaveBeenCalledWith(
90+
expect(MockNpmConfig).toHaveBeenCalledWith(
8191
expect.objectContaining({
8292
npmPath: '/custom/npm/path',
8393
}),
8494
)
8595
})
8696

8797
it('uses custom platform option', async () => {
88-
const NpmConfig = (await import('@npmcli/config')).default
98+
// Use MockNpmConfig directly
8999

90100
await getNpmConfig({ platform: 'win32' })
91101

92-
expect(NpmConfig).toHaveBeenCalledWith(
102+
expect(MockNpmConfig).toHaveBeenCalledWith(
93103
expect.objectContaining({
94104
platform: 'win32',
95105
}),
@@ -113,11 +123,11 @@ describe('npm-config utilities', () => {
113123
})
114124

115125
it('handles execPath option', async () => {
116-
const NpmConfig = (await import('@npmcli/config')).default
126+
// Use MockNpmConfig directly
117127

118128
await getNpmConfig({ execPath: '/usr/bin/node' })
119129

120-
expect(NpmConfig).toHaveBeenCalledWith(
130+
expect(MockNpmConfig).toHaveBeenCalledWith(
121131
expect.objectContaining({
122132
execPath: '/usr/bin/node',
123133
}),
@@ -127,11 +137,12 @@ describe('npm-config utilities', () => {
127137
it('calls config.load()', async () => {
128138
const mockLoad = vi.fn().mockResolvedValue(undefined)
129139
vi.mocked((await import('@npmcli/config')).default).mockImplementation(
130-
() =>
131-
({
140+
function () {
141+
return {
132142
load: mockLoad,
133143
flat: { test: 'value' },
134-
}) as any,
144+
}
145+
} as any,
135146
)
136147

137148
await getNpmConfig()

0 commit comments

Comments
 (0)