@@ -30,7 +30,14 @@ function createTmpFile(content = 'console.log("running");', ext = '.js', basenam
30
30
}
31
31
32
32
async function runWriteSucceed ( {
33
- file, watchedFile, watchFlag = '--watch' , args = [ file ] , completed = 'Completed running' , restarts = 2 , options = { }
33
+ file,
34
+ watchedFile,
35
+ watchFlag = '--watch' ,
36
+ args = [ file ] ,
37
+ completed = 'Completed running' ,
38
+ restarts = 2 ,
39
+ options = { } ,
40
+ shouldFail = false
34
41
} ) {
35
42
const child = spawn ( execPath , [ watchFlag , '--no-warnings' , ...args ] , { encoding : 'utf8' , stdio : 'pipe' , ...options } ) ;
36
43
let completes = 0 ;
@@ -57,6 +64,10 @@ async function runWriteSucceed({
57
64
cancelRestarts = restart ( watchedFile ) ;
58
65
}
59
66
}
67
+
68
+ if ( ! shouldFail && data . startsWith ( 'Failed running' ) ) {
69
+ break ;
70
+ }
60
71
}
61
72
} finally {
62
73
child . kill ( ) ;
@@ -120,7 +131,12 @@ describe('watch mode', { concurrency: !process.env.TEST_PARALLEL, timeout: 60_00
120
131
121
132
it ( 'should watch changes to a failing file' , async ( ) => {
122
133
const file = createTmpFile ( 'throw new Error("fails");' ) ;
123
- const { stderr, stdout } = await runWriteSucceed ( { file, watchedFile : file , completed : 'Failed running' } ) ;
134
+ const { stderr, stdout } = await runWriteSucceed ( {
135
+ file,
136
+ watchedFile : file ,
137
+ completed : 'Failed running' ,
138
+ shouldFail : true
139
+ } ) ;
124
140
125
141
assert . match ( stderr , / E r r o r : f a i l s \r ? \n / ) ;
126
142
assert . deepStrictEqual ( stdout , [
@@ -159,7 +175,13 @@ describe('watch mode', { concurrency: !process.env.TEST_PARALLEL, timeout: 60_00
159
175
const file = path . join ( dir , 'non-existing.js' ) ;
160
176
const watchedFile = createTmpFile ( '' , '.js' , dir ) ;
161
177
const args = [ '--watch-path' , dir , file ] ;
162
- const { stderr, stdout } = await runWriteSucceed ( { file, watchedFile, args, completed : 'Failed running' } ) ;
178
+ const { stderr, stdout } = await runWriteSucceed ( {
179
+ file,
180
+ watchedFile,
181
+ args,
182
+ completed : 'Failed running' ,
183
+ shouldFail : true
184
+ } ) ;
163
185
164
186
assert . match ( stderr , / E r r o r : C a n n o t f i n d m o d u l e / g) ;
165
187
assert . deepStrictEqual ( stdout , [
@@ -177,7 +199,13 @@ describe('watch mode', { concurrency: !process.env.TEST_PARALLEL, timeout: 60_00
177
199
const file = path . join ( dir , 'non-existing.js' ) ;
178
200
const watchedFile = createTmpFile ( '' , '.js' , dir ) ;
179
201
const args = [ `--watch-path=${ dir } ` , file ] ;
180
- const { stderr, stdout } = await runWriteSucceed ( { file, watchedFile, args, completed : 'Failed running' } ) ;
202
+ const { stderr, stdout } = await runWriteSucceed ( {
203
+ file,
204
+ watchedFile,
205
+ args,
206
+ completed : 'Failed running' ,
207
+ shouldFail : true
208
+ } ) ;
181
209
182
210
assert . match ( stderr , / E r r o r : C a n n o t f i n d m o d u l e / g) ;
183
211
assert . deepStrictEqual ( stdout , [
@@ -372,4 +400,132 @@ console.log(values.random);
372
400
`Completed running ${ inspect ( file ) } ` ,
373
401
] ) ;
374
402
} ) ;
403
+
404
+ it ( 'should run when `--watch-path=./foo --require ./bar.js`' , {
405
+ skip : ! supportsRecursive ,
406
+ } , async ( ) => {
407
+ const projectDir = tmpdir . resolve ( 'project2' ) ;
408
+ mkdirSync ( projectDir ) ;
409
+
410
+ const dir = path . join ( projectDir , 'watched-dir' ) ;
411
+ mkdirSync ( dir ) ;
412
+
413
+ writeFileSync ( path . join ( projectDir , 'some.js' ) , 'console.log(\'hello\')' ) ;
414
+
415
+ const file = createTmpFile ( 'console.log(\'running\');' , '.js' , projectDir ) ;
416
+ const watchedFile = createTmpFile ( '' , '.js' , dir ) ;
417
+ const args = [ `--watch-path=${ dir } ` , '--require' , './some.js' , file ] ;
418
+ const { stdout, stderr } = await runWriteSucceed ( {
419
+ file, watchedFile, args, options : {
420
+ cwd : projectDir
421
+ }
422
+ } ) ;
423
+
424
+ assert . strictEqual ( stderr , '' ) ;
425
+ assert . deepStrictEqual ( stdout , [
426
+ 'hello' ,
427
+ 'running' ,
428
+ `Completed running ${ inspect ( file ) } ` ,
429
+ `Restarting ${ inspect ( file ) } ` ,
430
+ 'hello' ,
431
+ 'running' ,
432
+ `Completed running ${ inspect ( file ) } ` ,
433
+ ] ) ;
434
+ } ) ;
435
+
436
+ it ( 'should run when `--watch-path=./foo --require=./bar.js`' , {
437
+ skip : ! supportsRecursive ,
438
+ } , async ( ) => {
439
+ const projectDir = tmpdir . resolve ( 'project3' ) ;
440
+ mkdirSync ( projectDir ) ;
441
+
442
+ const dir = path . join ( projectDir , 'watched-dir' ) ;
443
+ mkdirSync ( dir ) ;
444
+
445
+ writeFileSync ( path . join ( projectDir , 'some.js' ) , "console.log('hello')" ) ;
446
+
447
+ const file = createTmpFile ( "console.log('running');" , '.js' , projectDir ) ;
448
+ const watchedFile = createTmpFile ( '' , '.js' , dir ) ;
449
+ const args = [ `--watch-path=${ dir } ` , '--require=./some.js' , file ] ;
450
+ const { stdout, stderr } = await runWriteSucceed ( {
451
+ file, watchedFile, args, options : {
452
+ cwd : projectDir
453
+ }
454
+ } ) ;
455
+
456
+ assert . strictEqual ( stderr , '' ) ;
457
+ assert . deepStrictEqual ( stdout , [
458
+ 'hello' ,
459
+ 'running' ,
460
+ `Completed running ${ inspect ( file ) } ` ,
461
+ `Restarting ${ inspect ( file ) } ` ,
462
+ 'hello' ,
463
+ 'running' ,
464
+ `Completed running ${ inspect ( file ) } ` ,
465
+ ] ) ;
466
+ } ) ;
467
+
468
+ it ( 'should run when `--watch-path ./foo --require ./bar.js`' , {
469
+ skip : ! supportsRecursive ,
470
+ } , async ( ) => {
471
+ const projectDir = tmpdir . resolve ( 'project5' ) ;
472
+ mkdirSync ( projectDir ) ;
473
+
474
+ const dir = path . join ( projectDir , 'watched-dir' ) ;
475
+ mkdirSync ( dir ) ;
476
+
477
+ writeFileSync ( path . join ( projectDir , 'some.js' ) , 'console.log(\'hello\')' ) ;
478
+
479
+ const file = createTmpFile ( 'console.log(\'running\');' , '.js' , projectDir ) ;
480
+ const watchedFile = createTmpFile ( '' , '.js' , dir ) ;
481
+ const args = [ '--watch-path' , `${ dir } ` , '--require' , './some.js' , file ] ;
482
+ const { stdout, stderr } = await runWriteSucceed ( {
483
+ file, watchedFile, args, options : {
484
+ cwd : projectDir
485
+ }
486
+ } ) ;
487
+
488
+ assert . strictEqual ( stderr , '' ) ;
489
+ assert . deepStrictEqual ( stdout , [
490
+ 'hello' ,
491
+ 'running' ,
492
+ `Completed running ${ inspect ( file ) } ` ,
493
+ `Restarting ${ inspect ( file ) } ` ,
494
+ 'hello' ,
495
+ 'running' ,
496
+ `Completed running ${ inspect ( file ) } ` ,
497
+ ] ) ;
498
+ } ) ;
499
+
500
+ it ( 'should run when `--watch-path=./foo --require=./bar.js`' , {
501
+ skip : ! supportsRecursive ,
502
+ } , async ( ) => {
503
+ const projectDir = tmpdir . resolve ( 'project6' ) ;
504
+ mkdirSync ( projectDir ) ;
505
+
506
+ const dir = path . join ( projectDir , 'watched-dir' ) ;
507
+ mkdirSync ( dir ) ;
508
+
509
+ writeFileSync ( path . join ( projectDir , 'some.js' ) , "console.log('hello')" ) ;
510
+
511
+ const file = createTmpFile ( "console.log('running');" , '.js' , projectDir ) ;
512
+ const watchedFile = createTmpFile ( '' , '.js' , dir ) ;
513
+ const args = [ '--watch-path' , `${ dir } ` , '--require=./some.js' , file ] ;
514
+ const { stdout, stderr } = await runWriteSucceed ( {
515
+ file, watchedFile, args, options : {
516
+ cwd : projectDir
517
+ }
518
+ } ) ;
519
+
520
+ assert . strictEqual ( stderr , '' ) ;
521
+ assert . deepStrictEqual ( stdout , [
522
+ 'hello' ,
523
+ 'running' ,
524
+ `Completed running ${ inspect ( file ) } ` ,
525
+ `Restarting ${ inspect ( file ) } ` ,
526
+ 'hello' ,
527
+ 'running' ,
528
+ `Completed running ${ inspect ( file ) } ` ,
529
+ ] ) ;
530
+ } ) ;
375
531
} ) ;
0 commit comments