1'use strict'; 2 3const common = require('../common'); 4const assert = require('assert'); 5const { ChildProcess } = require('child_process'); 6assert.strictEqual(typeof ChildProcess, 'function'); 7 8{ 9 // Verify that invalid options to spawn() throw. 10 const child = new ChildProcess(); 11 12 [undefined, null, 'foo', 0, 1, NaN, true, false].forEach((options) => { 13 assert.throws(() => { 14 child.spawn(options); 15 }, { 16 code: 'ERR_INVALID_ARG_TYPE', 17 name: 'TypeError', 18 message: 'The "options" argument must be of type object.' + 19 `${common.invalidArgTypeHelper(options)}` 20 }); 21 }); 22} 23 24{ 25 // Verify that spawn throws if file is not a string. 26 const child = new ChildProcess(); 27 28 [undefined, null, 0, 1, NaN, true, false, {}].forEach((file) => { 29 assert.throws(() => { 30 child.spawn({ file }); 31 }, { 32 code: 'ERR_INVALID_ARG_TYPE', 33 name: 'TypeError', 34 message: 'The "options.file" property must be of type string.' + 35 `${common.invalidArgTypeHelper(file)}` 36 }); 37 }); 38} 39 40{ 41 // Verify that spawn throws if envPairs is not an array or undefined. 42 const child = new ChildProcess(); 43 44 [null, 0, 1, NaN, true, false, {}, 'foo'].forEach((envPairs) => { 45 assert.throws(() => { 46 child.spawn({ envPairs, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] }); 47 }, { 48 code: 'ERR_INVALID_ARG_TYPE', 49 name: 'TypeError', 50 message: 'The "options.envPairs" property must be an instance of Array.' + 51 common.invalidArgTypeHelper(envPairs) 52 }); 53 }); 54} 55 56{ 57 // Verify that spawn throws if args is not an array or undefined. 58 const child = new ChildProcess(); 59 60 [null, 0, 1, NaN, true, false, {}, 'foo'].forEach((args) => { 61 assert.throws(() => { 62 child.spawn({ file: 'foo', args }); 63 }, { 64 code: 'ERR_INVALID_ARG_TYPE', 65 name: 'TypeError', 66 message: 'The "options.args" property must be an instance of Array.' + 67 common.invalidArgTypeHelper(args) 68 }); 69 }); 70} 71 72// Test that we can call spawn 73const child = new ChildProcess(); 74child.spawn({ 75 file: process.execPath, 76 args: ['--interactive'], 77 cwd: process.cwd(), 78 stdio: 'pipe' 79}); 80 81assert.strictEqual(Object.hasOwn(child, 'pid'), true); 82assert(Number.isInteger(child.pid)); 83 84// Try killing with invalid signal 85assert.throws( 86 () => { child.kill('foo'); }, 87 { code: 'ERR_UNKNOWN_SIGNAL', name: 'TypeError' } 88); 89 90assert.strictEqual(child.kill(), true); 91