@@ -33,7 +33,7 @@ const {
3333
3434const { spawn } = require ( 'child_process' ) ;
3535const { finished } = require ( 'internal/streams/end-of-stream' ) ;
36- const { resolve } = require ( 'path' ) ;
36+ const { resolve, sep , isAbsolute } = require ( 'path' ) ;
3737const { DefaultDeserializer, DefaultSerializer } = require ( 'v8' ) ;
3838const { getOptionValue } = require ( 'internal/options' ) ;
3939const { Interface } = require ( 'internal/readline/interface' ) ;
@@ -56,14 +56,14 @@ const {
5656 validateObject,
5757 validateOneOf,
5858 validateInteger,
59+ validateString,
5960 validateStringArray,
6061} = require ( 'internal/validators' ) ;
6162const { getInspectPort, isUsingInspector, isInspectorMessage } = require ( 'internal/util/inspector' ) ;
6263const { isRegExp } = require ( 'internal/util/types' ) ;
6364const { pathToFileURL } = require ( 'internal/url' ) ;
6465const {
6566 createDeferredPromise,
66- getCWDURL,
6767 kEmptyObject,
6868} = require ( 'internal/util' ) ;
6969const { kEmitMessage } = require ( 'internal/test_runner/tests_stream' ) ;
@@ -137,7 +137,8 @@ function getRunArgs(path, { forceExit,
137137 testSkipPatterns,
138138 only,
139139 argv : suppliedArgs ,
140- execArgv } ) {
140+ execArgv,
141+ cwd } ) {
141142 const argv = ArrayPrototypeFilter ( process . execArgv , filterExecArgv ) ;
142143 if ( forceExit === true ) {
143144 ArrayPrototypePush ( argv , '--test-force-exit' ) ;
@@ -494,7 +495,8 @@ function watchFiles(testFiles, opts) {
494495 // When file renamed (created / deleted) we need to update the watcher
495496 if ( newFileName ) {
496497 owners = new SafeSet ( ) . add ( newFileName ) ;
497- watcher . filterFile ( resolve ( newFileName ) , owners ) ;
498+ const resolveFileName = isAbsolute ( newFileName ) ? newFileName : resolve ( opts . cwd , newFileName ) ;
499+ watcher . filterFile ( resolveFileName , owners ) ;
498500 }
499501
500502 if ( ! newFileName && previousFileName ) {
@@ -562,6 +564,7 @@ function run(options = kEmptyObject) {
562564 functionCoverage = 0 ,
563565 execArgv = [ ] ,
564566 argv = [ ] ,
567+ cwd = process . cwd ( ) ,
565568 } = options ;
566569
567570 if ( files != null ) {
@@ -586,6 +589,8 @@ function run(options = kEmptyObject) {
586589 validateArray ( globPatterns , 'options.globPatterns' ) ;
587590 }
588591
592+ validateString ( cwd , 'options.cwd' ) ;
593+
589594 if ( globPatterns ?. length > 0 && files ?. length > 0 ) {
590595 throw new ERR_INVALID_ARG_VALUE (
591596 'options.globPatterns' , globPatterns , 'is not supported when specifying \'options.files\'' ,
@@ -673,12 +678,9 @@ function run(options = kEmptyObject) {
673678 lineCoverage : lineCoverage ,
674679 branchCoverage : branchCoverage ,
675680 functionCoverage : functionCoverage ,
681+ cwd,
676682 } ;
677683 const root = createTestTree ( rootTestOptions , globalOptions ) ;
678-
679- // This const should be replaced by a run option in the future.
680- const cwd = process . cwd ( ) ;
681-
682684 let testFiles = files ?? createTestFileList ( globPatterns , cwd ) ;
683685
684686 if ( shard ) {
@@ -731,7 +733,8 @@ function run(options = kEmptyObject) {
731733 } ;
732734 } else if ( isolation === 'none' ) {
733735 if ( watch ) {
734- filesWatcher = watchFiles ( testFiles , opts ) ;
736+ const absoluteTestFiles = ArrayPrototypeMap ( testFiles , ( file ) => ( isAbsolute ( file ) ? file : resolve ( cwd , file ) ) ) ;
737+ filesWatcher = watchFiles ( absoluteTestFiles , opts ) ;
735738 runFiles = async ( ) => {
736739 root . harness . bootstrapPromise = null ;
737740 root . harness . buildPromise = null ;
@@ -744,7 +747,7 @@ function run(options = kEmptyObject) {
744747 const { promise, resolve : finishBootstrap } = createDeferredPromise ( ) ;
745748
746749 await root . runInAsyncScope ( async ( ) => {
747- const parentURL = getCWDURL ( ) . href ;
750+ const parentURL = pathToFileURL ( cwd + sep ) . href ;
748751 const cascadedLoader = esmLoader . getOrInitializeCascadedLoader ( ) ;
749752 let topLevelTestCount = 0 ;
750753
@@ -757,7 +760,7 @@ function run(options = kEmptyObject) {
757760
758761 for ( let i = 0 ; i < testFiles . length ; ++ i ) {
759762 const testFile = testFiles [ i ] ;
760- const fileURL = pathToFileURL ( testFile ) ;
763+ const fileURL = pathToFileURL ( resolve ( cwd , testFile ) ) ;
761764 const parent = i === 0 ? undefined : parentURL ;
762765 let threw = false ;
763766 let importError ;
0 commit comments