@@ -50,6 +50,7 @@ const {
5050 validateFunction,
5151 validateObject,
5252 validateInteger,
53+ validateString,
5354} = require ( 'internal/validators' ) ;
5455const { getInspectPort, isUsingInspector, isInspectorMessage } = require ( 'internal/util/inspector' ) ;
5556const { isRegExp } = require ( 'internal/util/types' ) ;
@@ -87,8 +88,7 @@ const kCanceledTests = new SafeSet()
8788
8889let kResistStopPropagation ;
8990
90- function createTestFileList ( patterns ) {
91- const cwd = process . cwd ( ) ;
91+ function createTestFileList ( patterns , cwd ) {
9292 const hasUserSuppliedPattern = patterns != null ;
9393 if ( ! patterns || patterns . length === 0 ) {
9494 patterns = [ kDefaultPattern ] ;
@@ -110,7 +110,7 @@ function createTestFileList(patterns) {
110110
111111function filterExecArgv ( arg , i , arr ) {
112112 return ! ArrayPrototypeIncludes ( kFilterArgs , arg ) &&
113- ! ArrayPrototypeSome ( kFilterArgValues , ( p ) => arg === p || ( i > 0 && arr [ i - 1 ] === p ) || StringPrototypeStartsWith ( arg , `${ p } =` ) ) ;
113+ ! ArrayPrototypeSome ( kFilterArgValues , ( p ) => arg === p || ( i > 0 && arr [ i - 1 ] === p ) || StringPrototypeStartsWith ( arg , `${ p } =` ) ) ;
114114}
115115
116116function getRunArgs ( path , { forceExit, inspectPort, testNamePatterns, testSkipPatterns, only } ) {
@@ -167,7 +167,7 @@ class FileTest extends Test {
167167 if ( firstSpaceIndex === - 1 ) return false ;
168168 const secondSpaceIndex = StringPrototypeIndexOf ( comment , ' ' , firstSpaceIndex + 1 ) ;
169169 return secondSpaceIndex === - 1 &&
170- ArrayPrototypeIncludes ( kDiagnosticsFilterArgs , StringPrototypeSlice ( comment , 0 , firstSpaceIndex ) ) ;
170+ ArrayPrototypeIncludes ( kDiagnosticsFilterArgs , StringPrototypeSlice ( comment , 0 , firstSpaceIndex ) ) ;
171171 }
172172 #handleReportItem( item ) {
173173 const isTopLevel = item . data . nesting === 0 ;
@@ -417,16 +417,19 @@ function watchFiles(testFiles, opts) {
417417 const watcherMode = opts . hasFiles ? 'filter' : 'all' ;
418418 const watcher = new FilesWatcher ( { __proto__ : null , debounce : 200 , mode : watcherMode , signal : opts . signal } ) ;
419419 if ( ! opts . hasFiles ) {
420- watcher . watchPath ( process . cwd ( ) ) ; // TODO: https://github.com/nodejs/node/issues/53867 before closing this MR
420+ watcher . watchPath ( opts . watchedDir ) ;
421421 }
422422 const filesWatcher = { __proto__ : null , watcher, runningProcesses, runningSubtests } ;
423423 opts . root . harness . watching = true ;
424424 // Watch for changes in current filtered files
425425 watcher . on ( 'changed' , ( { owners, eventType } ) => {
426426 if ( ! opts . hasFiles && ( eventType === 'rename' || eventType === 'change' ) ) {
427- const updatedTestFiles = createTestFileList ( opts . globPatterns ) ;
427+ const updatedTestFiles = createTestFileList ( opts . globPatterns , opts . watchedDir ) ;
428428
429429 const newFileName = ArrayPrototypeFind ( updatedTestFiles , ( x ) => ! ArrayPrototypeIncludes ( testFiles , x ) ) ;
430+ const previousFileName = ArrayPrototypeFind ( testFiles , ( x ) => ! ArrayPrototypeIncludes ( updatedTestFiles , x ) ) ;
431+
432+ testFiles = updatedTestFiles ;
430433
431434 // When file renamed (created / deleted) we need to update the watcher
432435 if ( newFileName ) {
@@ -437,8 +440,6 @@ function watchFiles(testFiles, opts) {
437440 if ( ! newFileName && previousFileName ) {
438441 return ; // Avoid rerunning files when file deleted
439442 }
440-
441- testFiles = updatedTestFiles ;
442443 }
443444
444445 watcher . unfilterFilesOwnedBy ( owners ) ;
@@ -491,6 +492,7 @@ function run(options = kEmptyObject) {
491492 setup,
492493 only,
493494 globPatterns,
495+ cwd = process . cwd ( ) ,
494496 } = options ;
495497
496498 if ( files != null ) {
@@ -515,6 +517,10 @@ function run(options = kEmptyObject) {
515517 validateArray ( globPatterns , 'options.globPatterns' ) ;
516518 }
517519
520+ if ( cwd != null ) {
521+ validateString ( cwd , 'options.cwd' ) ;
522+ }
523+
518524 if ( globPatterns ?. length > 0 && files ?. length > 0 ) {
519525 throw new ERR_INVALID_ARG_VALUE (
520526 'options.globPatterns' , globPatterns , 'is not supported when specifying \'options.files\'' ,
@@ -584,7 +590,7 @@ function run(options = kEmptyObject) {
584590 root . postRun ( ) ;
585591 return root . reporter ;
586592 }
587- let testFiles = files ?? createTestFileList ( globPatterns ) ;
593+ let testFiles = files ?? createTestFileList ( globPatterns , cwd ) ;
588594
589595 if ( shard ) {
590596 testFiles = ArrayPrototypeFilter ( testFiles , ( _ , index ) => index % shard . total === shard . index - 1 ) ;
@@ -604,6 +610,7 @@ function run(options = kEmptyObject) {
604610 globPatterns,
605611 only,
606612 forceExit,
613+ watchedDir : cwd ,
607614 } ;
608615 if ( watch ) {
609616 filesWatcher = watchFiles ( testFiles , opts ) ;
0 commit comments