@@ -86,6 +86,14 @@ t.test('remove single installed lib', async t => {
86
86
t . test ( 'remove multiple installed libs' , async t => {
87
87
const { uninstall, prefix } = await mockNpm ( t , {
88
88
prefixDir : {
89
+ 'package.json' : JSON . stringify ( {
90
+ name : 'test-rm-multiple-lib' ,
91
+ version : '1.0.0' ,
92
+ dependencies : {
93
+ a : '*' ,
94
+ b : '*' ,
95
+ } ,
96
+ } ) ,
89
97
node_modules : {
90
98
a : {
91
99
'package.json' : JSON . stringify ( {
@@ -139,18 +147,8 @@ t.test('remove multiple installed libs', async t => {
139
147
140
148
await uninstall ( [ 'b' ] )
141
149
142
- t . throws ( ( ) => fs . statSync ( a ) , 'should have removed a package from nm' )
143
150
t . throws ( ( ) => fs . statSync ( b ) , 'should have removed b package from nm' )
144
- } )
145
-
146
- t . test ( 'no args local' , async t => {
147
- const { uninstall } = await mockNpm ( t )
148
-
149
- await t . rejects (
150
- uninstall ( [ ] ) ,
151
- / M u s t p r o v i d e a p a c k a g e n a m e t o r e m o v e / ,
152
- 'should throw package name required error'
153
- )
151
+ t . ok ( fs . statSync ( a ) , 'should not have removed a package from nm' )
154
152
} )
155
153
156
154
t . test ( 'no args global' , async t => {
@@ -200,3 +198,87 @@ t.test('non ENOENT error reading from localPrefix package.json', async t => {
200
198
'should throw non ENOENT error'
201
199
)
202
200
} )
201
+
202
+ t . test ( 'package.json validation' , async t => {
203
+ await t . test ( 'no package.json in local uninstall' , async t => {
204
+ const { uninstall } = await mockNpm ( t , {
205
+ prefixDir : { } , // empty directory
206
+ } )
207
+ await t . rejects (
208
+ uninstall ( [ 'some-package' ] ) ,
209
+ {
210
+ code : 'ENOPROJECT' ,
211
+ message : 'No package.json found. Run "npm init" to create a new package.' ,
212
+ } ,
213
+ 'should throw ENOPROJECT error'
214
+ )
215
+ } )
216
+
217
+ await t . test ( 'no package.json in local uninstall' , async t => {
218
+ const { uninstall } = await mockNpm ( t , {
219
+ prefixDir : { } , // empty directory
220
+ } )
221
+ await t . rejects (
222
+ uninstall ( [ 'some-package' ] ) ,
223
+ {
224
+ code : 'ENOPROJECT' ,
225
+ message : 'No package.json found. Run "npm init" to create a new package.' ,
226
+ }
227
+ )
228
+ } )
229
+ } )
230
+
231
+ t . test ( 'validation error handling' , async t => {
232
+ const { uninstall } = await mockNpm ( t , {
233
+ prefixDir : { } ,
234
+ mocks : {
235
+ '{LIB}/utils/validate-project.js' : ( ) => {
236
+ throw new Error ( 'Generic error' )
237
+ } ,
238
+ } ,
239
+ } )
240
+ await t . rejects (
241
+ uninstall ( [ 'some-package' ] ) ,
242
+ / G e n e r i c e r r o r / ,
243
+ 'should throw through non-ENOPROJECT errors'
244
+ )
245
+ } )
246
+
247
+ t . test ( 'no args with package name validation' , async t => {
248
+ const { uninstall } = await mockNpm ( t , {
249
+ prefixDir : {
250
+ 'package.json' : JSON . stringify ( {
251
+ name : 'test-pkg' ,
252
+ version : '1.0.0' ,
253
+ } ) ,
254
+ } ,
255
+ } )
256
+ await t . rejects (
257
+ uninstall ( [ ] ) ,
258
+ {
259
+ message : 'Must provide a package name to remove' ,
260
+ } ,
261
+ 'should throw correct error message'
262
+ )
263
+ } )
264
+
265
+ t . test ( 'handles non-ENOPROJECT validation errors' , async t => {
266
+ const { uninstall } = await mockNpm ( t , {
267
+ prefixDir : { } ,
268
+ mocks : {
269
+ '{LIB}/utils/validate-project.js' : ( ) => {
270
+ const err = new Error ( 'Different error' )
271
+ err . code = 'EDIFFERENT'
272
+ throw err
273
+ } ,
274
+ } ,
275
+ } )
276
+ await t . rejects (
277
+ uninstall ( [ 'some-package' ] ) ,
278
+ {
279
+ code : 'EDIFFERENT' ,
280
+ message : 'Different error' ,
281
+ } ,
282
+ 'should throw through other errors unchanged'
283
+ )
284
+ } )
0 commit comments