diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2018-04-07 14:22:29 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-04-13 19:56:33 +0200 |
commit | a25f56730e815794ff645a2a050da22343fa7d89 (patch) | |
tree | 53d31ce028e1a87867eb5c3269849a65be38e1ec /test/parallel/test-assert.js | |
parent | ba9dc748388618c64af42006bc92f2c1d1abde09 (diff) | |
download | android-node-v8-a25f56730e815794ff645a2a050da22343fa7d89.tar.gz android-node-v8-a25f56730e815794ff645a2a050da22343fa7d89.tar.bz2 android-node-v8-a25f56730e815794ff645a2a050da22343fa7d89.zip |
assert: detect faulty throws usage
One of the biggest downsides to the `assert.throws` API is that it
does not check for the error message in case that is used as second
argument. It will instead be used in case no error is thrown.
This improves the situation by checking the actual error message
against the provided one and throws an error in case they are
identical. It is very unlikely that the user wants to use that error
message as information instead of checking against that message.
PR-URL: https://github.com/nodejs/node/pull/19867
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-assert.js')
-rw-r--r-- | test/parallel/test-assert.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 4115167d4b..c061551604 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -836,3 +836,32 @@ common.expectsError( } ); } + +assert.throws( + () => a.throws( + // eslint-disable-next-line no-throw-literal + () => { throw 'foo'; }, + 'foo' + ), + { + code: 'ERR_AMBIGUOUS_ARGUMENT', + message: 'The "error/message" argument is ambiguous. ' + + 'The error "foo" is identical to the message.' + } +); + +assert.throws( + () => a.throws( + () => { throw new TypeError('foo'); }, + 'foo' + ), + { + code: 'ERR_AMBIGUOUS_ARGUMENT', + message: 'The "error/message" argument is ambiguous. ' + + 'The error message "foo" is identical to the message.' + } +); + +// Should not throw. +// eslint-disable-next-line no-restricted-syntax, no-throw-literal +assert.throws(() => { throw null; }, 'foo'); |