Skip to content

Commit

Permalink
test_runner: disallow array in run options
Browse files Browse the repository at this point in the history
PR-URL: #49935
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Moshe Atlow <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
rluvaton authored Oct 6, 2023
1 parent c5de3b4 commit 527589b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
7 changes: 3 additions & 4 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,9 @@ function watchFiles(testFiles, opts) {
return filesWatcher;
}

function run(options) {
if (options === null || typeof options !== 'object') {
options = kEmptyObject;
}
function run(options = kEmptyObject) {
validateObject(options, 'options');

let { testNamePatterns, shard } = options;
const { concurrency, timeout, signal, files, inspectPort, watch, setup, only } = options;

Expand Down
24 changes: 16 additions & 8 deletions test/parallel/test-runner-run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import assert from 'node:assert';
const testFixtures = fixtures.path('test-runner');

describe('require(\'node:test\').run', { concurrency: true }, () => {

it('should run with no tests', async () => {
const stream = run({ files: [] });
stream.on('test:fail', common.mustNotCall());
Expand Down Expand Up @@ -65,13 +64,6 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
for await (const _ of stream);
});

it('should validate files', async () => {
[Symbol(), {}, () => {}, 0, 1, 0n, 1n, '', '1', Promise.resolve([]), true, false]
.forEach((files) => assert.throws(() => run({ files }), {
code: 'ERR_INVALID_ARG_TYPE'
}));
});

it('should be piped with dot', async () => {
const result = await run({
files: [join(testFixtures, 'default-behavior/test/random.cjs')]
Expand Down Expand Up @@ -437,4 +429,20 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
assert.deepStrictEqual(executedTestFiles.sort(), [...shardsTestsFiles].sort());
});
});

describe('validation', () => {
it('should only allow array in options.files', async () => {
[Symbol(), {}, () => {}, 0, 1, 0n, 1n, '', '1', Promise.resolve([]), true, false]
.forEach((files) => assert.throws(() => run({ files }), {
code: 'ERR_INVALID_ARG_TYPE'
}));
});

it('should only allow object as options', () => {
[Symbol(), [], () => {}, 0, 1, 0n, 1n, '', '1', true, false]
.forEach((options) => assert.throws(() => run(options), {
code: 'ERR_INVALID_ARG_TYPE'
}));
});
});
});

0 comments on commit 527589b

Please sign in to comment.