11import { 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.
2129vi . mock ( '@npmcli/config/lib/definitions' , ( ) => ( {
2230 definitions : { } ,
@@ -31,7 +39,9 @@ vi.mock('./paths.mts', () => ({
3139
3240describe ( '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