diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2018-05-02 16:59:49 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-05-10 14:14:23 +0200 |
commit | 560925fe22bfc23860b04704ff4cae21e4dd19ff (patch) | |
tree | 2e2be1e170fea1f8ae366d6c3b1a29bb0ea32445 /test/parallel/test-assert.js | |
parent | 5e6ca894cfc50690366a501be52e7248f3574d06 (diff) | |
download | android-node-v8-560925fe22bfc23860b04704ff4cae21e4dd19ff.tar.gz android-node-v8-560925fe22bfc23860b04704ff4cae21e4dd19ff.tar.bz2 android-node-v8-560925fe22bfc23860b04704ff4cae21e4dd19ff.zip |
assert: make sure throws is able to handle primitives
This fixes some possible issues with `assert.throws` and
`assert.rejects` in combination with an validation object. It will
now properly handle primitive values being thrown as error.
It also makes sure the `generatedMessage` property is properly set
if `assert.throws` or `assert.rejects` is used in combination with
an validation object and improves the error performance in such cases
by only creating the error once.
In addition it will fix detecting regular expressions from a different
context such as n-api that are passed through as validator for
`assert.throws` or `assert.rejects`. Until now those were not tested.
PR-URL: https://github.com/nodejs/node/pull/20482
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Diffstat (limited to 'test/parallel/test-assert.js')
-rw-r--r-- | test/parallel/test-assert.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index cab6fb33e1..dba40c0a0e 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -740,7 +740,9 @@ common.expectsError( const frames = err.stack.split('\n'); const [, filename, line, column] = frames[1].match(/\((.+):(\d+):(\d+)\)/); // Reset the cache to check again + const size = errorCache.size; errorCache.delete(`${filename}${line - 1}${column - 1}`); + assert.strictEqual(errorCache.size, size - 1); const data = `${'\n'.repeat(line - 1)}${' '.repeat(column - 1)}` + 'ok(failed(badly));'; try { @@ -849,6 +851,7 @@ common.expectsError( { name: 'AssertionError [ERR_ASSERTION]', code: 'ERR_ASSERTION', + generatedMessage: true, message: `${start}\n${actExp}\n\n` + " Comparison {\n name: 'Error',\n- message: 'foo'" + "\n+ message: ''\n }" @@ -940,3 +943,45 @@ assert.throws( ' }' } ); + +{ + let actual = null; + const expected = { message: 'foo' }; + assert.throws( + () => assert.throws( + () => { throw actual; }, + expected + ), + { + operator: 'throws', + actual, + expected, + generatedMessage: true, + message: `${start}\n${actExp}\n\n` + + '- null\n' + + '+ {\n' + + "+ message: 'foo'\n" + + '+ }' + } + ); + + actual = 'foobar'; + const message = 'message'; + assert.throws( + () => assert.throws( + () => { throw actual; }, + { message: 'foobar' }, + message + ), + { + actual, + message, + operator: 'throws', + generatedMessage: false + } + ); +} + +// TODO: This case is only there to make sure there is no breaking change. +// eslint-disable-next-line no-restricted-syntax, no-throw-literal +assert.throws(() => { throw 4; }, 4); |