Skip to content

Commit ef3d889

Browse files
nfriedlyMylesBorins
authored andcommitted
test: validate 'expected' argument to mustCall()
instead of silently overwriting invalid values with the default PR-URL: #10692 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Michal Zasso <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 21704a3 commit ef3d889

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

test/common.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,10 @@ function runCallChecks(exitCode) {
397397

398398

399399
exports.mustCall = function(fn, expected) {
400-
if (typeof expected !== 'number') expected = 1;
400+
if (expected === undefined)
401+
expected = 1;
402+
else if (typeof expected !== 'number')
403+
throw new TypeError(`Invalid expected value: ${expected}`);
401404

402405
const context = {
403406
expected: expected,

test/parallel/test-common.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ var assert = require('assert');
55
common.globalCheck = false;
66
global.gc = 42; // Not a valid global unless --expose_gc is set.
77
assert.deepStrictEqual(common.leakedGlobals(), ['gc']);
8+
9+
assert.throws(function() {
10+
common.mustCall(function() {}, 'foo');
11+
}, /^TypeError: Invalid expected value: foo$/);
12+
13+
assert.throws(function() {
14+
common.mustCall(function() {}, /foo/);
15+
}, /^TypeError: Invalid expected value: \/foo\/$/);

0 commit comments

Comments
 (0)