Skip to content
Myr

node.js

This file

Path

test/arguments/node.js

305 lines · 15.2 KiB · JavaScript

Last changed by

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

History of this file

Take it

Change default value of `serialization` option to `advanced` (#905)ehmickybefore adoption4577582

line 1why, one step back
1import {once} from 'node:events';

Split files (#837)ehmickybefore adoption672c2d7

line 2why, one step back
2import {dirname, relative} from 'node:path';

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 3why, one step back
3import process, {version} from 'node:process';

Split files (#837)ehmickybefore adoption672c2d7

lines 4–31why, one step back
4import {pathToFileURL} from 'node:url';
5import test from 'ava';6import getNode from 'get-node';7import {pEvent} from 'p-event';8import {execa, execaSync, execaNode} from '../../index.js';9import {FIXTURES_DIR} from '../helpers/fixtures-dir.js';10import {identity, fullStdio} from '../helpers/stdio.js';11import {foobarString} from '../helpers/input.js';1213process.chdir(FIXTURES_DIR);1415const runWithNodeOption = (file, args, options) => Array.isArray(args)16 ? execa(file, args, {...options, node: true})17 : execa(file, {...options, node: true});18const runWithNodeOptionSync = (file, args, options) => Array.isArray(args)19 ? execaSync(file, args, {...options, node: true})20 : execaSync(file, {...options, node: true});21const runWithIpc = (file, options) => execa('node', [file], {...options, ipc: true});2223const testNodeSuccess = async (t, execaMethod) => {24 const {exitCode, stdout} = await execaMethod('noop.js', [foobarString]);25 t.is(exitCode, 0);26 t.is(stdout, foobarString);27};2829test('execaNode() succeeds', testNodeSuccess, execaNode);30test('The "node" option succeeds', testNodeSuccess, runWithNodeOption);31test('The "node" option succeeds - sync', testNodeSuccess, runWithNodeOptionSync);

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

lines 32–58why, one step back
32
33test('execaNode(options) succeeds', async t => {34 const {stdout} = await execaNode({stripFinalNewline: false})('noop.js', [foobarString]);35 t.is(stdout, `${foobarString}\n`);36});3738test('execaNode`...` succeeds', async t => {39 const {stdout} = await execaNode`noop.js ${foobarString}`;40 t.is(stdout, foobarString);41});4243test('execaNode().pipe(execaNode()) succeeds', async t => {44 const {stdout} = await execaNode('noop.js').pipe(execaNode('--version'));45 t.is(stdout, version);46});4748test('execaNode().pipe(execa()) requires using "node"', async t => {49 await t.throwsAsync(execaNode('noop.js').pipe(execa('--version')));50});5152test('execaNode().pipe(...) requires using "node"', async t => {53 await t.throwsAsync(execaNode('noop.js').pipe('--version'));54});5556test('execaNode().pipe`...` requires using "node"', async t => {57 await t.throwsAsync(execaNode('noop.js').pipe`--version`);58});

Split files (#837)ehmickybefore adoption672c2d7

lines 59–127why, one step back
59
60test('execaNode() cannot set the "node" option to false', t => {61 t.throws(() => {62 execaNode('empty.js', {node: false});63 }, {message: /The "node" option cannot be false/});64});6566const testDoubleNode = (t, nodePath, execaMethod) => {67 t.throws(() => {68 execaMethod(nodePath, ['noop.js']);69 }, {message: /does not need to be "node"/});70};7172test('Cannot use "node" as binary - execaNode()', testDoubleNode, 'node', execaNode);73test('Cannot use "node" as binary - "node" option', testDoubleNode, 'node', runWithNodeOption);74test('Cannot use "node" as binary - "node" option sync', testDoubleNode, 'node', runWithNodeOptionSync);75test('Cannot use path to "node" as binary - execaNode()', testDoubleNode, process.execPath, execaNode);76test('Cannot use path to "node" as binary - "node" option', testDoubleNode, process.execPath, runWithNodeOption);77test('Cannot use path to "node" as binary - "node" option sync', testDoubleNode, process.execPath, runWithNodeOptionSync);7879const getNodePath = async () => {80 const {path} = await getNode(TEST_NODE_VERSION);81 return path;82};8384const TEST_NODE_VERSION = '16.0.0';8586const testNodePath = async (t, execaMethod, mapPath) => {87 const nodePath = mapPath(await getNodePath());88 const {stdout} = await execaMethod('--version', [], {nodePath});89 t.is(stdout, `v${TEST_NODE_VERSION}`);90};9192test.serial('The "nodePath" option can be used - execaNode()', testNodePath, execaNode, identity);93test.serial('The "nodePath" option can be a file URL - execaNode()', testNodePath, execaNode, pathToFileURL);94test.serial('The "nodePath" option can be used - "node" option', testNodePath, runWithNodeOption, identity);95test.serial('The "nodePath" option can be a file URL - "node" option', testNodePath, runWithNodeOption, pathToFileURL);96test.serial('The "nodePath" option can be used - "node" option sync', testNodePath, runWithNodeOptionSync, identity);97test.serial('The "nodePath" option can be a file URL - "node" option sync', testNodePath, runWithNodeOptionSync, pathToFileURL);9899const testNodePathDefault = async (t, execaMethod) => {100 const {stdout} = await execaMethod('--version');101 t.is(stdout, process.version);102};103104test('The "nodePath" option defaults to the current Node.js binary - execaNode()', testNodePathDefault, execaNode);105test('The "nodePath" option defaults to the current Node.js binary - "node" option', testNodePathDefault, runWithNodeOption);106test('The "nodePath" option defaults to the current Node.js binary - "node" option sync', testNodePathDefault, runWithNodeOptionSync);107108const testNodePathInvalid = (t, execaMethod) => {109 t.throws(() => {110 execaMethod('noop.js', [], {nodePath: true});111 }, {message: /The "nodePath" option must be a string or a file URL/});112};113114test('The "nodePath" option must be a string or URL - execaNode()', testNodePathInvalid, execaNode);115test('The "nodePath" option must be a string or URL - "node" option', testNodePathInvalid, runWithNodeOption);116test('The "nodePath" option must be a string or URL - "node" option sync', testNodePathInvalid, runWithNodeOptionSync);117118const testFormerNodePath = (t, execaMethod) => {119 t.throws(() => {120 execaMethod('noop.js', [], {execPath: process.execPath});121 }, {message: /The "execPath" option has been removed/});122};123124test('The "execPath" option cannot be used - execaNode()', testFormerNodePath, execaNode);125test('The "execPath" option cannot be used - "node" option', testFormerNodePath, runWithNodeOption);126test('The "execPath" option cannot be used - "node" option sync', testFormerNodePath, runWithNodeOptionSync);127

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

line 128why, one step back
128const nodePathArguments = ['-p', ['process.env.Path || process.env.PATH']];

Split files (#837)ehmickybefore adoption672c2d7

line 129why, one step back
129

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 130why, one step back
130const testSubprocessNodePath = async (t, execaMethod, mapPath) => {

Split files (#837)ehmickybefore adoption672c2d7

line 131why, one step back
131 const nodePath = mapPath(await getNodePath());

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

line 132why, one step back
132 const {stdout} = await execaMethod(...nodePathArguments, {nodePath});

Split files (#837)ehmickybefore adoption672c2d7

lines 133–135why, one step back
133 t.true(stdout.includes(TEST_NODE_VERSION));
134};135

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 136–138why, one step back
136test.serial('The "nodePath" option impacts the subprocess - execaNode()', testSubprocessNodePath, execaNode, identity);
137test.serial('The "nodePath" option impacts the subprocess - "node" option', testSubprocessNodePath, runWithNodeOption, identity);138test.serial('The "nodePath" option impacts the subprocess - "node" option sync', testSubprocessNodePath, runWithNodeOptionSync, identity);

Split files (#837)ehmickybefore adoption672c2d7

line 139why, one step back
139

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 140why, one step back
140const testSubprocessNodePathDefault = async (t, execaMethod) => {

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

line 141why, one step back
141 const {stdout} = await execaMethod(...nodePathArguments);

Split files (#837)ehmickybefore adoption672c2d7

line 142why, one step back
142 t.true(stdout.includes(dirname(process.execPath)));

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

line 143why, one step back
143};

Split files (#837)ehmickybefore adoption672c2d7

line 144why, one step back
144

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 145–147why, one step back
145test('The "nodePath" option defaults to the current Node.js binary in the subprocess - execaNode()', testSubprocessNodePathDefault, execaNode);
146test('The "nodePath" option defaults to the current Node.js binary in the subprocess - "node" option', testSubprocessNodePathDefault, runWithNodeOption);147test('The "nodePath" option defaults to the current Node.js binary in the subprocess - "node" option sync', testSubprocessNodePathDefault, runWithNodeOptionSync);

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

line 148why, one step back
148

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 149why, one step back
149test.serial('The "nodePath" option requires "node: true" to impact the subprocess', async t => {

Split files (#837)ehmickybefore adoption672c2d7

line 150why, one step back
150 const nodePath = await getNodePath();

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

line 151why, one step back
151 const {stdout} = await execa('node', nodePathArguments.flat(), {nodePath});

Split files (#837)ehmickybefore adoption672c2d7

lines 152–154why, one step back
152 t.false(stdout.includes(TEST_NODE_VERSION));
153});154

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 155why, one step back
155const testSubprocessNodePathCwd = async (t, execaMethod) => {

Split files (#837)ehmickybefore adoption672c2d7

lines 156–158why, one step back
156 const nodePath = await getNodePath();
157 const cwd = dirname(dirname(nodePath));158 const relativeExecPath = relative(cwd, nodePath);

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

line 159why, one step back
159 const {stdout} = await execaMethod(...nodePathArguments, {nodePath: relativeExecPath, cwd});

Split files (#837)ehmickybefore adoption672c2d7

line 160why, one step back
160 t.true(stdout.includes(TEST_NODE_VERSION));

Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)ehmickybefore adoptionb965682

lines 161–162why, one step back
161};
162

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 163–165why, one step back
163test.serial('The "nodePath" option is relative to "cwd" when used in the subprocess - execaNode()', testSubprocessNodePathCwd, execaNode);
164test.serial('The "nodePath" option is relative to "cwd" when used in the subprocess - "node" option', testSubprocessNodePathCwd, runWithNodeOption);165test.serial('The "nodePath" option is relative to "cwd" when used in the subprocess - "node" option sync', testSubprocessNodePathCwd, runWithNodeOptionSync);

Split files (#837)ehmickybefore adoption672c2d7

lines 166–199why, one step back
166
167const testCwdNodePath = async (t, execaMethod) => {168 const nodePath = await getNodePath();169 const cwd = dirname(dirname(nodePath));170 const relativeExecPath = relative(cwd, nodePath);171 const {stdout} = await execaMethod('--version', [], {nodePath: relativeExecPath, cwd});172 t.is(stdout, `v${TEST_NODE_VERSION}`);173};174175test.serial('The "nodePath" option is relative to "cwd" - execaNode()', testCwdNodePath, execaNode);176test.serial('The "nodePath" option is relative to "cwd" - "node" option', testCwdNodePath, runWithNodeOption);177test.serial('The "nodePath" option is relative to "cwd" - "node" option sync', testCwdNodePath, runWithNodeOptionSync);178179const testNodeOptions = async (t, execaMethod) => {180 const {stdout} = await execaMethod('empty.js', [], {nodeOptions: ['--version']});181 t.is(stdout, process.version);182};183184test('The "nodeOptions" option can be used - execaNode()', testNodeOptions, execaNode);185test('The "nodeOptions" option can be used - "node" option', testNodeOptions, runWithNodeOption);186test('The "nodeOptions" option can be used - "node" option sync', testNodeOptions, runWithNodeOptionSync);187188const spawnNestedExecaNode = (realExecArgv, fakeExecArgv, execaMethod, nodeOptions) => execa(189 'node',190 [...realExecArgv, 'nested-node.js', fakeExecArgv, execaMethod, nodeOptions, 'noop.js', foobarString],191 {...fullStdio, cwd: FIXTURES_DIR},192);193194const testInspectRemoval = async (t, fakeExecArgv, execaMethod) => {195 const {stdout, stdio} = await spawnNestedExecaNode([], fakeExecArgv, execaMethod, '');196 t.is(stdout, foobarString);197 t.is(stdio[3], '');198};199

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 200–207why, one step back
200test('The "nodeOptions" option removes --inspect without a port when defined by current process - execaNode()', testInspectRemoval, '--inspect', 'execaNode');
201test('The "nodeOptions" option removes --inspect without a port when defined by current process - "node" option', testInspectRemoval, '--inspect', 'nodeOption');202test('The "nodeOptions" option removes --inspect with a port when defined by current process - execaNode()', testInspectRemoval, '--inspect=9222', 'execaNode');203test('The "nodeOptions" option removes --inspect with a port when defined by current process - "node" option', testInspectRemoval, '--inspect=9222', 'nodeOption');204test('The "nodeOptions" option removes --inspect-brk without a port when defined by current process - execaNode()', testInspectRemoval, '--inspect-brk', 'execaNode');205test('The "nodeOptions" option removes --inspect-brk without a port when defined by current process - "node" option', testInspectRemoval, '--inspect-brk', 'nodeOption');206test('The "nodeOptions" option removes --inspect-brk with a port when defined by current process - execaNode()', testInspectRemoval, '--inspect-brk=9223', 'execaNode');207test('The "nodeOptions" option removes --inspect-brk with a port when defined by current process - "node" option', testInspectRemoval, '--inspect-brk=9223', 'nodeOption');

Split files (#837)ehmickybefore adoption672c2d7

lines 208–214why, one step back
208
209const testInspectDifferentPort = async (t, execaMethod) => {210 const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9225'], '', execaMethod, '--inspect=9224');211 t.is(stdout, foobarString);212 t.true(stdio[3].includes('Debugger listening'));213};214

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 215–216why, one step back
215test.serial('The "nodeOptions" option allows --inspect with a different port even when defined by current process - execaNode()', testInspectDifferentPort, 'execaNode');
216test.serial('The "nodeOptions" option allows --inspect with a different port even when defined by current process - "node" option', testInspectDifferentPort, 'nodeOption');

Split files (#837)ehmickybefore adoption672c2d7

lines 217–223why, one step back
217
218const testInspectSamePort = async (t, execaMethod) => {219 const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9226'], '', execaMethod, '--inspect=9226');220 t.is(stdout, foobarString);221 t.true(stdio[3].includes('address already in use'));222};223

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 224–225why, one step back
224test.serial('The "nodeOptions" option forbids --inspect with the same port when defined by current process - execaNode()', testInspectSamePort, 'execaNode');
225test.serial('The "nodeOptions" option forbids --inspect with the same port when defined by current process - "node" option', testInspectSamePort, 'nodeOption');

Split files (#837)ehmickybefore adoption672c2d7

lines 226–286why, one step back
226
227const testIpc = async (t, execaMethod, options) => {228 const subprocess = execaMethod('send.js', [], options);229 await pEvent(subprocess, 'message');230 subprocess.send(foobarString);231 const {stdout, stdio} = await subprocess;232 t.is(stdout, foobarString);233 t.is(stdio.length, 4);234 t.is(stdio[3], undefined);235};236237test('execaNode() adds an ipc channel', testIpc, execaNode, {});238test('The "node" option adds an ipc channel', testIpc, runWithNodeOption, {});239test('The "ipc" option adds an ipc channel', testIpc, runWithIpc, {});240test('The "ipc" option works with "stdio: \'pipe\'"', testIpc, runWithIpc, {stdio: 'pipe'});241test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\']"', testIpc, runWithIpc, {stdio: ['pipe', 'pipe', 'pipe']});242test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\', \'ipc\']"', testIpc, runWithIpc, {stdio: ['pipe', 'pipe', 'pipe', 'ipc']});243test('The "ipc" option works with "stdout: \'pipe\'"', testIpc, runWithIpc, {stdout: 'pipe'});244245test('No ipc channel is added by default', async t => {246 const {stdio} = await t.throwsAsync(execa('node', ['send.js']), {message: /process.send is not a function/});247 t.is(stdio.length, 3);248});249250const testDisableIpc = async (t, execaMethod) => {251 const {failed, message, stdio} = await execaMethod('send.js', [], {ipc: false, reject: false});252 t.true(failed);253 t.true(message.includes('process.send is not a function'));254 t.is(stdio.length, 3);255};256257test('Can disable "ipc" - execaNode()', testDisableIpc, execaNode);258test('Can disable "ipc" - "node" option', testDisableIpc, runWithNodeOption);259test('Can disable "ipc" - "node" option sync', testDisableIpc, runWithNodeOptionSync);260261const NO_IPC_MESSAGE = /The "ipc: true" option cannot be used/;262263const testNoIpcSync = (t, node) => {264 t.throws(() => {265 execaSync('node', ['send.js'], {ipc: true, node});266 }, {message: NO_IPC_MESSAGE});267};268269test('Cannot use "ipc: true" with execaSync()', testNoIpcSync, undefined);270test('Cannot use "ipc: true" with execaSync() - "node: false"', testNoIpcSync, false);271272test('Cannot use "ipc: true" with execaSync() - "node: true"', t => {273 t.throws(() => {274 execaSync('send.js', {ipc: true, node: true});275 }, {message: NO_IPC_MESSAGE});276});277278const testNoShell = async (t, execaMethod) => {279 const {failed, message} = await execaMethod('node --version', [], {shell: true, reject: false});280 t.true(failed);281 t.true(message.includes('MODULE_NOT_FOUND'));282};283284test('Cannot use "shell: true" - execaNode()', testNoShell, execaNode);285test('Cannot use "shell: true" - "node" option', testNoShell, runWithNodeOption);286test('Cannot use "shell: true" - "node" option sync', testNoShell, runWithNodeOptionSync);

Change default value of `serialization` option to `advanced` (#905)ehmickybefore adoption4577582

lines 287–305why, one step back
287
288test('The "serialization" option defaults to "advanced"', async t => {289 const subprocess = execa('node', ['ipc-echo.js'], {ipc: true});290 subprocess.send([0n]);291 const [message] = await once(subprocess, 'message');292 t.is(message[0], 0n);293 await subprocess;294});295296test('The "serialization" option can be set to "json"', async t => {297 const subprocess = execa('node', ['ipc-echo.js'], {ipc: true, serialization: 'json'});298 t.throws(() => {299 subprocess.send([0n]);300 }, {message: /serialize a BigInt/});301 subprocess.send(0);302 const [message] = await once(subprocess, 'message');303 t.is(message, 0);304 await subprocess;305});