@@ -335,15 +335,13 @@ class FileTest extends Test {
335
335
}
336
336
}
337
337
338
- const runningProcesses = new SafeMap ( ) ;
339
- const runningSubtests = new SafeMap ( ) ;
340
-
341
338
function runTestFile ( path , root , inspectPort , filesWatcher , testNamePatterns ) {
339
+ const watchMode = filesWatcher != null ;
342
340
const subtest = root . createSubtest ( FileTest , path , async ( t ) => {
343
341
const args = getRunArgs ( { path, inspectPort, testNamePatterns } ) ;
344
342
const stdio = [ 'pipe' , 'pipe' , 'pipe' ] ;
345
343
const env = { ...process . env , NODE_TEST_CONTEXT : 'child-v8' } ;
346
- if ( filesWatcher ) {
344
+ if ( watchMode ) {
347
345
stdio . push ( 'ipc' ) ;
348
346
env . WATCH_REPORT_DEPENDENCIES = '1' ;
349
347
}
@@ -352,11 +350,13 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
352
350
}
353
351
354
352
const child = spawn ( process . execPath , args , { signal : t . signal , encoding : 'utf8' , env, stdio } ) ;
355
- runningProcesses . set ( path , child ) ;
353
+ if ( watchMode ) {
354
+ filesWatcher . runningProcesses . set ( path , child ) ;
355
+ filesWatcher . watcher . watchChildProcessModules ( child , path ) ;
356
+ }
356
357
357
358
let err ;
358
359
359
- filesWatcher ?. watchChildProcessModules ( child , path ) ;
360
360
361
361
child . on ( 'error' , ( error ) => {
362
362
err = error ;
@@ -388,8 +388,14 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
388
388
finished ( child . stdout , { signal : t . signal } ) ,
389
389
] ) ;
390
390
391
- runningProcesses . delete ( path ) ;
392
- runningSubtests . delete ( path ) ;
391
+ if ( watchMode ) {
392
+ filesWatcher . runningProcesses . delete ( path ) ;
393
+ filesWatcher . runningSubtests . delete ( path ) ;
394
+ if ( filesWatcher . runningSubtests . size === 0 ) {
395
+ root . reporter [ kEmitMessage ] ( 'test:watch:drained' ) ;
396
+ }
397
+ }
398
+
393
399
if ( code !== 0 || signal !== null ) {
394
400
if ( ! err ) {
395
401
const failureType = subtest . failedSubtests ? kSubtestsFailed : kTestCodeFailure ;
@@ -410,9 +416,13 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
410
416
}
411
417
412
418
function watchFiles ( testFiles , root , inspectPort , signal , testNamePatterns ) {
413
- const filesWatcher = new FilesWatcher ( { throttle : 500 , mode : 'filter' , signal } ) ;
414
- filesWatcher . on ( 'changed' , ( { owners } ) => {
415
- filesWatcher . unfilterFilesOwnedBy ( owners ) ;
419
+ const runningProcesses = new SafeMap ( ) ;
420
+ const runningSubtests = new SafeMap ( ) ;
421
+ const watcher = new FilesWatcher ( { throttle : 500 , mode : 'filter' , signal } ) ;
422
+ const filesWatcher = { __proto__ : null , watcher, runningProcesses, runningSubtests } ;
423
+
424
+ watcher . on ( 'changed' , ( { owners } ) => {
425
+ watcher . unfilterFilesOwnedBy ( owners ) ;
416
426
PromisePrototypeThen ( SafePromiseAllReturnVoid ( testFiles , async ( file ) => {
417
427
if ( ! owners . has ( file ) ) {
418
428
return ;
@@ -433,6 +443,7 @@ function watchFiles(testFiles, root, inspectPort, signal, testNamePatterns) {
433
443
} ) ) ;
434
444
} ) ;
435
445
signal ?. addEventListener ( 'abort' , ( ) => root . postRun ( ) , { __proto__ : null , once : true } ) ;
446
+
436
447
return filesWatcher ;
437
448
}
438
449
@@ -482,7 +493,7 @@ function run(options) {
482
493
root . harness . bootstrapComplete = true ;
483
494
return SafePromiseAllSettledReturnVoid ( testFiles , ( path ) => {
484
495
const subtest = runTestFile ( path , root , inspectPort , filesWatcher , testNamePatterns ) ;
485
- runningSubtests . set ( path , subtest ) ;
496
+ filesWatcher ?. runningSubtests . set ( path , subtest ) ;
486
497
return subtest ;
487
498
} ) ;
488
499
} ;
0 commit comments