Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions test/fixtures/parse-args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { parseArgs } = require('util');

const parsedArgs = parseArgs({ strict: false }).values;

process.stdout.write(JSON.stringify(parsedArgs));
29 changes: 10 additions & 19 deletions test/parallel/test-parse-args.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { spawnPromisified } from '../common/index.mjs';
import { path } from '../common/fixtures.mjs';
Copy link
Contributor

@aduh95 aduh95 Dec 30, 2025

Choose a reason for hiding this comment

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

nit: path is usually referring to node:path, it's confusing to have it refer to a function

Suggested change
import { path } from '../common/fixtures.mjs';
import * as fixtures from '../common/fixtures.mjs';

EDIT: I guess it should be a follow up along with a lint rule

Copy link
Member Author

Choose a reason for hiding this comment

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

Both patterns are in common use at the moment, I don't mind changing but it will need following up in either case.

import assert from 'node:assert';
import { suite, test } from 'node:test';
import { parseArgs } from 'node:util';
Expand Down Expand Up @@ -212,6 +213,8 @@ test('order of option and positional does not matter (per README)', () => {
});

suite('correct default args', () => {
const expected = { code: 0, signal: null, stderr: '', stdout: { foo: true, bar: true } };

suite('with CLI flags', () => {
const evalCode = "JSON.stringify(require('util').parseArgs({ strict: false }).values)";
const evalCodePrinted = `process.stdout.write(${evalCode})`;
Expand All @@ -225,33 +228,21 @@ suite('correct default args', () => {
'--print --eval <script>': ['--print', '--eval', evalCode],
'--print --eval=<script>': ['--print', `--eval=${evalCode}`],
};
for (const description in execArgsTests) {
const execArgs = execArgsTests[description];
for (const [description, execArgs] of Object.entries(execArgsTests)) {
test(description, async () => {
const { code, signal, stderr, stdout } = await spawnPromisified(
process.execPath,
[...execArgs, '--', '--foo', '--bar']);
assert.deepStrictEqual({
code,
signal,
stderr,
stdout: JSON.parse(stdout),
}, {
code: 0,
signal: null,
stderr: '',
stdout: { foo: true, bar: true },
});
assert.deepStrictEqual({ code, signal, stderr, stdout: JSON.parse(stdout) }, expected);
});
}
});

test('without CLI flags', () => {
const holdArgv = process.argv;
process.argv = [process.argv0, 'script.js', '--foo', '--bar'];
const { values } = parseArgs({ strict: false });
assert.deepStrictEqual(values, { __proto__: null, foo: true, bar: true });
process.argv = holdArgv;
test('without CLI flags', async () => {
const { code, signal, stderr, stdout } = await spawnPromisified(
process.execPath,
[path('parse-args.js'), '--foo', '--bar']);
assert.deepStrictEqual({ code, signal, stderr, stdout: JSON.parse(stdout) }, expected);
});
});

Expand Down
Loading