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); +}