Skip to content
Myr

node.js

This file

Path

test/node.js

211 lines · 10.5 KiB · JavaScript

Last changed by

Add `node` option (#804)ehmickybefore adoption663136b

History of this file

Take it

The walk stopped after 200 commits: lines older than 7707880 point at it.

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

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

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 2why, one step back
2import process from 'node:process';

Allow `nodePath` option to be a file URL (#632)ehmickybefore adoption77e1477

line 3why, one step back
3import {pathToFileURL} from 'node:url';

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 4why, one step back
4import test from 'ava';

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 5why, one step back
5import getNode from 'get-node';

Minor tweaksSindre Sorhusbefore adoption244e87a

line 6why, one step back
6import {pEvent} from 'p-event';

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 7–9why, one step back
7import {execa, execaSync, execaNode} from '../index.js';
8import {FIXTURES_DIR} from './helpers/fixtures-dir.js';9import {identity, fullStdio} from './helpers/stdio.js';

Improve tests for `execaNode()` (#777)ehmickybefore adoption27bb798

line 10why, one step back
10import {foobarString} from './helpers/input.js';

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 11why, one step back
11

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 12why, one step back
12process.chdir(FIXTURES_DIR);

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 13why, one step back
13

Add `node` option (#804)ehmickybefore adoption663136b

lines 14–19why, one step back
14const runWithNodeOption = (file, args = [], options = {}) => execa(file, args, {...options, node: true});
15const runWithNodeOptionSync = (file, args = [], options = {}) => execaSync(file, args, {...options, node: true});16const runWithIpc = (file, options) => execa('node', [file], {...options, ipc: true});1718const testNodeSuccess = async (t, execaMethod) => {19 const {exitCode, stdout} = await execaMethod('noop.js', [foobarString]);

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 20why, one step back
20 t.is(exitCode, 0);

Add `node` option (#804)ehmickybefore adoption663136b

lines 21–22why, one step back
21 t.is(stdout, foobarString);
22};

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 23why, one step back
23

Add `node` option (#804)ehmickybefore adoption663136b

lines 24–31why, one step back
24test('execaNode() succeeds', testNodeSuccess, execaNode);
25test('The "node" option succeeds', testNodeSuccess, runWithNodeOption);26test('The "node" option succeeds - sync', testNodeSuccess, runWithNodeOptionSync);2728test('execaNode() cannot set the "node" option to false', t => {29 t.throws(() => {30 execaNode('empty.js', {node: false});31 }, {message: /The "node" option cannot be false/});

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 32–33why, one step back
32});
33

Add `node` option (#804)ehmickybefore adoption663136b

lines 34–46why, one step back
34const testDoubleNode = (t, execPath, execaMethod) => {
35 t.throws(() => {36 execaMethod(execPath, ['noop.js']);37 }, {message: /does not need to be "node"/});38};3940test('Cannot use "node" as binary - execaNode()', testDoubleNode, 'node', execaNode);41test('Cannot use "node" as binary - "node" option', testDoubleNode, 'node', runWithNodeOption);42test('Cannot use "node" as binary - "node" option sync', testDoubleNode, 'node', runWithNodeOptionSync);43test('Cannot use path to "node" as binary - execaNode()', testDoubleNode, process.execPath, execaNode);44test('Cannot use path to "node" as binary - "node" option', testDoubleNode, process.execPath, runWithNodeOption);45test('Cannot use path to "node" as binary - "node" option sync', testDoubleNode, process.execPath, runWithNodeOptionSync);46

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 47–53why, one step back
47const getNodePath = async () => {
48 const {path} = await getNode(TEST_NODE_VERSION);49 return path;50};5152const TEST_NODE_VERSION = '16.0.0';53

Add `node` option (#804)ehmickybefore adoption663136b

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

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

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

Add `node` option (#804)ehmickybefore adoption663136b

line 56why, one step back
56 const {stdout} = await execaMethod('--version', [], {nodePath});

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 57–59why, one step back
57 t.is(stdout, `v${TEST_NODE_VERSION}`);
58};59

Add `node` option (#804)ehmickybefore adoption663136b

lines 60–63why, one step back
60test.serial('The "nodePath" option can be used - execaNode()', testNodePath, execaNode, identity);
61test.serial('The "nodePath" option can be a file URL - execaNode()', testNodePath, execaNode, pathToFileURL);62test.serial('The "nodePath" option can be used - "node" option', testNodePath, runWithNodeOption, identity);63test.serial('The "nodePath" option can be a file URL - "node" option', testNodePath, runWithNodeOption, pathToFileURL);

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 64why, one step back
64

Add `node` option (#804)ehmickybefore adoption663136b

lines 65–66why, one step back
65const testNodePathDefault = async (t, execaMethod) => {
66 const {stdout} = await execaMethod('--version');

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 67why, one step back
67 t.is(stdout, process.version);

Add `node` option (#804)ehmickybefore adoption663136b

line 68why, one step back
68};

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 69why, one step back
69

Add `node` option (#804)ehmickybefore adoption663136b

lines 70–82why, one step back
70test('The "nodePath" option defaults to the current Node.js binary - execaNode()', testNodePathDefault, execaNode);
71test('The "nodePath" option defaults to the current Node.js binary - "node" option', testNodePathDefault, runWithNodeOption);7273const testNodePathInvalid = (t, execaMethod) => {74 t.throws(() => {75 execaMethod('noop.js', [], {nodePath: true});76 }, {message: /The "nodePath" option must be a string or a file URL/});77};7879test('The "nodePath" option must be a string or URL - execaNode()', testNodePathInvalid, execaNode);80test('The "nodePath" option must be a string or URL - "node" option', testNodePathInvalid, runWithNodeOption);81test('The "nodePath" option must be a string or URL - "node" option sync', testNodePathInvalid, runWithNodeOptionSync);82

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

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

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 84why, one step back
84

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 85–96why, one step back
85const testExecPath = async (t, mapPath) => {
86 const execPath = mapPath(await getNodePath());87 const {stdout} = await execa(...nodePathArguments, {preferLocal: true, execPath});88 t.true(stdout.includes(TEST_NODE_VERSION));89};9091test.serial('The "execPath" option can be used', testExecPath, identity);92test.serial('The "execPath" option can be a file URL', testExecPath, pathToFileURL);9394test('The "execPath" option defaults to the current Node.js binary', async t => {95 const {stdout} = await execa(...nodePathArguments, {preferLocal: true});96 t.true(stdout.includes(dirname(process.execPath)));

Allow `nodePath` option to be a file URL (#632)ehmickybefore adoption77e1477

lines 97–98why, one step back
97});
98

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 99–102why, one step back
99test.serial('The "execPath" option requires "preferLocal: true"', async t => {
100 const execPath = await getNodePath();101 const {stdout} = await execa(...nodePathArguments, {execPath});102 t.false(stdout.includes(TEST_NODE_VERSION));

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 103–104why, one step back
103});
104

Add `node` option (#804)ehmickybefore adoption663136b

lines 105–106why, one step back
105const testNodeOptions = async (t, execaMethod) => {
106 const {stdout} = await execaMethod('empty.js', [], {nodeOptions: ['--version']});

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 107why, one step back
107 t.is(stdout, process.version);

Add `node` option (#804)ehmickybefore adoption663136b

line 108why, one step back
108};

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 109why, one step back
109

Add `node` option (#804)ehmickybefore adoption663136b

lines 110–113why, one step back
110test('The "nodeOptions" option can be used - execaNode()', testNodeOptions, execaNode);
111test('The "nodeOptions" option can be used - "node" option', testNodeOptions, runWithNodeOption);112113const spawnNestedExecaNode = (realExecArgv, fakeExecArgv, execaMethod, nodeOptions) => execa(

Improve tests for `execaNode()` (#777)ehmickybefore adoption27bb798

line 114why, one step back
114 'node',

Add `node` option (#804)ehmickybefore adoption663136b

line 115why, one step back
115 [...realExecArgv, 'nested-node.js', fakeExecArgv, execaMethod, nodeOptions, 'noop.js', foobarString],

Improve tests for `execaNode()` (#777)ehmickybefore adoption27bb798

line 116why, one step back
116 {...fullStdio, cwd: FIXTURES_DIR},

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

lines 117–118why, one step back
117);
118

Add `node` option (#804)ehmickybefore adoption663136b

lines 119–120why, one step back
119const testInspectRemoval = async (t, fakeExecArgv, execaMethod) => {
120 const {stdout, stdio} = await spawnNestedExecaNode([], fakeExecArgv, execaMethod, '');

Improve tests for `execaNode()` (#777)ehmickybefore adoption27bb798

lines 121–123why, one step back
121 t.is(stdout, foobarString);
122 t.is(stdio[3], '');123};

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 124why, one step back
124

Add `node` option (#804)ehmickybefore adoption663136b

lines 125–132why, one step back
125test('The "nodeOptions" option removes --inspect without a port when defined by parent process - execaNode()', testInspectRemoval, '--inspect', 'execaNode');
126test('The "nodeOptions" option removes --inspect without a port when defined by parent process - "node" option', testInspectRemoval, '--inspect', 'nodeOption');127test('The "nodeOptions" option removes --inspect with a port when defined by parent process - execaNode()', testInspectRemoval, '--inspect=9222', 'execaNode');128test('The "nodeOptions" option removes --inspect with a port when defined by parent process - "node" option', testInspectRemoval, '--inspect=9222', 'nodeOption');129test('The "nodeOptions" option removes --inspect-brk without a port when defined by parent process - execaNode()', testInspectRemoval, '--inspect-brk', 'execaNode');130test('The "nodeOptions" option removes --inspect-brk without a port when defined by parent process - "node" option', testInspectRemoval, '--inspect-brk', 'nodeOption');131test('The "nodeOptions" option removes --inspect-brk with a port when defined by parent process - execaNode()', testInspectRemoval, '--inspect-brk=9223', 'execaNode');132test('The "nodeOptions" option removes --inspect-brk with a port when defined by parent process - "node" option', testInspectRemoval, '--inspect-brk=9223', 'nodeOption');

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 133why, one step back
133

Add `node` option (#804)ehmickybefore adoption663136b

lines 134–135why, one step back
134const testInspectDifferentPort = async (t, execaMethod) => {
135 const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9225'], '', execaMethod, '--inspect=9224');

Improve tests for `execaNode()` (#777)ehmickybefore adoption27bb798

lines 136–137why, one step back
136 t.is(stdout, foobarString);
137 t.true(stdio[3].includes('Debugger listening'));

Add `node` option (#804)ehmickybefore adoption663136b

line 138why, one step back
138};

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 139why, one step back
139

Add `node` option (#804)ehmickybefore adoption663136b

lines 140–144why, one step back
140test.serial('The "nodeOptions" option allows --inspect with a different port even when defined by parent process - execaNode()', testInspectDifferentPort, 'execaNode');
141test.serial('The "nodeOptions" option allows --inspect with a different port even when defined by parent process - "node" option', testInspectDifferentPort, 'nodeOption');142143const testInspectSamePort = async (t, execaMethod) => {144 const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9226'], '', execaMethod, '--inspect=9226');

Improve tests for `execaNode()` (#777)ehmickybefore adoption27bb798

lines 145–146why, one step back
145 t.is(stdout, foobarString);
146 t.true(stdio[3].includes('address already in use'));

Add `node` option (#804)ehmickybefore adoption663136b

line 147why, one step back
147};

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 148why, one step back
148

Add `node` option (#804)ehmickybefore adoption663136b

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

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 151–152why, one step back
151
152const testIpc = async (t, execaMethod, options) => {

Add `node` option (#804)ehmickybefore adoption663136b

line 153why, one step back
153 const subprocess = execaMethod('send.js', [], options);

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 154why, one step back
154 await pEvent(subprocess, 'message');

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 155–160why, one step back
155 subprocess.send(foobarString);
156 const {stdout, stdio} = await subprocess;157 t.is(stdout, foobarString);158 t.is(stdio.length, 4);159 t.is(stdio[3], undefined);160};

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 161why, one step back
161

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 162why, one step back
162test('execaNode() adds an ipc channel', testIpc, execaNode, {});

Add `node` option (#804)ehmickybefore adoption663136b

line 163why, one step back
163test('The "node" option adds an ipc channel', testIpc, runWithNodeOption, {});

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 164–168why, one step back
164test('The "ipc" option adds an ipc channel', testIpc, runWithIpc, {});
165test('The "ipc" option works with "stdio: \'pipe\'"', testIpc, runWithIpc, {stdio: 'pipe'});166test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\']"', testIpc, runWithIpc, {stdio: ['pipe', 'pipe', 'pipe']});167test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\', \'ipc\']"', testIpc, runWithIpc, {stdio: ['pipe', 'pipe', 'pipe', 'ipc']});168test('The "ipc" option works with "stdout: \'pipe\'"', testIpc, runWithIpc, {stdout: 'pipe'});

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 169why, one step back
169

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

lines 170–173why, one step back
170test('No ipc channel is added by default', async t => {
171 const {stdio} = await t.throwsAsync(execa('node', ['send.js']), {message: /process.send is not a function/});172 t.is(stdio.length, 3);173});

Fix processes not exiting on `inputFile` validation (#588)ehmickybefore adoption5b1a1c7

line 174why, one step back
174

Add `node` option (#804)ehmickybefore adoption663136b

lines 175–178why, one step back
175const testDisableIpc = async (t, execaMethod) => {
176 const {failed, message, stdio} = await execaMethod('send.js', [], {ipc: false, reject: false});177 t.true(failed);178 t.true(message.includes('process.send is not a function'));

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 179why, one step back
179 t.is(stdio.length, 3);

Add `node` option (#804)ehmickybefore adoption663136b

line 180why, one step back
180};

Improve some tests (#655)ehmickybefore adoption6cf1c5e

line 181why, one step back
181

Add `node` option (#804)ehmickybefore adoption663136b

lines 182–188why, one step back
182test('Can disable "ipc" - execaNode()', testDisableIpc, execaNode);
183test('Can disable "ipc" - "node" option', testDisableIpc, runWithNodeOption);184test('Can disable "ipc" - "node" option sync', testDisableIpc, runWithNodeOptionSync);185186const NO_IPC_MESSAGE = /The "ipc: true" option cannot be used/;187188const testNoIpcSync = (t, node) => {

Add `ipc` option (#794)ehmickybefore adoptionc8a7961

line 189why, one step back
189 t.throws(() => {

Add `node` option (#804)ehmickybefore adoption663136b

lines 190–200why, one step back
190 execaSync('node', ['send.js'], {ipc: true, node});
191 }, {message: NO_IPC_MESSAGE});192};193194test('Cannot use "ipc: true" with execaSync()', testNoIpcSync, undefined);195test('Cannot use "ipc: true" with execaSync() - "node: false"', testNoIpcSync, false);196197test('Cannot use "ipc: true" with execaSync() - "node: true"', t => {198 t.throws(() => {199 execaSync('send.js', {ipc: true, node: true});200 }, {message: NO_IPC_MESSAGE});

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

line 201why, one step back
201});

Add `node` option (#804)ehmickybefore adoption663136b

lines 202–211why, one step back
202
203const testNoShell = async (t, execaMethod) => {204 const {failed, message} = await execaMethod('node --version', [], {shell: true, reject: false});205 t.true(failed);206 t.true(message.includes('MODULE_NOT_FOUND'));207};208209test('Cannot use "shell: true" - execaNode()', testNoShell, execaNode);210test('Cannot use "shell: true" - "node" option', testNoShell, runWithNodeOption);211test('Cannot use "shell: true" - "node" option sync', testNoShell, runWithNodeOptionSync);