1'use strict'; 2const common = require('../common'); 3const fixtures = require('../common/fixtures'); 4const assert = require('assert'); 5const { fork } = require('child_process'); 6 7// This test check the arguments of `fork` method 8// Refs: https://github.com/nodejs/node/issues/20749 9const expectedEnv = { foo: 'bar' }; 10 11// Ensure that first argument `modulePath` must be provided 12// and be of type string 13{ 14 const invalidModulePath = [ 15 0, 16 true, 17 undefined, 18 null, 19 [], 20 {}, 21 () => {}, 22 Symbol('t'), 23 ]; 24 invalidModulePath.forEach((modulePath) => { 25 assert.throws(() => fork(modulePath), { 26 code: 'ERR_INVALID_ARG_TYPE', 27 name: 'TypeError', 28 message: /^The "modulePath" argument must be of type string/ 29 }); 30 }); 31 32 const cp = fork(fixtures.path('child-process-echo-options.js')); 33 cp.on( 34 'exit', 35 common.mustCall((code) => { 36 assert.strictEqual(code, 0); 37 }) 38 ); 39} 40 41// Ensure that the second argument of `fork` 42// and `fork` should parse options 43// correctly if args is undefined or null 44{ 45 const invalidSecondArgs = [ 46 0, 47 true, 48 () => {}, 49 Symbol('t'), 50 ]; 51 invalidSecondArgs.forEach((arg) => { 52 assert.throws( 53 () => { 54 fork(fixtures.path('child-process-echo-options.js'), arg); 55 }, 56 { 57 code: 'ERR_INVALID_ARG_VALUE', 58 name: 'TypeError' 59 } 60 ); 61 }); 62 63 const argsLists = [undefined, null, []]; 64 65 argsLists.forEach((args) => { 66 const cp = fork(fixtures.path('child-process-echo-options.js'), args, { 67 env: { ...process.env, ...expectedEnv } 68 }); 69 70 cp.on( 71 'message', 72 common.mustCall(({ env }) => { 73 assert.strictEqual(env.foo, expectedEnv.foo); 74 }) 75 ); 76 77 cp.on( 78 'exit', 79 common.mustCall((code) => { 80 assert.strictEqual(code, 0); 81 }) 82 ); 83 }); 84} 85 86// Ensure that the third argument should be type of object if provided 87{ 88 const invalidThirdArgs = [ 89 0, 90 true, 91 () => {}, 92 Symbol('t'), 93 ]; 94 invalidThirdArgs.forEach((arg) => { 95 assert.throws( 96 () => { 97 fork(fixtures.path('child-process-echo-options.js'), [], arg); 98 }, 99 { 100 code: 'ERR_INVALID_ARG_VALUE', 101 name: 'TypeError' 102 } 103 ); 104 }); 105} 106