diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2018-03-20 01:49:29 +0100 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-03-25 03:10:15 +0200 |
commit | a1c96f8e07115d3b6ff1498e7e9f530d2e0f1b6b (patch) | |
tree | 41cdf64c8541ae06f0976d43670ca5bd086bf309 /lib | |
parent | 28e4e43e513ae90e8a4236dbae7f3442ab8dbb4f (diff) | |
download | android-node-v8-a1c96f8e07115d3b6ff1498e7e9f530d2e0f1b6b.tar.gz android-node-v8-a1c96f8e07115d3b6ff1498e7e9f530d2e0f1b6b.tar.bz2 android-node-v8-a1c96f8e07115d3b6ff1498e7e9f530d2e0f1b6b.zip |
assert: improve assert.throws
This switches the assert.throws output to the one used in strict mode
if a error object is used for comparison. From now on it will show
the complete difference between two objects instead of only showing
the first failing property.
It also fixes detecting properties with a undefined value and fails
in case the thrown error does not contain the value at all.
PR-URL: https://github.com/nodejs/node/pull/19463
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/assert.js | 53 |
1 files changed, 38 insertions, 15 deletions
diff --git a/lib/assert.js b/lib/assert.js index 35132bfb03..2fc3cf33e8 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -366,13 +366,38 @@ assert.notStrictEqual = function notStrictEqual(actual, expected, message) { } }; -function compareExceptionKey(actual, expected, key, msg) { - if (!isDeepStrictEqual(actual[key], expected[key])) { +class Comparison { + constructor(obj, keys) { + for (const key of keys) { + if (key in obj) + this[key] = obj[key]; + } + } +} + +function compareExceptionKey(actual, expected, key, message, keys) { + if (!(key in actual) || !isDeepStrictEqual(actual[key], expected[key])) { + if (!message) { + // Create placeholder objects to create a nice output. + const a = new Comparison(actual, keys); + const b = new Comparison(expected, keys); + + const tmpLimit = Error.stackTraceLimit; + Error.stackTraceLimit = 0; + const err = new AssertionError({ + actual: a, + expected: b, + operator: 'deepStrictEqual', + stackStartFn: assert.throws, + errorDiff: ERR_DIFF_EQUAL + }); + Error.stackTraceLimit = tmpLimit; + message = err.message; + } innerFail({ - actual: actual[key], - expected: expected[key], - message: msg || `${key}: expected ${inspect(expected[key])}, ` + - `not ${inspect(actual[key])}`, + actual, + expected, + message, operator: 'throws', stackStartFn: assert.throws }); @@ -389,16 +414,14 @@ function expectedException(actual, expected, msg) { 'expected', ['Function', 'RegExp'], expected ); } - // The name and message could be non enumerable. Therefore test them - // explicitly. - if ('name' in expected) { - compareExceptionKey(actual, expected, 'name', msg); - } - if ('message' in expected) { - compareExceptionKey(actual, expected, 'message', msg); + const keys = Object.keys(expected); + // Special handle errors to make sure the name and the message are compared + // as well. + if (expected instanceof Error) { + keys.push('name', 'message'); } - for (const key of Object.keys(expected)) { - compareExceptionKey(actual, expected, key, msg); + for (const key of keys) { + compareExceptionKey(actual, expected, key, msg, keys); } return true; } |