From 527589b755a99a3692dc503ebfcb9496ed80f7b7 Mon Sep 17 00:00:00 2001 From: Raz Luvaton <16746759+rluvaton@users.noreply.github.com> Date: Fri, 6 Oct 2023 23:21:15 +0300 Subject: [PATCH] test_runner: disallow array in `run` options PR-URL: https://github.com/nodejs/node/pull/49935 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Luigi Pinca Reviewed-By: Moshe Atlow Reviewed-By: Colin Ihrig --- lib/internal/test_runner/runner.js | 7 +++---- test/parallel/test-runner-run.mjs | 24 ++++++++++++++++-------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 9588db005d75c3..cc9bcc09b714fe 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -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; diff --git a/test/parallel/test-runner-run.mjs b/test/parallel/test-runner-run.mjs index 02fed7a3659162..3201da25f87a1d 100644 --- a/test/parallel/test-runner-run.mjs +++ b/test/parallel/test-runner-run.mjs @@ -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()); @@ -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')] @@ -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' + })); + }); + }); });