mirror of
https://github.com/nodejs/node.git
synced 2025-05-04 18:29:54 +00:00

This completely refactors the `expectsError` behavior: so far it's almost identical to `assert.throws(fn, object)` in case it was used with a function as first argument. It had a magical property check that allowed to verify a functions `type` in case `type` was passed used in the validation object. This pattern is now completely removed and `assert.throws()` should be used instead. The main intent for `common.expectsError()` is to verify error cases for callback based APIs. This is now more flexible by accepting all validation possibilites that `assert.throws()` accepts as well. No magical properties exist anymore. This reduces surprising behavior for developers who are not used to the Node.js core code base. This has the side effect that `common` is used significantly less frequent. PR-URL: https://github.com/nodejs/node/pull/31092 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
91 lines
2.4 KiB
JavaScript
91 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');
|
|
|
|
{
|
|
// Verify that invalid options to spawn() throw.
|
|
const child = new ChildProcess();
|
|
|
|
[undefined, null, 'foo', 0, 1, NaN, true, false].forEach((options) => {
|
|
assert.throws(() => {
|
|
child.spawn(options);
|
|
}, {
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: 'The "options" argument must be of type object.' +
|
|
`${common.invalidArgTypeHelper(options)}`
|
|
});
|
|
});
|
|
}
|
|
|
|
{
|
|
// Verify that spawn throws if file is not a string.
|
|
const child = new ChildProcess();
|
|
|
|
[undefined, null, 0, 1, NaN, true, false, {}].forEach((file) => {
|
|
assert.throws(() => {
|
|
child.spawn({ file });
|
|
}, {
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: 'The "options.file" property must be of type string.' +
|
|
`${common.invalidArgTypeHelper(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) => {
|
|
assert.throws(() => {
|
|
child.spawn({ envPairs, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });
|
|
}, {
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: 'The "options.envPairs" property must be an instance of Array.' +
|
|
common.invalidArgTypeHelper(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) => {
|
|
assert.throws(() => {
|
|
child.spawn({ file: 'foo', args });
|
|
}, {
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: 'The "options.args" property must be an instance of Array.' +
|
|
common.invalidArgTypeHelper(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
|
|
assert.throws(
|
|
() => { child.kill('foo'); },
|
|
{ code: 'ERR_UNKNOWN_SIGNAL', name: 'TypeError' }
|
|
);
|
|
|
|
assert.strictEqual(child.kill(), true);
|