Skip to content

Commit f848750

Browse files
committed
test_runner: add cwd option and update documentation
1 parent 2eb2916 commit f848750

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
lines changed

doc/api/test.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,9 @@ added:
12391239
- v18.9.0
12401240
- v16.19.0
12411241
changes:
1242+
- version: REPLACEME
1243+
pr-url: https://github.com/nodejs/node/pull/54225
1244+
description: Added the `cwd` option.
12421245
- version: v22.6.0
12431246
pr-url: https://github.com/nodejs/node/pull/53866
12441247
description: Added the `globPatterns` option.
@@ -1263,6 +1266,9 @@ changes:
12631266
parallel.
12641267
If `false`, it would only run one test file at a time.
12651268
**Default:** `false`.
1269+
* `cwd`: {string} Specifies the current working directory (cwd) to be used by the test runner.
1270+
The cwd serves as the base path for resolving files according to the [test runner execution model][].
1271+
**Default:** `process.cwd()`.
12661272
* `files`: {Array} An array containing the list of files to run.
12671273
**Default** matching files from [test runner execution model][].
12681274
* `forceExit`: {boolean} Configures the test runner to exit the process once

lib/internal/test_runner/runner.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const {
5050
validateFunction,
5151
validateObject,
5252
validateInteger,
53+
validateString,
5354
} = require('internal/validators');
5455
const { getInspectPort, isUsingInspector, isInspectorMessage } = require('internal/util/inspector');
5556
const { 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);

test/parallel/test-runner-run-watch.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ const test = require('node:test');
2525
require('./dependency.js');
2626
import('./dependency.mjs');
2727
import('data:text/javascript,');
28-
test('test has ran');`,
29-
'test-without-dep.js': `
30-
const test = require('node:test');
3128
test('test has ran');`,
3229
};
3330

@@ -227,10 +224,14 @@ describe('test runner watch mode', () => {
227224
});
228225

229226
it('should run new tests when a new file is created in a different cwd', async () => {
227+
const newTestWithoutDep = `
228+
const test = require('node:test');
229+
test('test without dep has ran');
230+
`;
230231
const differentCwd = mkdtempSync(`${tmpdir.path}/different-cwd`);
231232
const testFileName = 'test-without-dep.js';
232233
const newTestFilePath = join(differentCwd, testFileName);
233-
writeFileSync(newTestFilePath, fixtureContent[testFileName]);
234+
writeFileSync(newTestFilePath, newTestWithoutDep);
234235
const differentCwdTmpPath = basename(differentCwd);
235236

236237
await testWatch({

test/parallel/test-runner-run.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,13 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
481481
});
482482
});
483483

484+
it('should only allow a string in options.cwd', async () => {
485+
[Symbol(), {}, [], () => {}, 0, 1, 0n, 1n, true, false]
486+
.forEach((cwd) => assert.throws(() => run({ cwd }), {
487+
code: 'ERR_INVALID_ARG_TYPE'
488+
}));
489+
});
490+
484491
it('should only allow object as options', () => {
485492
[Symbol(), [], () => {}, 0, 1, 0n, 1n, '', '1', true, false]
486493
.forEach((options) => assert.throws(() => run(options), {

test/parallel/test-runner-watch-mode.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ const test = require('node:test');
2727
require('./dependency.js');
2828
import('./dependency.mjs');
2929
import('data:text/javascript,');
30-
test('test has ran');`,
31-
'test-without-dep.js': `
32-
const test = require('node:test');
3330
test('test has ran');`,
3431
};
3532

@@ -198,10 +195,14 @@ describe('test runner watch mode', () => {
198195
});
199196

200197
it('should run new tests when a new file is created in a different cwd', async () => {
198+
const newTestWithoutDep = `
199+
const test = require('node:test');
200+
test('test without dep has ran');
201+
`;
201202
const differentCwd = mkdtempSync(`${tmpdir.path}/different-cwd`);
202203
const testFileName = 'test-without-dep.js';
203204
const newTestFilePath = join(differentCwd, testFileName);
204-
writeFileSync(newTestFilePath, fixtureContent[testFileName]);
205+
writeFileSync(newTestFilePath, newTestWithoutDep);
205206
const differentCwdTmpPath = basename(differentCwd);
206207

207208
await testWatch({

0 commit comments

Comments
 (0)