Skip to content

Commit

Permalink
test: validate 'expected' argument to mustCall()
Browse files Browse the repository at this point in the history
instead of silently overwriting invalid values with the default

PR-URL: nodejs#10692
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Michal Zasso <targos@protonmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
nfriedly authored and italoacasas committed Jan 27, 2017
1 parent e408f0a commit dbb7727
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,10 @@ function runCallChecks(exitCode) {


exports.mustCall = function(fn, expected) {
if (typeof expected !== 'number') expected = 1;
if (expected === undefined)
expected = 1;
else if (typeof expected !== 'number')
throw new TypeError(`Invalid expected value: ${expected}`);

const context = {
expected: expected,
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ 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');
}, /^TypeError: Invalid expected value: foo$/);

assert.throws(function() {
common.mustCall(function() {}, /foo/);
}, /^TypeError: Invalid expected value: \/foo\/$/);

0 comments on commit dbb7727

Please sign in to comment.