Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup
Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina committed Jul 18, 2024
commit d7ad6ee483a907b099dcb8404395b6bb69b7edc2
2 changes: 2 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,8 @@ changes:
parallel.
If `false`, it would only run one test file at a time.
**Default:** `false`.
* `cwd` {string} The current working directory for the test runner.
**Default:** `process.cwd()`.
* `files`: {Array} An array containing the list of files to run.
**Default** matching files from [test runner execution model][].
* `forceExit`: {boolean} Configures the test runner to exit the process once
Expand Down
15 changes: 11 additions & 4 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const {
validateFunction,
validateObject,
validateInteger,
validateString,
} = require('internal/validators');
const { getInspectPort, isUsingInspector, isInspectorMessage } = require('internal/util/inspector');
const { isRegExp } = require('internal/util/types');
Expand Down Expand Up @@ -88,8 +89,7 @@ const kCanceledTests = new SafeSet()

let kResistStopPropagation;

function createTestFileList(patterns) {
const cwd = process.cwd();
function createTestFileList(cwd, patterns) {
const hasUserSuppliedPattern = patterns != null;
if (!patterns || patterns.length === 0) {
patterns = [kDefaultPattern];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would assign the default patterns in the run() validation. You can do that in this PR, otherwise, I will probably do it in a follow up.

Expand Down Expand Up @@ -421,7 +421,7 @@ function watchFiles(testFiles, opts) {

watcher.on('changed', ({ owners, eventType }) => {
if (!opts.hasFiles && eventType === 'rename') {
const updatedTestFiles = createTestFileList(opts.globPatterns);
const updatedTestFiles = createTestFileList(opts.cwd, opts.globPatterns);

const newFileName = ArrayPrototypeFind(updatedTestFiles, (x) => !ArrayPrototypeIncludes(testFiles, x));
const previousFileName = ArrayPrototypeFind(testFiles, (x) => !ArrayPrototypeIncludes(updatedTestFiles, x));
Expand Down Expand Up @@ -484,6 +484,7 @@ function run(options = kEmptyObject) {
watch,
setup,
only,
cwd,
globPatterns,
} = options;

Expand Down Expand Up @@ -530,6 +531,11 @@ function run(options = kEmptyObject) {
if (setup != null) {
validateFunction(setup, 'options.setup');
}

if (cwd != null) {
validateString(cwd, 'options.cwd');
}

if (testNamePatterns != null) {
if (!ArrayIsArray(testNamePatterns)) {
testNamePatterns = [testNamePatterns];
Expand Down Expand Up @@ -571,7 +577,7 @@ function run(options = kEmptyObject) {
root.postRun();
return root.reporter;
}
let testFiles = files ?? createTestFileList(globPatterns);
let testFiles = files ?? createTestFileList(cwd, globPatterns);

if (shard) {
testFiles = ArrayPrototypeFilter(testFiles, (_, index) => index % shard.total === shard.index - 1);
Expand All @@ -590,6 +596,7 @@ function run(options = kEmptyObject) {
globPatterns,
only,
forceExit,
cwd,
};
if (watch) {
filesWatcher = watchFiles(testFiles, opts);
Expand Down
11 changes: 6 additions & 5 deletions test/parallel/test-runner-run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import tmpdir from '../common/tmpdir.js';

const testFixtures = fixtures.path('test-runner');

// This is needed, because calling run() with no files will
// load files matching from cwd, which includes all content of test/
tmpdir.refresh();
process.chdir(tmpdir.path);

describe('require(\'node:test\').run', { concurrency: true }, () => {
it('should run with no tests', async () => {
const stream = run({ files: [] });
Expand Down Expand Up @@ -509,7 +504,9 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
});

it('should run with no files', async () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these tests be removed? Shouldn't these still work the same?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately they will now fail as they are trying to load all files in test that matches the given glob pattern. They were passing before due to the CLI checks that I removed.

tmpdir.refresh();
const stream = run({
cwd: tmpdir.path,
files: undefined
}).compose(tap);
stream.on('test:fail', common.mustNotCall());
Expand All @@ -520,7 +517,9 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
});

it('should run with no files and use spec reporter', async () => {
tmpdir.refresh();
const stream = run({
cwd: tmpdir.path,
files: undefined
}).compose(spec);
stream.on('test:fail', common.mustNotCall());
Expand All @@ -531,7 +530,9 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
});

it('should run with no files and use dot reporter', async () => {
tmpdir.refresh();
const stream = run({
cwd: tmpdir.path,
files: undefined
}).compose(dot);
stream.on('test:fail', common.mustNotCall());
Expand Down