1
1
const t = require ( 'tap' )
2
- const fs = require ( 'fs' )
3
- const { resolve } = require ( 'path' )
4
- const { fake : mockNpm } = require ( '../../fixtures/mock-npm' )
5
-
6
- const config = {
7
- cache : 'bad-cache-dir' ,
8
- 'init-module' : '~/.npm-init.js' ,
9
- yes : true ,
10
- }
11
- const flatOptions = {
12
- cache : 'test-config-dir/_cacache' ,
13
- npxCache : 'test-config-dir/_npx' ,
14
- }
15
- const npm = mockNpm ( {
16
- flatOptions,
17
- config,
18
- } )
19
- const mocks = {
20
- npmlog : {
21
- disableProgress : ( ) => null ,
22
- enableProgress : ( ) => null ,
23
- } ,
24
- 'proc-log' : {
25
- info : ( ) => null ,
26
- pause : ( ) => null ,
27
- resume : ( ) => null ,
28
- silly : ( ) => null ,
29
- } ,
2
+ const fs = require ( 'fs/promises' )
3
+ const { resolve, basename } = require ( 'path' )
4
+ const _mockNpm = require ( '../../fixtures/mock-npm' )
5
+ const { cleanTime } = require ( '../../fixtures/clean-snapshot' )
6
+
7
+ t . cleanSnapshot = cleanTime
8
+
9
+ const mockNpm = async ( t , { noLog, libnpmexec, initPackageJson, packageJson, ...opts } = { } ) => {
10
+ const res = await _mockNpm ( t , {
11
+ ...opts ,
12
+ mocks : {
13
+ ...( libnpmexec ? { libnpmexec } : { } ) ,
14
+ ...( initPackageJson ? { 'init-package-json' : initPackageJson } : { } ) ,
15
+ ...( packageJson ? { '@npmcli/package-json' : packageJson } : { } ) ,
16
+ } ,
17
+ globals : {
18
+ // init-package-json prints directly to console.log
19
+ // this avoids poluting test output with those logs
20
+ ...( noLog ? { 'console.log' : ( ) => { } } : { } ) ,
21
+ } ,
22
+ } )
23
+
24
+ return res
30
25
}
31
- const Init = t . mock ( '../../../lib/commands/init.js' , mocks )
32
- const init = new Init ( npm )
33
- const _cwd = process . cwd ( )
34
- const _consolelog = console . log
35
- const noop = ( ) => { }
36
-
37
- t . afterEach ( ( ) => {
38
- config . yes = true
39
- config . package = undefined
40
- process . chdir ( _cwd )
41
- console . log = _consolelog
26
+
27
+ t . test ( 'displays output' , async t => {
28
+ const { npm, joinedOutput } = await mockNpm ( t , {
29
+ initPackageJson : ( ...args ) => args [ 3 ] ( ) ,
30
+ } )
31
+
32
+ await npm . exec ( 'init' , [ ] )
33
+ t . matchSnapshot ( joinedOutput ( ) , 'displays helper info' )
42
34
} )
43
35
44
36
t . test ( 'classic npm init -y' , async t => {
45
- npm . localPrefix = t . testdir ( { } )
46
-
47
- // init-package-json prints directly to console.log
48
- // this avoids poluting test output with those logs
49
- console . log = noop
37
+ const { npm, prefix } = await mockNpm ( t , {
38
+ config : { yes : true } ,
39
+ noLog : true ,
40
+ } )
50
41
51
- process . chdir ( npm . localPrefix )
52
- await init . exec ( [ ] )
42
+ await npm . exec ( 'init' , [ ] )
53
43
54
- const pkg = require ( resolve ( npm . localPrefix , 'package.json' ) )
44
+ const pkg = require ( resolve ( prefix , 'package.json' ) )
55
45
t . equal ( pkg . version , '1.0.0' )
56
46
t . equal ( pkg . license , 'ISC' )
57
47
} )
58
48
59
49
t . test ( 'classic interactive npm init' , async t => {
60
- npm . localPrefix = t . testdir ( { } )
61
- config . yes = undefined
50
+ t . plan ( 1 )
62
51
63
- const Init = t . mock ( '../../../lib/commands/init.js' , {
64
- ...mocks ,
65
- 'init-package-json' : ( path , initFile , config , cb ) => {
52
+ const { npm } = await mockNpm ( t , {
53
+ initPackageJson : ( ...args ) => {
66
54
t . equal (
67
- path ,
55
+ args [ 0 ] ,
68
56
resolve ( npm . localPrefix ) ,
69
57
'should start init package.json in expected path'
70
58
)
71
- cb ( )
59
+ args [ 3 ] ( )
72
60
} ,
73
61
} )
74
- const init = new Init ( npm )
75
62
76
- process . chdir ( npm . localPrefix )
77
- await init . exec ( [ ] )
63
+ await npm . exec ( 'init' , [ ] )
78
64
} )
79
65
80
66
t . test ( 'npm init <arg>' , async t => {
81
- t . plan ( 3 )
82
- npm . localPrefix = t . testdir ( { } )
67
+ t . plan ( 1 )
83
68
84
- const Init = t . mock ( '../../../lib/commands/init.js' , {
85
- libnpmexec : ( { args, cache , npxCache } ) => {
69
+ const { npm } = await mockNpm ( t , {
70
+ libnpmexec : ( { args } ) => {
86
71
t . same (
87
72
args ,
88
73
[ 'create-react-app@*' ] ,
89
74
'should npx with listed packages'
90
75
)
91
- t . same ( cache , flatOptions . cache )
92
- t . same ( npxCache , flatOptions . npxCache )
93
76
} ,
94
77
} )
95
- const init = new Init ( npm )
96
78
97
- process . chdir ( npm . localPrefix )
98
- await init . exec ( [ 'react-app' ] )
79
+ await npm . exec ( 'init' , [ 'react-app' ] )
99
80
} )
100
81
101
82
t . test ( 'npm init <arg> -- other-args' , async t => {
102
83
t . plan ( 1 )
103
- npm . localPrefix = t . testdir ( { } )
104
84
105
- const Init = t . mock ( '../../../lib/commands/init.js' , {
85
+ const { npm } = await mockNpm ( t , {
106
86
libnpmexec : ( { args } ) => {
107
87
t . same (
108
88
args ,
109
89
[ 'create-react-app@*' , 'my-path' , '--some-option' , 'some-value' ] ,
110
90
'should npm exec with expected args'
111
91
)
112
92
} ,
93
+
113
94
} )
114
- const init = new Init ( npm )
115
95
116
- process . chdir ( npm . localPrefix )
117
- await init . exec ( [ 'react-app' , 'my-path' , '--some-option' , 'some-value' ] )
96
+ await npm . exec ( 'init' , [ 'react-app' , 'my-path' , '--some-option' , 'some-value' ] )
118
97
} )
119
98
120
99
t . test ( 'npm init @scope/name' , async t => {
121
100
t . plan ( 1 )
122
- npm . localPrefix = t . testdir ( { } )
123
101
124
- const Init = t . mock ( '../../../lib/commands/init.js' , {
102
+ const { npm } = await mockNpm ( t , {
125
103
libnpmexec : ( { args } ) => {
126
104
t . same (
127
105
args ,
@@ -130,17 +108,14 @@ t.test('npm init @scope/name', async t => {
130
108
)
131
109
} ,
132
110
} )
133
- const init = new Init ( npm )
134
111
135
- process . chdir ( npm . localPrefix )
136
- await init . exec ( [ '@npmcli/something' ] )
112
+ await npm . exec ( 'init' , [ '@npmcli/something' ] )
137
113
} )
138
114
139
115
t . test ( 'npm init @scope@spec' , async t => {
140
116
t . plan ( 1 )
141
- npm . localPrefix = t . testdir ( { } )
142
117
143
- const Init = t . mock ( '../../../lib/commands/init.js' , {
118
+ const { npm } = await mockNpm ( t , {
144
119
libnpmexec : ( { args } ) => {
145
120
t . same (
146
121
args ,
@@ -149,17 +124,14 @@ t.test('npm init @scope@spec', async t => {
149
124
)
150
125
} ,
151
126
} )
152
- const init = new Init ( npm )
153
127
154
- process . chdir ( npm . localPrefix )
155
- await init . exec ( [ '@npmcli@foo' ] )
128
+ await npm . exec ( 'init' , [ '@npmcli@foo' ] )
156
129
} )
157
130
158
131
t . test ( 'npm init @scope/name@spec' , async t => {
159
132
t . plan ( 1 )
160
- npm . localPrefix = t . testdir ( { } )
161
133
162
- const Init = t . mock ( '../../../lib/commands/init.js' , {
134
+ const { npm } = await mockNpm ( t , {
163
135
libnpmexec : ( { args } ) => {
164
136
t . same (
165
137
args ,
@@ -168,17 +140,13 @@ t.test('npm init @scope/name@spec', async t => {
168
140
)
169
141
} ,
170
142
} )
171
- const init = new Init ( npm )
172
143
173
- process . chdir ( npm . localPrefix )
174
- await init . exec ( [ '@npmcli/something@foo' ] )
144
+ await npm . exec ( 'init' , [ '@npmcli/something@foo' ] )
175
145
} )
176
146
177
147
t . test ( 'npm init git spec' , async t => {
178
148
t . plan ( 1 )
179
- npm . localPrefix = t . testdir ( { } )
180
-
181
- const Init = t . mock ( '../../../lib/commands/init.js' , {
149
+ const { npm } = await mockNpm ( t , {
182
150
libnpmexec : ( { args } ) => {
183
151
t . same (
184
152
args ,
@@ -187,17 +155,14 @@ t.test('npm init git spec', async t => {
187
155
)
188
156
} ,
189
157
} )
190
- const init = new Init ( npm )
191
158
192
- process . chdir ( npm . localPrefix )
193
- await init . exec ( [ 'npm/something' ] )
159
+ await npm . exec ( 'init' , [ 'npm/something' ] )
194
160
} )
195
161
196
162
t . test ( 'npm init @scope' , async t => {
197
163
t . plan ( 1 )
198
- npm . localPrefix = t . testdir ( { } )
199
164
200
- const Init = t . mock ( '../../../lib/commands/init.js' , {
165
+ const { npm } = await mockNpm ( t , {
201
166
libnpmexec : ( { args } ) => {
202
167
t . same (
203
168
args ,
@@ -206,28 +171,24 @@ t.test('npm init @scope', async t => {
206
171
)
207
172
} ,
208
173
} )
209
- const init = new Init ( npm )
210
174
211
- process . chdir ( npm . localPrefix )
212
- await init . exec ( [ '@npmcli' ] )
175
+ await npm . exec ( 'init' , [ '@npmcli' ] )
213
176
} )
214
177
215
178
t . test ( 'npm init tgz' , async t => {
216
- npm . localPrefix = t . testdir ( { } )
179
+ const { npm } = await mockNpm ( t )
217
180
218
- process . chdir ( npm . localPrefix )
219
181
await t . rejects (
220
- init . exec ( [ 'something.tgz' ] ) ,
182
+ npm . exec ( 'init' , [ 'something.tgz' ] ) ,
221
183
/ U n r e c o g n i z e d i n i t i a l i z e r : s o m e t h i n g .t g z / ,
222
184
'should throw error when using an unsupported spec'
223
185
)
224
186
} )
225
187
226
188
t . test ( 'npm init <arg>@next' , async t => {
227
189
t . plan ( 1 )
228
- npm . localPrefix = t . testdir ( { } )
229
190
230
- const Init = t . mock ( '../../../lib/commands/init.js' , {
191
+ const { npm } = await mockNpm ( t , {
231
192
libnpmexec : ( { args } ) => {
232
193
t . same (
233
194
args ,
@@ -236,35 +197,28 @@ t.test('npm init <arg>@next', async t => {
236
197
)
237
198
} ,
238
199
} )
239
- const init = new Init ( npm )
240
200
241
- process . chdir ( npm . localPrefix )
242
- await init . exec ( [ 'something@next' ] )
201
+ await npm . exec ( 'init' , [ 'something@next' ] )
243
202
} )
244
203
245
204
t . test ( 'npm init exec error' , async t => {
246
- npm . localPrefix = t . testdir ( { } )
247
-
248
- const Init = t . mock ( '../../../lib/commands/init.js' , {
249
- libnpmexec : async ( { args } ) => {
205
+ const { npm } = await mockNpm ( t , {
206
+ libnpmexec : async ( ) => {
250
207
throw new Error ( 'ERROR' )
251
208
} ,
252
209
} )
253
- const init = new Init ( npm )
254
210
255
- process . chdir ( npm . localPrefix )
256
211
await t . rejects (
257
- init . exec ( [ 'something@next' ] ) ,
212
+ npm . exec ( 'init' , [ 'something@next' ] ) ,
258
213
/ E R R O R / ,
259
214
'should exit with exec error'
260
215
)
261
216
} )
262
217
263
218
t . test ( 'should not rewrite flatOptions' , async t => {
264
219
t . plan ( 1 )
265
- npm . localPrefix = t . testdir ( { } )
266
220
267
- const Init = t . mock ( '../../../lib/commands/init.js' , {
221
+ const { npm } = await mockNpm ( t , {
268
222
libnpmexec : async ( { args } ) => {
269
223
t . same (
270
224
args ,
@@ -273,270 +227,198 @@ t.test('should not rewrite flatOptions', async t => {
273
227
)
274
228
} ,
275
229
} )
276
- const init = new Init ( npm )
277
230
278
- process . chdir ( npm . localPrefix )
279
- await init . exec ( [ 'react-app' , 'my-app' ] )
231
+ await npm . exec ( 'init' , [ 'react-app' , 'my-app' ] )
280
232
} )
281
233
282
234
t . test ( 'npm init cancel' , async t => {
283
- t . plan ( 2 )
284
- npm . localPrefix = t . testdir ( { } )
285
-
286
- const Init = t . mock ( '../../../lib/commands/init.js' , {
287
- ...mocks ,
288
- 'init-package-json' : ( dir , initFile , config , cb ) => cb (
235
+ const { npm, logs } = await mockNpm ( t , {
236
+ initPackageJson : ( ...args ) => args [ 3 ] (
289
237
new Error ( 'canceled' )
290
238
) ,
291
- 'proc-log' : {
292
- ...mocks [ 'proc-log' ] ,
293
- warn : ( title , msg ) => {
294
- t . equal ( title , 'init' , 'should have init title' )
295
- t . equal ( msg , 'canceled' , 'should log canceled' )
296
- } ,
297
- } ,
298
239
} )
299
- const init = new Init ( npm )
300
240
301
- process . chdir ( npm . localPrefix )
302
- await init . exec ( [ ] )
241
+ await npm . exec ( 'init' , [ ] )
242
+
243
+ t . equal ( logs . warn [ 0 ] [ 0 ] , 'init' , 'should have init title' )
244
+ t . equal ( logs . warn [ 0 ] [ 1 ] , 'canceled' , 'should log canceled' )
303
245
} )
304
246
305
247
t . test ( 'npm init error' , async t => {
306
- npm . localPrefix = t . testdir ( { } )
307
-
308
- const Init = t . mock ( '../../../lib/commands/init.js' , {
309
- ...mocks ,
310
- 'init-package-json' : ( dir , initFile , config , cb ) => cb (
248
+ const { npm } = await mockNpm ( t , {
249
+ initPackageJson : ( ...args ) => args [ 3 ] (
311
250
new Error ( 'Unknown Error' )
312
251
) ,
313
252
} )
314
- const init = new Init ( npm )
315
253
316
- process . chdir ( npm . localPrefix )
317
254
await t . rejects (
318
- init . exec ( [ ] ) ,
255
+ npm . exec ( 'init' , [ ] ) ,
319
256
/ U n k n o w n E r r o r / ,
320
257
'should throw error'
321
258
)
322
259
} )
323
260
324
- t . test ( 'workspaces' , t => {
325
- t . test ( 'no args' , async t => {
326
- t . teardown ( ( ) => {
327
- npm . _mockOutputs . length = 0
328
- } )
329
- npm . _mockOutputs . length = 0
330
- npm . localPrefix = t . testdir ( {
331
- 'package.json' : JSON . stringify ( {
332
- name : 'top-level' ,
333
- } ) ,
334
- } )
335
-
336
- const Init = t . mock ( '../../../lib/commands/init.js' , {
337
- ...mocks ,
338
- 'init-package-json' : ( dir , initFile , config , cb ) => {
339
- t . equal ( dir , resolve ( npm . localPrefix , 'a' ) , 'should use the ws path' )
340
- cb ( )
341
- } ,
342
- } )
343
- const init = new Init ( npm )
344
- await init . execWorkspaces ( [ ] , [ 'a' ] )
345
- t . matchSnapshot ( npm . _mockOutputs , 'should print helper info' )
346
- } )
347
-
348
- t . test ( 'post workspace-init reify' , async t => {
349
- const _consolelog = console . log
350
- console . log = ( ) => null
351
- t . teardown ( ( ) => {
352
- console . log = _consolelog
353
- npm . _mockOutputs . length = 0
354
- delete npm . flatOptions . workspacesUpdate
355
- } )
356
- npm . started = Date . now ( )
357
- npm . _mockOutputs . length = 0
358
- npm . flatOptions . workspacesUpdate = true
359
- npm . localPrefix = t . testdir ( {
360
- 'package.json' : JSON . stringify ( {
361
- name : 'top-level' ,
362
- } ) ,
363
- } )
364
-
365
- const Init = t . mock ( '../../../lib/commands/init.js' , {
366
- ...mocks ,
367
- 'init-package-json' : ( dir , initFile , config , cb ) => {
368
- t . equal ( dir , resolve ( npm . localPrefix , 'a' ) , 'should use the ws path' )
369
- return require ( 'init-package-json' ) ( dir , initFile , config , cb )
261
+ t . test ( 'workspaces' , async t => {
262
+ await t . test ( 'no args -- yes' , async t => {
263
+ const { npm, prefix, joinedOutput } = await mockNpm ( t , {
264
+ prefixDir : {
265
+ 'package.json' : JSON . stringify ( {
266
+ name : 'top-level' ,
267
+ } ) ,
370
268
} ,
269
+ config : { workspace : 'a' , yes : true } ,
270
+ noLog : true ,
371
271
} )
372
- const init = new Init ( npm )
373
- await init . execWorkspaces ( [ ] , [ 'a' ] )
374
- const output = npm . _mockOutputs . map ( arr => arr . map ( i => i . replace ( / [ 0 - 9 ] * m ? s $ / , '100ms' ) ) )
375
- t . matchSnapshot ( output , 'should print helper info' )
376
- const lockFilePath = resolve ( npm . localPrefix , 'package-lock.json' )
377
- const lockFile = fs . readFileSync ( lockFilePath , { encoding : 'utf8' } )
378
- t . matchSnapshot ( lockFile , 'should reify tree on init ws complete' )
379
- } )
380
272
381
- t . test ( 'no args, existing folder' , async t => {
382
- t . teardown ( ( ) => {
383
- npm . _mockOutputs . length = 0
384
- } )
385
- // init-package-json prints directly to console.log
386
- // this avoids poluting test output with those logs
387
- console . log = noop
273
+ await npm . exec ( 'init' , [ ] )
388
274
389
- npm . localPrefix = t . testdir ( {
390
- packages : {
391
- a : {
392
- 'package.json' : JSON . stringify ( {
393
- name : 'a' ,
394
- version : '1.0.0' ,
395
- } ) ,
396
- } ,
397
- } ,
398
- 'package.json' : JSON . stringify ( {
399
- name : 'top-level' ,
400
- workspaces : [ 'packages/a' ] ,
401
- } ) ,
402
- } )
275
+ const pkg = require ( resolve ( prefix , 'a/package.json' ) )
276
+ t . equal ( pkg . name , 'a' )
277
+ t . equal ( pkg . version , '1.0.0' )
278
+ t . equal ( pkg . license , 'ISC' )
403
279
404
- await init . execWorkspaces ( [ ] , [ 'packages/a' ] )
280
+ t . matchSnapshot ( joinedOutput ( ) , 'should print helper info' )
405
281
406
- t . matchSnapshot ( npm . _mockOutputs , 'should print helper info' )
282
+ const lock = require ( resolve ( prefix , 'package-lock.json' ) )
283
+ t . ok ( lock . packages . a )
407
284
} )
408
285
409
- t . test ( 'with arg but missing workspace folder' , async t => {
410
- t . teardown ( ( ) => {
411
- npm . _mockOutputs . length = 0
412
- } )
413
- // init-package-json prints directly to console.log
414
- // this avoids poluting test output with those logs
415
- console . log = noop
416
-
417
- npm . localPrefix = t . testdir ( {
418
- node_modules : {
419
- a : t . fixture ( 'symlink' , '../a' ) ,
420
- 'create-index' : {
421
- 'index.js' : `` ,
286
+ await t . test ( 'no args, existing folder' , async t => {
287
+ const { npm, prefix } = await mockNpm ( t , {
288
+ prefixDir : {
289
+ packages : {
290
+ a : {
291
+ 'package.json' : JSON . stringify ( {
292
+ name : 'a' ,
293
+ version : '2.0.0' ,
294
+ } ) ,
295
+ } ,
422
296
} ,
423
- } ,
424
- a : {
425
297
'package.json' : JSON . stringify ( {
426
- name : 'a ' ,
427
- version : '1.0.0' ,
298
+ name : 'top-level ' ,
299
+ workspaces : [ 'packages/a' ] ,
428
300
} ) ,
429
301
} ,
430
- 'package.json' : JSON . stringify ( {
431
- name : 'top-level' ,
432
- } ) ,
302
+ config : { workspace : 'packages/a' , yes : true } ,
303
+ noLog : true ,
433
304
} )
434
305
435
- await init . execWorkspaces ( [ ] , [ 'packages/a' ] )
306
+ await npm . exec ( 'init' , [ ] )
436
307
437
- t . matchSnapshot ( npm . _mockOutputs , 'should print helper info' )
308
+ const pkg = require ( resolve ( prefix , 'packages/a/package.json' ) )
309
+ t . equal ( pkg . name , 'a' )
310
+ t . equal ( pkg . version , '2.0.0' )
311
+ t . equal ( pkg . license , 'ISC' )
438
312
} )
439
313
440
- t . test ( 'fail parsing top-level package.json to set workspace' , async t => {
441
- // init-package-json prints directly to console.log
442
- // this avoids poluting test output with those logs
443
- console . log = noop
444
-
445
- npm . localPrefix = t . testdir ( {
446
- 'package.json' : JSON . stringify ( {
447
- name : 'top-level' ,
448
- } ) ,
449
- } )
450
-
451
- const Init = t . mock ( '../../../lib/commands/init.js' , {
452
- ...mocks ,
453
- '@npmcli/package-json' : {
314
+ await t . test ( 'fail parsing top-level package.json to set workspace' , async t => {
315
+ const { npm } = await mockNpm ( t , {
316
+ prefixDir : {
317
+ 'package.json' : JSON . stringify ( {
318
+ name : 'top-level' ,
319
+ } ) ,
320
+ } ,
321
+ packageJson : {
454
322
async load ( ) {
455
323
throw new Error ( 'ERR' )
456
324
} ,
457
325
} ,
326
+ config : { workspace : 'a' , yes : true } ,
327
+ noLog : true ,
458
328
} )
459
- const init = new Init ( npm )
460
329
461
330
await t . rejects (
462
- init . execWorkspaces ( [ ] , [ 'a' ] ) ,
331
+ npm . exec ( 'init' , [ ] ) ,
463
332
/ E R R / ,
464
333
'should exit with error'
465
334
)
466
335
} )
467
336
468
- t . test ( 'missing top-level package.json when settting workspace' , async t => {
469
- // init-package-json prints directly to console.log
470
- // this avoids poluting test output with those logs
471
- console . log = noop
472
-
473
- npm . localPrefix = t . testdir ( { } )
474
-
475
- const Init = require ( '../../../lib/commands/init.js' )
476
- const init = new Init ( npm )
337
+ await t . test ( 'missing top-level package.json when settting workspace' , async t => {
338
+ const { npm } = await mockNpm ( t , {
339
+ config : { workspace : 'a' } ,
340
+ } )
477
341
478
342
await t . rejects (
479
- init . execWorkspaces ( [ ] , [ 'a' ] ) ,
343
+ npm . exec ( 'init' , [ ] ) ,
480
344
{ code : 'ENOENT' } ,
481
345
'should exit with missing package.json file error'
482
346
)
483
347
} )
484
348
485
- t . test ( 'using args' , async t => {
486
- npm . localPrefix = t . testdir ( {
487
- b : {
349
+ await t . test ( 'using args - no package.json' , async t => {
350
+ const { npm, prefix } = await mockNpm ( t , {
351
+ prefixDir : {
352
+ b : {
353
+ 'package.json' : JSON . stringify ( {
354
+ name : 'b' ,
355
+ } ) ,
356
+ } ,
488
357
'package.json' : JSON . stringify ( {
489
- name : 'b' ,
358
+ name : 'top-level' ,
359
+ workspaces : [ 'b' ] ,
490
360
} ) ,
491
361
} ,
492
- 'package.json' : JSON . stringify ( {
493
- name : 'top-level' ,
494
- workspaces : [ 'b' ] ,
495
- } ) ,
362
+ // Important: exec did not write a package.json here
363
+ libnpmexec : async ( ) => { } ,
364
+ config : { workspace : 'a' , yes : true } ,
496
365
} )
497
366
498
- const Init = t . mock ( '../../../lib/commands/init.js' , {
499
- ...mocks ,
500
- libnpmexec : ( { args, path } ) => {
501
- t . same (
502
- args ,
503
- [ 'create-react-app@*' ] ,
504
- 'should npx with listed packages'
505
- )
506
- t . same (
507
- path ,
508
- resolve ( npm . localPrefix , 'a' ) ,
509
- 'should use workspace path'
510
- )
511
- fs . writeFileSync (
512
- resolve ( npm . localPrefix , 'a/package.json' ) ,
513
- JSON . stringify ( { name : 'a' } )
514
- )
367
+ await npm . exec ( 'init' , [ 'react-app' ] )
368
+
369
+ const pkg = require ( resolve ( prefix , 'package.json' ) )
370
+ t . strictSame ( pkg . workspaces , [ 'b' ] , 'pkg workspaces did not get updated' )
371
+ } )
372
+
373
+ await t . test ( 'init template - bad package.json' , async t => {
374
+ const { npm, prefix } = await mockNpm ( t , {
375
+ prefixDir : {
376
+ b : {
377
+ 'package.json' : JSON . stringify ( {
378
+ name : 'b' ,
379
+ } ) ,
380
+ } ,
381
+ 'package.json' : JSON . stringify ( {
382
+ name : 'top-level' ,
383
+ workspaces : [ 'b' ] ,
384
+ } ) ,
515
385
} ,
386
+ initPackageJson : async ( ...args ) => {
387
+ const [ dir ] = args
388
+ if ( dir . endsWith ( 'c' ) ) {
389
+ await fs . writeFile ( resolve ( dir , 'package.json' ) , JSON . stringify ( {
390
+ name : basename ( dir ) ,
391
+ } ) , 'utf-8' )
392
+ }
393
+ args [ 3 ] ( )
394
+ } ,
395
+ config : { yes : true , workspace : [ 'a' , 'c' ] } ,
516
396
} )
517
397
518
- const init = new Init ( npm )
519
- await init . execWorkspaces ( [ 'react-app' ] , [ 'a' ] )
520
- } )
398
+ await npm . exec ( 'init' , [ ] )
521
399
522
- t . end ( )
523
- } )
400
+ const pkg = require ( resolve ( prefix , 'package.json' ) )
401
+ t . strictSame ( pkg . workspaces , [ 'b' , 'c' ] )
524
402
525
- t . test ( 'npm init workspces with root' , async t => {
526
- t . teardown ( ( ) => {
527
- npm . _mockOutputs . length = 0
403
+ const lock = require ( resolve ( prefix , 'package-lock.json' ) )
404
+ t . notOk ( lock . packages . a )
528
405
} )
529
- npm . localPrefix = t . testdir ( { } )
530
- npm . flatOptions . includeWorkspaceRoot = true
531
406
532
- // init-package-json prints directly to console.log
533
- // this avoids poluting test output with those logs
534
- console . log = noop
407
+ t . test ( 'workspace root' , async t => {
408
+ const { npm } = await mockNpm ( t , {
409
+ config : { workspace : 'packages/a' , 'include-workspace-root' : true , yes : true } ,
410
+ noLog : true ,
411
+ } )
535
412
536
- process . chdir ( npm . localPrefix )
537
- await init . execWorkspaces ( [ ] , [ 'packages/a' ] )
538
- const pkg = require ( resolve ( npm . localPrefix , 'package.json' ) )
539
- t . equal ( pkg . version , '1.0.0' )
540
- t . equal ( pkg . license , 'ISC' )
541
- t . matchSnapshot ( npm . _mockOutputs , 'does not print helper info' )
413
+ await npm . exec ( 'init' , [ ] )
414
+
415
+ const pkg = require ( resolve ( npm . localPrefix , 'package.json' ) )
416
+ t . equal ( pkg . version , '1.0.0' )
417
+ t . equal ( pkg . license , 'ISC' )
418
+ t . strictSame ( pkg . workspaces , [ 'packages/a' ] )
419
+
420
+ const ws = require ( resolve ( npm . localPrefix , 'packages/a/package.json' ) )
421
+ t . equal ( ws . version , '1.0.0' )
422
+ t . equal ( ws . license , 'ISC' )
423
+ } )
542
424
} )
0 commit comments