| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent eb05d68 commit ace3f16
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,7 +20,7 @@ | |||
| 20 | 20 | ||
| 21 | 21 | 'use strict'; | |
| 22 | 22 | ||
| 23 | - const { Object } = primordials; | ||
| 23 | + const { Object, ObjectPrototype } = primordials; | ||
| 24 | 24 | ||
| 25 | 25 | const { Buffer } = require('buffer'); | |
| 26 | 26 | const { codes: { | |
@@ -36,6 +36,7 @@ const { inspect } = require('internal/util/inspect'); | |||
| 36 | 36 | const { isPromise, isRegExp } = require('internal/util/types'); | |
| 37 | 37 | const { EOL } = require('internal/constants'); | |
| 38 | 38 | const { NativeModule } = require('internal/bootstrap/loaders'); | |
| 39 | + const { isError } = require('internal/util'); | ||
| 39 | 40 | ||
| 40 | 41 | const errorCache = new Map(); | |
| 41 | 42 | ||
@@ -561,6 +562,8 @@ function compareExceptionKey(actual, expected, key, message, keys, fn) { | |||
| 561 | 562 | } | |
| 562 | 563 | ||
| 563 | 564 | function expectedException(actual, expected, message, fn) { | |
| 565 | + let generatedMessage = false; | ||
| 566 | + | ||
| 564 | 567 | if (typeof expected !== 'function') { | |
| 565 | 568 | // Handle regular expressions. | |
| 566 | 569 | if (isRegExp(expected)) { | |
@@ -618,8 +621,26 @@ function expectedException(actual, expected, message, fn) { | |||
| 618 | 621 | if (expected.prototype !== undefined && actual instanceof expected) { | |
| 619 | 622 | return; | |
| 620 | 623 | } | |
| 621 | - if (Error.isPrototypeOf(expected)) { | ||
| 622 | - throw actual; | ||
| 624 | + if (ObjectPrototype.isPrototypeOf(Error, expected)) { | ||
| 625 | + if (!message) { | ||
| 626 | + generatedMessage = true; | ||
| 627 | + message = 'The error is expected to be an instance of ' + | ||
| 628 | + `"${expected.name}". Received `; | ||
| 629 | + if (isError(actual)) { | ||
| 630 | + message += `"${actual.name}"`; | ||
| 631 | + } else { | ||
| 632 | + message += `"${inspect(actual, { depth: -1 })}"`; | ||
| 633 | + } | ||
| 634 | + } | ||
| 635 | + const err = new AssertionError({ | ||
| 636 | + actual, | ||
| 637 | + expected, | ||
| 638 | + message, | ||
| 639 | + operator: fn.name, | ||
| 640 | + stackStartFn: fn | ||
| 641 | + }); | ||
| 642 | + err.generatedMessage = generatedMessage; | ||
| 643 | + throw err; | ||
| 623 | 644 | } | |
| 624 | 645 | ||
| 625 | 646 | // Check validation functions return value. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -123,16 +123,19 @@ assert.throws(() => thrower(a.AssertionError)); | |||
| 123 | 123 | assert.throws(() => thrower(TypeError)); | |
| 124 | 124 | ||
| 125 | 125 | // When passing a type, only catch errors of the appropriate type. | |
| 126 | - { | ||
| 127 | - let threw = false; | ||
| 128 | - try { | ||
| 129 | - a.throws(() => thrower(TypeError), a.AssertionError); | ||
| 130 | - } catch (e) { | ||
| 131 | - threw = true; | ||
| 132 | - assert.ok(e instanceof TypeError, 'type'); | ||
| 126 | + assert.throws( | ||
| 127 | + () => a.throws(() => thrower(TypeError), a.AssertionError), | ||
| 128 | + { | ||
| 129 | + generatedMessage: true, | ||
| 130 | + actual: new TypeError({}), | ||
| 131 | + expected: a.AssertionError, | ||
| 132 | + code: 'ERR_ASSERTION', | ||
| 133 | + name: 'AssertionError', | ||
| 134 | + operator: 'throws', | ||
| 135 | + message: 'The error is expected to be an instance of "AssertionError". ' + | ||
| 136 | + 'Received "TypeError"' | ||
| 133 | 137 | } | |
| 134 | - assert.ok(threw, 'a.throws with an explicit error is eating extra errors'); | ||
| 135 | - } | ||
| 138 | + ); | ||
| 136 | 139 | ||
| 137 | 140 | // doesNotThrow should pass through all errors. | |
| 138 | 141 | { | |
@@ -237,20 +240,27 @@ a.throws(() => thrower(TypeError), (err) => { | |||
| 237 | 240 | ||
| 238 | 241 | // https://github.com/nodejs/node/issues/3188 | |
| 239 | 242 | { | |
| 240 | - let threw = false; | ||
| 241 | - let AnotherErrorType; | ||
| 242 | - try { | ||
| 243 | - const ES6Error = class extends Error {}; | ||
| 244 | - AnotherErrorType = class extends Error {}; | ||
| 245 | - | ||
| 246 | - assert.throws(() => { throw new AnotherErrorType('foo'); }, ES6Error); | ||
| 247 | - } catch (e) { | ||
| 248 | - threw = true; | ||
| 249 | - assert(e instanceof AnotherErrorType, | ||
| 250 | - `expected AnotherErrorType, received ${e}`); | ||
| 251 | - } | ||
| 243 | + let actual; | ||
| 244 | + assert.throws( | ||
| 245 | + () => { | ||
| 246 | + const ES6Error = class extends Error {}; | ||
| 247 | + const AnotherErrorType = class extends Error {}; | ||
| 252 | 248 | ||
| 253 | - assert.ok(threw); | ||
| 249 | + assert.throws(() => { | ||
| 250 | + actual = new AnotherErrorType('foo'); | ||
| 251 | + throw actual; | ||
| 252 | + }, ES6Error); | ||
| 253 | + }, | ||
| 254 | + (err) => { | ||
| 255 | + assert.strictEqual( | ||
| 256 | + err.message, | ||
| 257 | + 'The error is expected to be an instance of "ES6Error". ' + | ||
| 258 | + 'Received "Error"' | ||
| 259 | + ); | ||
| 260 | + assert.strictEqual(err.actual, actual); | ||
| 261 | + return true; | ||
| 262 | + } | ||
| 263 | + ); | ||
| 254 | 264 | } | |
| 255 | 265 | ||
| 256 | 266 | // Check messages from assert.throws(). | |
@@ -1299,3 +1309,20 @@ assert.throws( | |||
| 1299 | 1309 | assert(!err2.stack.includes('hidden')); | |
| 1300 | 1310 | })(); | |
| 1301 | 1311 | } | |
| 1312 | + | ||
| 1313 | + assert.throws( | ||
| 1314 | + () => assert.throws(() => { throw Symbol('foo'); }, RangeError), | ||
| 1315 | + { | ||
| 1316 | + message: 'The error is expected to be an instance of "RangeError". ' + | ||
| 1317 | + 'Received "Symbol(foo)"' | ||
| 1318 | + } | ||
| 1319 | + ); | ||
| 1320 | + | ||
| 1321 | + assert.throws( | ||
| 1322 | + // eslint-disable-next-line no-throw-literal | ||
| 1323 | + () => assert.throws(() => { throw [1, 2]; }, RangeError), | ||
| 1324 | + { | ||
| 1325 | + message: 'The error is expected to be an instance of "RangeError". ' + | ||
| 1326 | + 'Received "[Array]"' | ||
| 1327 | + } | ||
| 1328 | + ); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments