summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2019-06-14 00:24:31 +0200
committerRuben Bridgewater <ruben@bridgewater.de>2019-10-01 22:55:07 +0200
commitace3f169172d1f2d9e72707ec949b802f09d47d1 (patch)
tree18c502f83e7abe74f27148e98df7b8f2b9a2eaa8 /test
parenteb05d6881521a969ad605b137e0a06847dd01971 (diff)
downloadandroid-node-v8-ace3f169172d1f2d9e72707ec949b802f09d47d1.tar.gz
android-node-v8-ace3f169172d1f2d9e72707ec949b802f09d47d1.tar.bz2
android-node-v8-ace3f169172d1f2d9e72707ec949b802f09d47d1.zip
assert: improve class instance errors
This improves `assert.throws()` and `assert.rejects()` in case error classes are used as validation for the expected error. In case of a failure it will now throw an `AssertionError` instead of the received error if the check fails. That error has the received error as actual property and the expected property is set to the expected error class. The error message should help users to identify the problem faster than before by providing extra context what went wrong. PR-URL: https://github.com/nodejs/node/pull/28263 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test')
-rw-r--r--test/parallel/test-assert.js71
1 files changed, 49 insertions, 22 deletions
diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js
index f65caac433..13f9d6427b 100644
--- a/test/parallel/test-assert.js
+++ b/test/parallel/test-assert.js
@@ -123,16 +123,19 @@ assert.throws(() => thrower(a.AssertionError));
assert.throws(() => thrower(TypeError));
// When passing a type, only catch errors of the appropriate type.
-{
- let threw = false;
- try {
- a.throws(() => thrower(TypeError), a.AssertionError);
- } catch (e) {
- threw = true;
- assert.ok(e instanceof TypeError, 'type');
+assert.throws(
+ () => a.throws(() => thrower(TypeError), a.AssertionError),
+ {
+ generatedMessage: true,
+ actual: new TypeError({}),
+ expected: a.AssertionError,
+ code: 'ERR_ASSERTION',
+ name: 'AssertionError',
+ operator: 'throws',
+ message: 'The error is expected to be an instance of "AssertionError". ' +
+ 'Received "TypeError"'
}
- assert.ok(threw, 'a.throws with an explicit error is eating extra errors');
-}
+);
// doesNotThrow should pass through all errors.
{
@@ -237,20 +240,27 @@ a.throws(() => thrower(TypeError), (err) => {
// https://github.com/nodejs/node/issues/3188
{
- let threw = false;
- let AnotherErrorType;
- try {
- const ES6Error = class extends Error {};
- AnotherErrorType = class extends Error {};
-
- assert.throws(() => { throw new AnotherErrorType('foo'); }, ES6Error);
- } catch (e) {
- threw = true;
- assert(e instanceof AnotherErrorType,
- `expected AnotherErrorType, received ${e}`);
- }
+ let actual;
+ assert.throws(
+ () => {
+ const ES6Error = class extends Error {};
+ const AnotherErrorType = class extends Error {};
- assert.ok(threw);
+ assert.throws(() => {
+ actual = new AnotherErrorType('foo');
+ throw actual;
+ }, ES6Error);
+ },
+ (err) => {
+ assert.strictEqual(
+ err.message,
+ 'The error is expected to be an instance of "ES6Error". ' +
+ 'Received "Error"'
+ );
+ assert.strictEqual(err.actual, actual);
+ return true;
+ }
+ );
}
// Check messages from assert.throws().
@@ -1299,3 +1309,20 @@ assert.throws(
assert(!err2.stack.includes('hidden'));
})();
}
+
+assert.throws(
+ () => assert.throws(() => { throw Symbol('foo'); }, RangeError),
+ {
+ message: 'The error is expected to be an instance of "RangeError". ' +
+ 'Received "Symbol(foo)"'
+ }
+);
+
+assert.throws(
+ // eslint-disable-next-line no-throw-literal
+ () => assert.throws(() => { throw [1, 2]; }, RangeError),
+ {
+ message: 'The error is expected to be an instance of "RangeError". ' +
+ 'Received "[Array]"'
+ }
+);