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 fileTake it
The walk stopped after 200 commits: lines older than 7707880 point at it.
Add `ipc` option (#794)ehmickybefore adoptionc8a7961
line 1why, one step back1import {dirname} from 'node:path';Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 2why, one step back2import process from 'node:process';Allow `nodePath` option to be a file URL (#632)ehmickybefore adoption77e1477
line 3why, one step back3import {pathToFileURL} from 'node:url';Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 4why, one step back4import test from 'ava';Add `ipc` option (#794)ehmickybefore adoptionc8a7961
line 5why, one step back5import getNode from 'get-node';Minor tweaksSindre Sorhusbefore adoption244e87a
line 6why, one step back6import {pEvent} from 'p-event';Add `ipc` option (#794)ehmickybefore adoptionc8a7961
lines 7–9why, one step back7import {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 back10import {foobarString} from './helpers/input.js';Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 11why, one step back11Add `ipc` option (#794)ehmickybefore adoptionc8a7961
line 12why, one step back12process.chdir(FIXTURES_DIR);Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 13why, one step back13Add `node` option (#804)ehmickybefore adoption663136b
lines 14–19why, one step back14const 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 back20 t.is(exitCode, 0);Add `node` option (#804)ehmickybefore adoption663136b
lines 21–22why, one step back21 t.is(stdout, foobarString);22};Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 23why, one step back23Add `node` option (#804)ehmickybefore adoption663136b
lines 24–31why, one step back24test('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 back32});33Add `node` option (#804)ehmickybefore adoption663136b
lines 34–46why, one step back34const 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);46Add `ipc` option (#794)ehmickybefore adoptionc8a7961
lines 47–53why, one step back47const getNodePath = async () => {48 const {path} = await getNode(TEST_NODE_VERSION);49 return path;50};5152const TEST_NODE_VERSION = '16.0.0';53Add `node` option (#804)ehmickybefore adoption663136b
line 54why, one step back54const testNodePath = async (t, execaMethod, mapPath) => {Add `ipc` option (#794)ehmickybefore adoptionc8a7961
line 55why, one step back55 const nodePath = mapPath(await getNodePath());Add `node` option (#804)ehmickybefore adoption663136b
line 56why, one step back56 const {stdout} = await execaMethod('--version', [], {nodePath});Add `ipc` option (#794)ehmickybefore adoptionc8a7961
lines 57–59why, one step back57 t.is(stdout, `v${TEST_NODE_VERSION}`);58};59Add `node` option (#804)ehmickybefore adoption663136b
lines 60–63why, one step back60test.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 back64Add `node` option (#804)ehmickybefore adoption663136b
lines 65–66why, one step back65const testNodePathDefault = async (t, execaMethod) => {66 const {stdout} = await execaMethod('--version');Add `ipc` option (#794)ehmickybefore adoptionc8a7961
line 67why, one step back67 t.is(stdout, process.version);Add `node` option (#804)ehmickybefore adoption663136b
line 68why, one step back68};Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 69why, one step back69Add `node` option (#804)ehmickybefore adoption663136b
lines 70–82why, one step back70test('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);82Add `ipc` option (#794)ehmickybefore adoptionc8a7961
line 83why, one step back83const 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 back84Add `ipc` option (#794)ehmickybefore adoptionc8a7961
lines 85–96why, one step back85const 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 back97});98Add `ipc` option (#794)ehmickybefore adoptionc8a7961
lines 99–102why, one step back99test.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 back103});104Add `node` option (#804)ehmickybefore adoption663136b
lines 105–106why, one step back105const testNodeOptions = async (t, execaMethod) => {106 const {stdout} = await execaMethod('empty.js', [], {nodeOptions: ['--version']});Add `ipc` option (#794)ehmickybefore adoptionc8a7961
line 107why, one step back107 t.is(stdout, process.version);Add `node` option (#804)ehmickybefore adoption663136b
line 108why, one step back108};Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 109why, one step back109Add `node` option (#804)ehmickybefore adoption663136b
lines 110–113why, one step back110test('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 back114 'node',Add `node` option (#804)ehmickybefore adoption663136b
line 115why, one step back115 [...realExecArgv, 'nested-node.js', fakeExecArgv, execaMethod, nodeOptions, 'noop.js', foobarString],Improve tests for `execaNode()` (#777)ehmickybefore adoption27bb798
line 116why, one step back116 {...fullStdio, cwd: FIXTURES_DIR},Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
lines 117–118why, one step back117);118Add `node` option (#804)ehmickybefore adoption663136b
lines 119–120why, one step back119const 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 back121 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 back124Add `node` option (#804)ehmickybefore adoption663136b
lines 125–132why, one step back125test('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 back133Add `node` option (#804)ehmickybefore adoption663136b
lines 134–135why, one step back134const 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 back136 t.is(stdout, foobarString);137 t.true(stdio[3].includes('Debugger listening'));Add `node` option (#804)ehmickybefore adoption663136b
line 138why, one step back138};Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 139why, one step back139Add `node` option (#804)ehmickybefore adoption663136b
lines 140–144why, one step back140test.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 back145 t.is(stdout, foobarString);146 t.true(stdio[3].includes('address already in use'));Add `node` option (#804)ehmickybefore adoption663136b
line 147why, one step back147};Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 148why, one step back148Add `node` option (#804)ehmickybefore adoption663136b
lines 149–150why, one step back149test.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 back151152const testIpc = async (t, execaMethod, options) => {Add `node` option (#804)ehmickybefore adoption663136b
line 153why, one step back153 const subprocess = execaMethod('send.js', [], options);Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 154why, one step back154 await pEvent(subprocess, 'message');Add `ipc` option (#794)ehmickybefore adoptionc8a7961
lines 155–160why, one step back155 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 back161Add `ipc` option (#794)ehmickybefore adoptionc8a7961
line 162why, one step back162test('execaNode() adds an ipc channel', testIpc, execaNode, {});Add `node` option (#804)ehmickybefore adoption663136b
line 163why, one step back163test('The "node" option adds an ipc channel', testIpc, runWithNodeOption, {});Add `ipc` option (#794)ehmickybefore adoptionc8a7961
lines 164–168why, one step back164test('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 back169Add `ipc` option (#794)ehmickybefore adoptionc8a7961
lines 170–173why, one step back170test('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 back174Add `node` option (#804)ehmickybefore adoption663136b
lines 175–178why, one step back175const 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 back179 t.is(stdio.length, 3);Add `node` option (#804)ehmickybefore adoption663136b
line 180why, one step back180};Improve some tests (#655)ehmickybefore adoption6cf1c5e
line 181why, one step back181Add `node` option (#804)ehmickybefore adoption663136b
lines 182–188why, one step back182test('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 back189 t.throws(() => {Add `node` option (#804)ehmickybefore adoption663136b
lines 190–200why, one step back190 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 back201});Add `node` option (#804)ehmickybefore adoption663136b
lines 202–211why, one step back202203const 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);