@@ -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' ) ;
@@ -425,6 +426,9 @@ function watchFiles(testFiles, opts) {
425426 const updatedTestFiles = createTestFileList ( opts . globPatterns , opts . watchedDir ) ;
426427
427428 const newFileName = ArrayPrototypeFind ( updatedTestFiles , ( x ) => ! ArrayPrototypeIncludes ( testFiles , x ) ) ;
429+ const previousFileName = ArrayPrototypeFind ( testFiles , ( x ) => ! ArrayPrototypeIncludes ( updatedTestFiles , x ) ) ;
430+
431+ testFiles = updatedTestFiles ;
428432
429433 // When file renamed (created / deleted) we need to update the watcher
430434 if ( newFileName ) {
@@ -435,8 +439,6 @@ function watchFiles(testFiles, opts) {
435439 if ( ! newFileName && previousFileName ) {
436440 return ; // Avoid rerunning files when file deleted
437441 }
438-
439- testFiles = updatedTestFiles ;
440442 }
441443
442444 watcher . unfilterFilesOwnedBy ( owners ) ;
@@ -489,7 +491,7 @@ function run(options = kEmptyObject) {
489491 setup,
490492 only,
491493 globPatterns,
492- cwd,
494+ cwd = process . cwd ( ) ,
493495 } = options ;
494496
495497 if ( files != null ) {
@@ -514,6 +516,10 @@ function run(options = kEmptyObject) {
514516 validateArray ( globPatterns , 'options.globPatterns' ) ;
515517 }
516518
519+ if ( cwd != null ) {
520+ validateString ( cwd , 'options.cwd' ) ;
521+ }
522+
517523 if ( globPatterns ?. length > 0 && files ?. length > 0 ) {
518524 throw new ERR_INVALID_ARG_VALUE (
519525 'options.globPatterns' , globPatterns , 'is not supported when specifying \'options.files\'' ,
@@ -568,16 +574,14 @@ function run(options = kEmptyObject) {
568574 } ) ;
569575 }
570576
571- const runCwd = cwd ?? process . cwd ( ) ;
572-
573577 const root = createTestTree ( { __proto__ : null , concurrency, timeout, signal } ) ;
574578
575579 if ( process . env . NODE_TEST_CONTEXT !== undefined ) {
576580 process . emitWarning ( 'node:test run() is being called recursively within a test file. skipping running files.' ) ;
577581 root . postRun ( ) ;
578582 return root . reporter ;
579583 }
580- let testFiles = files ?? createTestFileList ( globPatterns , runCwd ) ;
584+ let testFiles = files ?? createTestFileList ( globPatterns , cwd ) ;
581585
582586 if ( shard ) {
583587 testFiles = ArrayPrototypeFilter ( testFiles , ( _ , index ) => index % shard . total === shard . index - 1 ) ;
@@ -597,7 +601,7 @@ function run(options = kEmptyObject) {
597601 globPatterns,
598602 only,
599603 forceExit,
600- watchedDir : runCwd ,
604+ watchedDir : cwd ,
601605 } ;
602606 if ( watch ) {
603607 filesWatcher = watchFiles ( testFiles , opts ) ;
0 commit comments