| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a10856a commit e13d1df
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -101,7 +101,9 @@ assert.deepEqual(obj1, obj4); | |||
| 101 | 101 | ||
| 102 | 102 | If the values are not equal, an `AssertionError` is thrown with a `message` | |
| 103 | 103 | property set equal to the value of the `message` parameter. If the `message` | |
| 104 | - parameter is undefined, a default error message is assigned. | ||
| 104 | + parameter is undefined, a default error message is assigned. If the `message` | ||
| 105 | + parameter is an instance of an `Error` then it will be thrown instead of the | ||
| 106 | + `AssertionError`. | ||
| 105 | 107 | ||
| 106 | 108 | ## assert.deepStrictEqual(actual, expected[, message]) | |
| 107 | 109 | <!-- YAML | |
@@ -176,7 +178,9 @@ assert.deepStrictEqual(NaN, NaN); | |||
| 176 | 178 | ||
| 177 | 179 | If the values are not equal, an `AssertionError` is thrown with a `message` | |
| 178 | 180 | property set equal to the value of the `message` parameter. If the `message` | |
| 179 | - parameter is undefined, a default error message is assigned. | ||
| 181 | + parameter is undefined, a default error message is assigned. If the `message` | ||
| 182 | + parameter is an instance of an `Error` then it will be thrown instead of the | ||
| 183 | + `AssertionError`. | ||
| 180 | 184 | ||
| 181 | 185 | ## assert.doesNotThrow(block[, error][, message]) | |
| 182 | 186 | <!-- YAML | |
@@ -270,7 +274,9 @@ assert.equal({ a: { b: 1 } }, { a: { b: 1 } }); | |||
| 270 | 274 | ||
| 271 | 275 | If the values are not equal, an `AssertionError` is thrown with a `message` | |
| 272 | 276 | property set equal to the value of the `message` parameter. If the `message` | |
| 273 | - parameter is undefined, a default error message is assigned. | ||
| 277 | + parameter is undefined, a default error message is assigned. If the `message` | ||
| 278 | + parameter is an instance of an `Error` then it will be thrown instead of the | ||
| 279 | + `AssertionError`. | ||
| 274 | 280 | ||
| 275 | 281 | ## assert.fail([message]) | |
| 276 | 282 | ## assert.fail(actual, expected[, message[, operator[, stackStartFunction]]]) | |
@@ -284,13 +290,15 @@ added: v0.1.21 | |||
| 284 | 290 | * `stackStartFunction` {function} (default: `assert.fail`) | |
| 285 | 291 | ||
| 286 | 292 | Throws an `AssertionError`. If `message` is falsy, the error message is set as | |
| 287 | - the values of `actual` and `expected` separated by the provided `operator`. | ||
| 288 | - If just the two `actual` and `expected` arguments are provided, `operator` will | ||
| 289 | - default to `'!='`. If `message` is provided only it will be used as the error | ||
| 290 | - message, the other arguments will be stored as properties on the thrown object. | ||
| 291 | - If `stackStartFunction` is provided, all stack frames above that function will | ||
| 292 | - be removed from stacktrace (see [`Error.captureStackTrace`]). If no arguments | ||
| 293 | - are given, the default message `Failed` will be used. | ||
| 293 | + the values of `actual` and `expected` separated by the provided `operator`. If | ||
| 294 | + the `message` parameter is an instance of an `Error` then it will be thrown | ||
| 295 | + instead of the `AssertionError`. If just the two `actual` and `expected` | ||
| 296 | + arguments are provided, `operator` will default to `'!='`. If `message` is | ||
| 297 | + provided only it will be used as the error message, the other arguments will be | ||
| 298 | + stored as properties on the thrown object. If `stackStartFunction` is provided, | ||
| 299 | + all stack frames above that function will be removed from stacktrace (see | ||
| 300 | + [`Error.captureStackTrace`]). If no arguments are given, the default message | ||
| 301 | + `Failed` will be used. | ||
| 294 | 302 | ||
| 295 | 303 | ```js | |
| 296 | 304 | const assert = require('assert'); | |
@@ -303,6 +311,9 @@ assert.fail(1, 2, 'fail'); | |||
| 303 | 311 | ||
| 304 | 312 | assert.fail(1, 2, 'whoops', '>'); | |
| 305 | 313 | // AssertionError [ERR_ASSERTION]: whoops | |
| 314 | + | ||
| 315 | + assert.fail(1, 2, new TypeError('need array')); | ||
| 316 | + // TypeError: need array | ||
| 306 | 317 | ``` | |
| 307 | 318 | ||
| 308 | 319 | *Note*: Is the last two cases `actual`, `expected`, and `operator` have no | |
@@ -414,7 +425,9 @@ assert.notDeepEqual(obj1, obj4); | |||
| 414 | 425 | ||
| 415 | 426 | If the values are deeply equal, an `AssertionError` is thrown with a `message` | |
| 416 | 427 | property set equal to the value of the `message` parameter. If the `message` | |
| 417 | - parameter is undefined, a default error message is assigned. | ||
| 428 | + parameter is undefined, a default error message is assigned. If the `message` | ||
| 429 | + parameter is an instance of an `Error` then it will be thrown instead of the | ||
| 430 | + `AssertionError`. | ||
| 418 | 431 | ||
| 419 | 432 | ## assert.notDeepStrictEqual(actual, expected[, message]) | |
| 420 | 433 | <!-- YAML | |
@@ -455,9 +468,11 @@ assert.notDeepStrictEqual({ a: 1 }, { a: '1' }); | |||
| 455 | 468 | // OK | |
| 456 | 469 | ``` | |
| 457 | 470 | ||
| 458 | - If the values are deeply and strictly equal, an `AssertionError` is thrown | ||
| 459 | - with a `message` property set equal to the value of the `message` parameter. If | ||
| 460 | - the `message` parameter is undefined, a default error message is assigned. | ||
| 471 | + If the values are deeply and strictly equal, an `AssertionError` is thrown with | ||
| 472 | + a `message` property set equal to the value of the `message` parameter. If the | ||
| 473 | + `message` parameter is undefined, a default error message is assigned. If the | ||
| 474 | + `message` parameter is an instance of an `Error` then it will be thrown instead | ||
| 475 | + of the `AssertionError`. | ||
| 461 | 476 | ||
| 462 | 477 | ## assert.notEqual(actual, expected[, message]) | |
| 463 | 478 | <!-- YAML | |
@@ -485,7 +500,9 @@ assert.notEqual(1, '1'); | |||
| 485 | 500 | ||
| 486 | 501 | If the values are equal, an `AssertionError` is thrown with a `message` | |
| 487 | 502 | property set equal to the value of the `message` parameter. If the `message` | |
| 488 | - parameter is undefined, a default error message is assigned. | ||
| 503 | + parameter is undefined, a default error message is assigned. If the `message` | ||
| 504 | + parameter is an instance of an `Error` then it will be thrown instead of the | ||
| 505 | + `AssertionError`. | ||
| 489 | 506 | ||
| 490 | 507 | ## assert.notStrictEqual(actual, expected[, message]) | |
| 491 | 508 | <!-- YAML | |
@@ -513,7 +530,9 @@ assert.notStrictEqual(1, '1'); | |||
| 513 | 530 | ||
| 514 | 531 | If the values are strictly equal, an `AssertionError` is thrown with a | |
| 515 | 532 | `message` property set equal to the value of the `message` parameter. If the | |
| 516 | - `message` parameter is undefined, a default error message is assigned. | ||
| 533 | + `message` parameter is undefined, a default error message is assigned. If the | ||
| 534 | + `message` parameter is an instance of an `Error` then it will be thrown instead | ||
| 535 | + of the `AssertionError`. | ||
| 517 | 536 | ||
| 518 | 537 | ## assert.ok(value[, message]) | |
| 519 | 538 | <!-- YAML | |
@@ -527,7 +546,9 @@ Tests if `value` is truthy. It is equivalent to | |||
| 527 | 546 | ||
| 528 | 547 | If `value` is not truthy, an `AssertionError` is thrown with a `message` | |
| 529 | 548 | property set equal to the value of the `message` parameter. If the `message` | |
| 530 | - parameter is `undefined`, a default error message is assigned. | ||
| 549 | + parameter is `undefined`, a default error message is assigned. If the `message` | ||
| 550 | + parameter is an instance of an `Error` then it will be thrown instead of the | ||
| 551 | + `AssertionError`. | ||
| 531 | 552 | ||
| 532 | 553 | ```js | |
| 533 | 554 | const assert = require('assert'); | |
@@ -570,7 +591,9 @@ assert.strictEqual(1, '1'); | |||
| 570 | 591 | ||
| 571 | 592 | If the values are not strictly equal, an `AssertionError` is thrown with a | |
| 572 | 593 | `message` property set equal to the value of the `message` parameter. If the | |
| 573 | - `message` parameter is undefined, a default error message is assigned. | ||
| 594 | + `message` parameter is undefined, a default error message is assigned. If the | ||
| 595 | + `message` parameter is an instance of an `Error` then it will be thrown instead | ||
| 596 | + of the `AssertionError`. | ||
| 574 | 597 | ||
| 575 | 598 | ## assert.throws(block[, error][, message]) | |
| 576 | 599 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -38,6 +38,8 @@ const assert = module.exports = ok; | |||
| 38 | 38 | // display purposes. | |
| 39 | 39 | ||
| 40 | 40 | function innerFail(actual, expected, message, operator, stackStartFunction) { | |
| 41 | + if (message instanceof Error) throw message; | ||
| 42 | + | ||
| 41 | 43 | throw new errors.AssertionError({ | |
| 42 | 44 | message, | |
| 43 | 45 | actual, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,6 +28,17 @@ common.expectsError(() => { | |||
| 28 | 28 | expected: undefined | |
| 29 | 29 | }); | |
| 30 | 30 | ||
| 31 | + // One arg = Error | ||
| 32 | + common.expectsError(() => { | ||
| 33 | + assert.fail(new TypeError('custom message')); | ||
| 34 | + }, { | ||
| 35 | + type: TypeError, | ||
| 36 | + message: 'custom message', | ||
| 37 | + operator: undefined, | ||
| 38 | + actual: undefined, | ||
| 39 | + expected: undefined | ||
| 40 | + }); | ||
| 41 | + | ||
| 31 | 42 | // Two args only, operator defaults to '!=' | |
| 32 | 43 | common.expectsError(() => { | |
| 33 | 44 | assert.fail('first', 'second'); | |
@@ -52,6 +63,17 @@ common.expectsError(() => { | |||
| 52 | 63 | expected: 'ignored' | |
| 53 | 64 | }); | |
| 54 | 65 | ||
| 66 | + // Three args with custom Error | ||
| 67 | + common.expectsError(() => { | ||
| 68 | + assert.fail(typeof 1, 'object', new TypeError('another custom message')); | ||
| 69 | + }, { | ||
| 70 | + type: TypeError, | ||
| 71 | + message: 'another custom message', | ||
| 72 | + operator: undefined, | ||
| 73 | + actual: 'number', | ||
| 74 | + expected: 'object' | ||
| 75 | + }); | ||
| 76 | + | ||
| 55 | 77 | // No third arg (but a fourth arg) | |
| 56 | 78 | common.expectsError(() => { | |
| 57 | 79 | assert.fail('first', 'second', undefined, 'operator'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -645,6 +645,34 @@ try { | |||
| 645 | 645 | 'Message incorrectly marked as generated'); | |
| 646 | 646 | } | |
| 647 | 647 | ||
| 648 | + { | ||
| 649 | + let threw = false; | ||
| 650 | + const rangeError = new RangeError('my range'); | ||
| 651 | + | ||
| 652 | + // verify custom errors | ||
| 653 | + try { | ||
| 654 | + assert.strictEqual(1, 2, rangeError); | ||
| 655 | + } catch (e) { | ||
| 656 | + assert.strictEqual(e, rangeError); | ||
| 657 | + threw = true; | ||
| 658 | + assert.ok(e instanceof RangeError, 'Incorrect error type thrown'); | ||
| 659 | + } | ||
| 660 | + assert.ok(threw); | ||
| 661 | + threw = false; | ||
| 662 | + | ||
| 663 | + // verify AssertionError is the result from doesNotThrow with custom Error | ||
| 664 | + try { | ||
| 665 | + assert.doesNotThrow(() => { | ||
| 666 | + throw new TypeError('wrong type'); | ||
| 667 | + }, TypeError, rangeError); | ||
| 668 | + } catch (e) { | ||
| 669 | + threw = true; | ||
| 670 | + assert.ok(e.message.includes(rangeError.message)); | ||
| 671 | + assert.ok(e instanceof assert.AssertionError); | ||
| 672 | + } | ||
| 673 | + assert.ok(threw); | ||
| 674 | + } | ||
| 675 | + | ||
| 648 | 676 | { | |
| 649 | 677 | // Verify that throws() and doesNotThrow() throw on non-function block | |
| 650 | 678 | function typeName(value) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments