@@ -13,25 +13,28 @@ import {
13
13
installSyncSaveDev ,
14
14
fetchPeerDependencies ,
15
15
checkDeps ,
16
- checkDevDeps
16
+ checkDevDeps ,
17
+ checkPackageJson
17
18
} from "../../lib/utils/npm-utils.js" ;
18
19
import { defineInMemoryFs } from "../_utils/in-memory-fs.js" ;
19
- import esmock from "esmock" ;
20
20
import { assert , describe , afterEach , it } from "vitest" ;
21
+ import fs from "fs" ;
21
22
22
23
//------------------------------------------------------------------------------
23
24
// Helpers
24
25
//------------------------------------------------------------------------------
25
26
26
27
/**
27
- * Import `npm-utils` with the in-memory file system.
28
+ * Replace native fs methods with the in-memory file system methods used by `npm-utils` .
28
29
* @param {Object } files The file definitions.
29
- * @returns {Object } `npm-utils`.
30
+ * @returns {void }
30
31
*/
31
- async function requireNpmUtilsWithInMemoryFileSystem ( files ) {
32
- const fs = defineInMemoryFs ( { files } ) ;
32
+ async function useInMemoryFileSystem ( files ) {
33
+ const inMemoryFs = defineInMemoryFs ( { files } ) ;
33
34
34
- return await esmock ( "../../lib/utils/npm-utils.js" , { fs } ) ;
35
+ sinon . replace ( fs , "readFileSync" , inMemoryFs . readFileSync ) ;
36
+ sinon . replace ( fs , "existsSync" , inMemoryFs . existsSync ) ;
37
+ sinon . replace ( fs , "statSync" , inMemoryFs . statSync ) ;
35
38
}
36
39
37
40
//------------------------------------------------------------------------------
@@ -68,21 +71,21 @@ describe("npmUtils", () => {
68
71
} ) ;
69
72
70
73
it ( "should handle missing devDependencies key" , async ( ) => {
71
- const { checkDevDeps : stubcheckDevDeps } = await requireNpmUtilsWithInMemoryFileSystem ( {
74
+ await useInMemoryFileSystem ( {
72
75
"package.json" : JSON . stringify ( { private : true , dependencies : { } } )
73
76
} ) ;
74
77
75
78
// Should not throw.
76
- stubcheckDevDeps ( [ "some-package" ] ) ;
79
+ checkDevDeps ( [ "some-package" ] ) ;
77
80
} ) ;
78
81
79
82
it ( "should throw with message when parsing invalid package.json" , async ( ) => {
80
- const { checkDevDeps : stubcheckDevDeps } = await requireNpmUtilsWithInMemoryFileSystem ( {
83
+ await useInMemoryFileSystem ( {
81
84
"package.json" : '{ "not: "valid json" }'
82
85
} ) ;
83
86
84
87
assert . throws ( ( ) => {
85
- stubcheckDevDeps ( [ "some-package" ] ) ;
88
+ checkDevDeps ( [ "some-package" ] ) ;
86
89
} , / J S O N / u) ;
87
90
} ) ;
88
91
} ) ;
@@ -118,38 +121,38 @@ describe("npmUtils", () => {
118
121
} ) ;
119
122
120
123
it ( "should handle missing dependencies key" , async ( ) => {
121
- const { checkDeps : stubbedcheckDeps } = await requireNpmUtilsWithInMemoryFileSystem ( {
124
+ await useInMemoryFileSystem ( {
122
125
"package.json" : JSON . stringify ( { private : true , devDependencies : { } } )
123
126
} ) ;
124
127
125
128
// Should not throw.
126
- stubbedcheckDeps ( [ "some-package" ] ) ;
129
+ checkDeps ( [ "some-package" ] ) ;
127
130
} ) ;
128
131
129
132
it ( "should throw with message when parsing invalid package.json" , async ( ) => {
130
- const { checkDeps : stubbedcheckDeps } = await requireNpmUtilsWithInMemoryFileSystem ( {
133
+ await useInMemoryFileSystem ( {
131
134
"package.json" : '{ "not: "valid json" }'
132
135
} ) ;
133
136
134
137
assert . throws ( ( ) => {
135
- stubbedcheckDeps ( [ "some-package" ] ) ;
138
+ checkDeps ( [ "some-package" ] ) ;
136
139
} , / J S O N / u) ;
137
140
} ) ;
138
141
} ) ;
139
142
140
143
describe ( "checkPackageJson()" , ( ) => {
141
144
it ( "should return true if package.json exists" , async ( ) => {
142
- const { checkPackageJson : stubbedcheckPackageJson } = await requireNpmUtilsWithInMemoryFileSystem ( {
145
+ await useInMemoryFileSystem ( {
143
146
"package.json" : '{ "file": "contents" }'
144
147
} ) ;
145
148
146
- assert . strictEqual ( stubbedcheckPackageJson ( ) , true ) ;
149
+ assert . strictEqual ( checkPackageJson ( ) , true ) ;
147
150
} ) ;
148
151
149
152
it ( "should return false if package.json does not exist" , async ( ) => {
150
- const { checkPackageJson : stubbedcheckPackageJson } = await requireNpmUtilsWithInMemoryFileSystem ( { } ) ;
153
+ await useInMemoryFileSystem ( { } ) ;
151
154
152
- assert . strictEqual ( stubbedcheckPackageJson ( ) , false ) ;
155
+ assert . strictEqual ( checkPackageJson ( ) , false ) ;
153
156
} ) ;
154
157
} ) ;
155
158
@@ -188,14 +191,11 @@ describe("npmUtils", () => {
188
191
it ( "should log an error message if npm throws ENOENT error" , async ( ) => {
189
192
const logErrorStub = sinon . spy ( ) ;
190
193
const npmUtilsStub = sinon . stub ( spawn , "sync" ) . returns ( { error : { code : "ENOENT" } } ) ;
194
+ const log = await import ( "../../lib/utils/logging.js" ) ;
191
195
192
- const { installSyncSaveDev : stubinstallSyncSaveDev } = await esmock ( "../../lib/utils/npm-utils.js" , {
193
- "../../lib/utils/logging.js" : {
194
- error : logErrorStub
195
- }
196
- } ) ;
196
+ sinon . replaceGetter ( log , "error" , ( ) => logErrorStub ) ;
197
197
198
- stubinstallSyncSaveDev ( "some-package" ) ;
198
+ installSyncSaveDev ( "some-package" ) ;
199
199
200
200
assert ( logErrorStub . calledOnce ) ;
201
201
0 commit comments