node/test/parallel/test-child-process-constructor.js
Ruben Bridgewater c6b6c92185
lib: always show ERR_INVALID_ARG_TYPE received part
This makes a effort to make sure all of these errors will actually
also show the received input.
On top of that it refactors a few tests for better maintainability.
It will also change the returned type to always be a simple typeof
instead of special handling null.

PR-URL: https://github.com/nodejs/node/pull/19445
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2018-03-25 01:45:37 +01:00

95 lines
2.4 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const { ChildProcess } = require('child_process');
assert.strictEqual(typeof ChildProcess, 'function');
function typeName(value) {
return typeof value;
}
{
// Verify that invalid options to spawn() throw.
const child = new ChildProcess();
[undefined, null, 'foo', 0, 1, NaN, true, false].forEach((options) => {
common.expectsError(() => {
child.spawn(options);
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options" argument must be of type Object. ' +
`Received type ${typeName(options)}`
});
});
}
{
// Verify that spawn throws if file is not a string.
const child = new ChildProcess();
[undefined, null, 0, 1, NaN, true, false, {}].forEach((file) => {
common.expectsError(() => {
child.spawn({ file });
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.file" property must be of type string. ' +
`Received type ${typeName(file)}`
});
});
}
{
// Verify that spawn throws if envPairs is not an array or undefined.
const child = new ChildProcess();
[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((envPairs) => {
common.expectsError(() => {
child.spawn({ envPairs, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.envPairs" property must be of type Array. ' +
`Received type ${typeName(envPairs)}`
});
});
}
{
// Verify that spawn throws if args is not an array or undefined.
const child = new ChildProcess();
[null, 0, 1, NaN, true, false, {}, 'foo'].forEach((args) => {
common.expectsError(() => {
child.spawn({ file: 'foo', args });
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options.args" property must be of type Array. ' +
`Received type ${typeName(args)}`
});
});
}
// test that we can call spawn
const child = new ChildProcess();
child.spawn({
file: process.execPath,
args: ['--interactive'],
cwd: process.cwd(),
stdio: 'pipe'
});
assert.strictEqual(child.hasOwnProperty('pid'), true);
assert(Number.isInteger(child.pid));
// try killing with invalid signal
common.expectsError(
() => { child.kill('foo'); },
{ code: 'ERR_UNKNOWN_SIGNAL', type: TypeError }
);
assert.strictEqual(child.kill(), true);