diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2018-04-09 00:38:41 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-04-20 00:21:33 +0200 |
commit | 7007eee6a272398054c65f2f32bdf3467a29ee9f (patch) | |
tree | 4e79e6650e57ba6d3d2a4aae5cc29a28fd21c5d0 /lib | |
parent | ad1d1057f9362558fcc76b603458374f5f5a31c5 (diff) | |
download | android-node-v8-7007eee6a272398054c65f2f32bdf3467a29ee9f.tar.gz android-node-v8-7007eee6a272398054c65f2f32bdf3467a29ee9f.tar.bz2 android-node-v8-7007eee6a272398054c65f2f32bdf3467a29ee9f.zip |
assert: validate the block return type
This makes sure the returned value when calling `block` is actually
of type promise in case `assert.rejects` or `assert.doesNotReject`
is called.
PR-URL: https://github.com/nodejs/node/pull/19886
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/assert.js | 8 | ||||
-rw-r--r-- | lib/internal/errors.js | 10 |
2 files changed, 17 insertions, 1 deletions
diff --git a/lib/assert.js b/lib/assert.js index 62847e55c3..05b43b5b54 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -30,7 +30,8 @@ const { errorCache, codes: { ERR_AMBIGUOUS_ARGUMENT, - ERR_INVALID_ARG_TYPE + ERR_INVALID_ARG_TYPE, + ERR_INVALID_RETURN_VALUE } } = require('internal/errors'); const { openSync, closeSync, readSync } = require('fs'); @@ -455,6 +456,11 @@ async function waitForActual(block) { if (typeof block === 'function') { // Return a rejected promise if `block` throws synchronously. resultPromise = block(); + // Fail in case no promise is returned. + if (!checkIsPromise(resultPromise)) { + throw new ERR_INVALID_RETURN_VALUE('instance of Promise', + 'block', resultPromise); + } } else if (checkIsPromise(block)) { resultPromise = block; } else { diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 4823f0b65c..88c7f86eb9 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -904,6 +904,16 @@ E('ERR_INVALID_PROTOCOL', TypeError); E('ERR_INVALID_REPL_EVAL_CONFIG', 'Cannot specify both "breakEvalOnSigint" and "eval" for REPL', TypeError); +E('ERR_INVALID_RETURN_VALUE', (input, name, value) => { + let type; + if (value && value.constructor && value.constructor.name) { + type = `instance of ${value.constructor.name}`; + } else { + type = `type ${typeof value}`; + } + return `Expected ${input} to be returned from the "${name}"` + + ` function but got ${type}.`; +}, TypeError); E('ERR_INVALID_SYNC_FORK_INPUT', 'Asynchronous forks do not support Buffer, Uint8Array or string input: %s', TypeError); |