@@ -50,6 +50,7 @@ const {
50
50
validateFunction,
51
51
validateObject,
52
52
validateInteger,
53
+ validateString,
53
54
} = require ( 'internal/validators' ) ;
54
55
const { getInspectPort, isUsingInspector, isInspectorMessage } = require ( 'internal/util/inspector' ) ;
55
56
const { isRegExp } = require ( 'internal/util/types' ) ;
@@ -87,8 +88,7 @@ const kCanceledTests = new SafeSet()
87
88
88
89
let kResistStopPropagation ;
89
90
90
- function createTestFileList ( patterns ) {
91
- const cwd = process . cwd ( ) ;
91
+ function createTestFileList ( patterns , cwd ) {
92
92
const hasUserSuppliedPattern = patterns != null ;
93
93
if ( ! patterns || patterns . length === 0 ) {
94
94
patterns = [ kDefaultPattern ] ;
@@ -110,7 +110,7 @@ function createTestFileList(patterns) {
110
110
111
111
function filterExecArgv ( arg , i , arr ) {
112
112
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 } =` ) ) ;
114
114
}
115
115
116
116
function getRunArgs ( path , { forceExit, inspectPort, testNamePatterns, testSkipPatterns, only } ) {
@@ -167,7 +167,7 @@ class FileTest extends Test {
167
167
if ( firstSpaceIndex === - 1 ) return false ;
168
168
const secondSpaceIndex = StringPrototypeIndexOf ( comment , ' ' , firstSpaceIndex + 1 ) ;
169
169
return secondSpaceIndex === - 1 &&
170
- ArrayPrototypeIncludes ( kDiagnosticsFilterArgs , StringPrototypeSlice ( comment , 0 , firstSpaceIndex ) ) ;
170
+ ArrayPrototypeIncludes ( kDiagnosticsFilterArgs , StringPrototypeSlice ( comment , 0 , firstSpaceIndex ) ) ;
171
171
}
172
172
#handleReportItem( item ) {
173
173
const isTopLevel = item . data . nesting === 0 ;
@@ -417,16 +417,19 @@ function watchFiles(testFiles, opts) {
417
417
const watcherMode = opts . hasFiles ? 'filter' : 'all' ;
418
418
const watcher = new FilesWatcher ( { __proto__ : null , debounce : 200 , mode : watcherMode , signal : opts . signal } ) ;
419
419
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 ) ;
421
421
}
422
422
const filesWatcher = { __proto__ : null , watcher, runningProcesses, runningSubtests } ;
423
423
opts . root . harness . watching = true ;
424
424
// Watch for changes in current filtered files
425
425
watcher . on ( 'changed' , ( { owners, eventType } ) => {
426
426
if ( ! opts . hasFiles && ( eventType === 'rename' || eventType === 'change' ) ) {
427
- const updatedTestFiles = createTestFileList ( opts . globPatterns ) ;
427
+ const updatedTestFiles = createTestFileList ( opts . globPatterns , opts . watchedDir ) ;
428
428
429
429
const newFileName = ArrayPrototypeFind ( updatedTestFiles , ( x ) => ! ArrayPrototypeIncludes ( testFiles , x ) ) ;
430
+ const previousFileName = ArrayPrototypeFind ( testFiles , ( x ) => ! ArrayPrototypeIncludes ( updatedTestFiles , x ) ) ;
431
+
432
+ testFiles = updatedTestFiles ;
430
433
431
434
// When file renamed (created / deleted) we need to update the watcher
432
435
if ( newFileName ) {
@@ -437,8 +440,6 @@ function watchFiles(testFiles, opts) {
437
440
if ( ! newFileName && previousFileName ) {
438
441
return ; // Avoid rerunning files when file deleted
439
442
}
440
-
441
- testFiles = updatedTestFiles ;
442
443
}
443
444
444
445
watcher . unfilterFilesOwnedBy ( owners ) ;
@@ -491,6 +492,7 @@ function run(options = kEmptyObject) {
491
492
setup,
492
493
only,
493
494
globPatterns,
495
+ cwd = process . cwd ( ) ,
494
496
} = options ;
495
497
496
498
if ( files != null ) {
@@ -515,6 +517,10 @@ function run(options = kEmptyObject) {
515
517
validateArray ( globPatterns , 'options.globPatterns' ) ;
516
518
}
517
519
520
+ if ( cwd != null ) {
521
+ validateString ( cwd , 'options.cwd' ) ;
522
+ }
523
+
518
524
if ( globPatterns ?. length > 0 && files ?. length > 0 ) {
519
525
throw new ERR_INVALID_ARG_VALUE (
520
526
'options.globPatterns' , globPatterns , 'is not supported when specifying \'options.files\'' ,
@@ -584,7 +590,7 @@ function run(options = kEmptyObject) {
584
590
root . postRun ( ) ;
585
591
return root . reporter ;
586
592
}
587
- let testFiles = files ?? createTestFileList ( globPatterns ) ;
593
+ let testFiles = files ?? createTestFileList ( globPatterns , cwd ) ;
588
594
589
595
if ( shard ) {
590
596
testFiles = ArrayPrototypeFilter ( testFiles , ( _ , index ) => index % shard . total === shard . index - 1 ) ;
@@ -604,6 +610,7 @@ function run(options = kEmptyObject) {
604
610
globPatterns,
605
611
only,
606
612
forceExit,
613
+ watchedDir : cwd ,
607
614
} ;
608
615
if ( watch ) {
609
616
filesWatcher = watchFiles ( testFiles , opts ) ;
0 commit comments