diff --git a/test/common.js b/test/common.js index 8a5a5f14c1cba0..17d951876e59d7 100644 --- a/test/common.js +++ b/test/common.js @@ -402,8 +402,10 @@ function runCallChecks(exitCode) { } -exports.mustCall = function(fn, expected) { - if (typeof expected !== 'number') expected = 1; +exports.mustCall = function(fn, expected = 1) { + if (typeof expected !== 'number' || expected < 0) { + throw new RangeError(`Invalid expected value: ${expected}`); + } var context = { expected: expected, diff --git a/test/parallel/test-common.js b/test/parallel/test-common.js index 5ffe2c351503c1..0cc4313c74f27d 100644 --- a/test/parallel/test-common.js +++ b/test/parallel/test-common.js @@ -5,3 +5,12 @@ var assert = require('assert'); common.globalCheck = false; global.gc = 42; // Not a valid global unless --expose_gc is set. assert.deepStrictEqual(common.leakedGlobals(), ['gc']); + + +assert.throws(function() { + common.mustCall(function() {}, 'foo')(); +}, /invalid expected value: foo/i); + +assert.throws(function() { + common.mustCall(function() {}, /foo/)(); +}, /invalid expected value: \/foo\//i);