From 24a021fef1202901275a118ae683bd5955e6031a Mon Sep 17 00:00:00 2001 From: sangwook Date: Tue, 17 Feb 2026 07:40:02 +0900 Subject: [PATCH] test_runner: preserve user flags in argv This commit fixes an issue where user-specified flags were being stripped from process.argv when running with the --test flag. It ensures that arguments following '--' or starting with '-' (that are not consumed by Node.js options) are correctly passed to the test process. Fixes: https://github.com/nodejs/node/issues/61852 --- lib/internal/main/test_runner.js | 24 ++++++++++++-- test/fixtures/test-runner/argv.js | 1 + test/parallel/test-runner-cli-args.js | 47 +++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/test-runner/argv.js create mode 100644 test/parallel/test-runner-cli-args.js diff --git a/lib/internal/main/test_runner.js b/lib/internal/main/test_runner.js index fda47897da9f06..c9c192052b87a2 100644 --- a/lib/internal/main/test_runner.js +++ b/lib/internal/main/test_runner.js @@ -1,7 +1,8 @@ 'use strict'; const { - ArrayPrototypeSlice, + ArrayPrototypePush, + StringPrototypeStartsWith, } = primordials; const { @@ -30,7 +31,26 @@ if (isUsingInspector() && options.isolation === 'process') { options.inspectPort = process.debugPort; } -options.globPatterns = ArrayPrototypeSlice(process.argv, 1); +const userArgs = []; +const globPatterns = []; +let isArg = false; + +for (let i = 1; i < process.argv.length; i++) { + const arg = process.argv[i]; + if (isArg) { + ArrayPrototypePush(userArgs, arg); + } else if (arg === '--') { + isArg = true; + ArrayPrototypePush(userArgs, arg); + } else if (StringPrototypeStartsWith(arg, '-')) { + ArrayPrototypePush(userArgs, arg); + } else { + ArrayPrototypePush(globPatterns, arg); + } +} + +options.globPatterns = globPatterns; +options.argv = userArgs; debug('test runner configuration:', options); run(options).on('test:summary', (data) => { diff --git a/test/fixtures/test-runner/argv.js b/test/fixtures/test-runner/argv.js new file mode 100644 index 00000000000000..4f94da66200e29 --- /dev/null +++ b/test/fixtures/test-runner/argv.js @@ -0,0 +1 @@ +console.log(JSON.stringify(process.argv)); diff --git a/test/parallel/test-runner-cli-args.js b/test/parallel/test-runner-cli-args.js new file mode 100644 index 00000000000000..5c88ca01832bf6 --- /dev/null +++ b/test/parallel/test-runner-cli-args.js @@ -0,0 +1,47 @@ +'use strict'; +require('../common'); +const assert = require('assert'); +const { spawnSync } = require('child_process'); +const fixtures = require('../common/fixtures'); + +const testFixture = fixtures.path('test-runner/argv.js'); + +{ + const args = ['--test', testFixture, '--hello']; + const child = spawnSync(process.execPath, args); + + assert.strictEqual(child.stderr.toString(), ''); + const stdout = child.stdout.toString(); + assert.match(stdout, /"--hello"\]/); + assert.strictEqual(child.status, 0); +} + +{ + const args = ['--test', testFixture, '--', '--hello', '--world']; + const child = spawnSync(process.execPath, args); + + assert.strictEqual(child.stderr.toString(), ''); + const stdout = child.stdout.toString(); + assert.match(stdout, /"--","--hello","--world"\]/); + assert.strictEqual(child.status, 0); +} + +{ + const args = ['--test', testFixture, '--', 'foo.js']; + const child = spawnSync(process.execPath, args); + + assert.strictEqual(child.stderr.toString(), ''); + const stdout = child.stdout.toString(); + assert.match(stdout, /"--","foo\.js"\]/); + assert.strictEqual(child.status, 0); +} + +{ + const args = ['--test', testFixture, '--hello', '--', 'world']; + const child = spawnSync(process.execPath, args); + + assert.strictEqual(child.stderr.toString(), ''); + const stdout = child.stdout.toString(); + assert.match(stdout, /"--hello","--","world"\]/); + assert.strictEqual(child.status, 0); +}