@@ -335,15 +335,13 @@ class FileTest extends Test {
335335 }
336336}
337337
338- const runningProcesses = new SafeMap ( ) ;
339- const runningSubtests = new SafeMap ( ) ;
340-
341338function runTestFile ( path , root , inspectPort , filesWatcher , testNamePatterns ) {
339+ const watchMode = filesWatcher != null ;
342340 const subtest = root . createSubtest ( FileTest , path , async ( t ) => {
343341 const args = getRunArgs ( { path, inspectPort, testNamePatterns } ) ;
344342 const stdio = [ 'pipe' , 'pipe' , 'pipe' ] ;
345343 const env = { ...process . env , NODE_TEST_CONTEXT : 'child-v8' } ;
346- if ( filesWatcher ) {
344+ if ( watchMode ) {
347345 stdio . push ( 'ipc' ) ;
348346 env . WATCH_REPORT_DEPENDENCIES = '1' ;
349347 }
@@ -352,11 +350,13 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
352350 }
353351
354352 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+ }
356357
357358 let err ;
358359
359- filesWatcher ?. watchChildProcessModules ( child , path ) ;
360360
361361 child . on ( 'error' , ( error ) => {
362362 err = error ;
@@ -388,8 +388,14 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
388388 finished ( child . stdout , { signal : t . signal } ) ,
389389 ] ) ;
390390
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+
393399 if ( code !== 0 || signal !== null ) {
394400 if ( ! err ) {
395401 const failureType = subtest . failedSubtests ? kSubtestsFailed : kTestCodeFailure ;
@@ -410,9 +416,13 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
410416}
411417
412418function 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 ) ;
416426 PromisePrototypeThen ( SafePromiseAllReturnVoid ( testFiles , async ( file ) => {
417427 if ( ! owners . has ( file ) ) {
418428 return ;
@@ -433,6 +443,7 @@ function watchFiles(testFiles, root, inspectPort, signal, testNamePatterns) {
433443 } ) ) ;
434444 } ) ;
435445 signal ?. addEventListener ( 'abort' , ( ) => root . postRun ( ) , { __proto__ : null , once : true } ) ;
446+
436447 return filesWatcher ;
437448}
438449
@@ -482,7 +493,7 @@ function run(options) {
482493 root . harness . bootstrapComplete = true ;
483494 return SafePromiseAllSettledReturnVoid ( testFiles , ( path ) => {
484495 const subtest = runTestFile ( path , root , inspectPort , filesWatcher , testNamePatterns ) ;
485- runningSubtests . set ( path , subtest ) ;
496+ filesWatcher ?. runningSubtests . set ( path , subtest ) ;
486497 return subtest ;
487498 } ) ;
488499 } ;
0 commit comments