Skip to content

Commit dbb7727

Browse files
nfriedlyitaloacasas
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 e408f0a commit dbb7727

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

test/common.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,10 @@ function runCallChecks(exitCode) {
398398

399399

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

403406
const context = {
404407
expected: expected,

test/parallel/test-common.js

+8
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)