Skip to content

Commit f767226

Browse files
sam-githubpiscisaureus
authored andcommitted
test: use assert.throw to test exceptions
The test wasn't checking directly that an assertion was thrown. Instead, it was checking that spawn did not sucessfully spawn a non-existent command. However, the command chosen, dir, exists in GNU coreutils, so it exists on Linux (though not on BSD derived OS X). The test as written passed on Linux, even with the TypeError it is supposed to be checking for deleted from spawn(). It would also pass on Windows if a ls.exe existed. The approach is unnecessarily obscure, assert.throw() is for asserting code throws, using it is more clear and works regardless of what commands do or do not exist. PR-URL: nodejs/node-v0.x-archive#8454 Reviewed-by: Trevor Norris <[email protected]> Cherry-picked-from: nodejs/node-v0.x-archive@2ff29cc Conflicts: test/parallel/test-child-process-spawn-typeerror.js
1 parent 22d2058 commit f767226

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

test/parallel/test-child-process-spawn-typeerror.js

+11-23
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
1-
var spawn = require('child_process').spawn,
2-
assert = require('assert'),
3-
windows = (process.platform === 'win32'),
4-
cmd = (windows) ? 'rundll32' : 'ls',
5-
invalidcmd = 'hopefully_you_dont_have_this_on_your_machine',
6-
invalidArgsMsg = /Incorrect value of args option/,
7-
invalidOptionsMsg = /options argument must be an object/,
8-
errors = 0;
9-
10-
try {
11-
// Ensure this throws a TypeError
12-
var child = spawn(invalidcmd, 'this is not an array');
13-
14-
child.on('error', function (err) {
15-
errors++;
16-
});
17-
18-
} catch (e) {
19-
assert.equal(e instanceof TypeError, true);
20-
}
1+
var assert = require('assert');
2+
var child_process = require('child_process');
3+
var spawn = child_process.spawn;
4+
var cmd = (process.platform === 'win32') ? 'rundll32' : 'ls';
5+
var invalidArgsMsg = /Incorrect value of args option/;
6+
var invalidOptionsMsg = /options argument must be an object/;
7+
8+
// verify that args argument must be an array
9+
assert.throws(function() {
10+
spawn(cmd, 'this is not an array');
11+
}, TypeError);
2112

2213
// verify that valid argument combinations do not throw
2314
assert.doesNotThrow(function() {
@@ -57,6 +48,3 @@ assert.throws(function() {
5748
spawn(cmd, [], 1);
5849
}, invalidOptionsMsg);
5950

60-
process.on('exit', function() {
61-
assert.equal(errors, 0);
62-
});

0 commit comments

Comments
 (0)