| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8157a50 commit ef8f147
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -549,30 +549,38 @@ function compareExceptionKey(actual, expected, key, message, keys, fn) { | |||
| 549 | 549 | } | |
| 550 | 550 | } | |
| 551 | 551 | ||
| 552 | - function expectedException(actual, expected, msg, fn) { | ||
| 552 | + function expectedException(actual, expected, message, fn) { | ||
| 553 | 553 | if (typeof expected !== 'function') { | |
| 554 | - if (isRegExp(expected)) | ||
| 555 | - return expected.test(actual); | ||
| 556 | - // assert.doesNotThrow does not accept objects. | ||
| 557 | - if (arguments.length === 2) { | ||
| 558 | - throw new ERR_INVALID_ARG_TYPE( | ||
| 559 | - 'expected', ['Function', 'RegExp'], expected | ||
| 560 | - ); | ||
| 554 | + // Handle regular expressions. | ||
| 555 | + if (isRegExp(expected)) { | ||
| 556 | + const str = String(actual); | ||
| 557 | + if (expected.test(str)) | ||
| 558 | + return; | ||
| 559 | + | ||
| 560 | + throw new AssertionError({ | ||
| 561 | + actual, | ||
| 562 | + expected, | ||
| 563 | + message: message || 'The input did not match the regular expression ' + | ||
| 564 | + `${inspect(expected)}. Input:\n\n${inspect(str)}\n`, | ||
| 565 | + operator: fn.name, | ||
| 566 | + stackStartFn: fn | ||
| 567 | + }); | ||
| 561 | 568 | } | |
| 562 | 569 | ||
| 563 | 570 | // Handle primitives properly. | |
| 564 | 571 | if (typeof actual !== 'object' || actual === null) { | |
| 565 | 572 | const err = new AssertionError({ | |
| 566 | 573 | actual, | |
| 567 | 574 | expected, | |
| 568 | - message: msg, | ||
| 575 | + message, | ||
| 569 | 576 | operator: 'deepStrictEqual', | |
| 570 | 577 | stackStartFn: fn | |
| 571 | 578 | }); | |
| 572 | 579 | err.operator = fn.name; | |
| 573 | 580 | throw err; | |
| 574 | 581 | } | |
| 575 | 582 | ||
| 583 | + // Handle validation objects. | ||
| 576 | 584 | const keys = Object.keys(expected); | |
| 577 | 585 | // Special handle errors to make sure the name and the message are compared | |
| 578 | 586 | // as well. | |
@@ -589,18 +597,25 @@ function expectedException(actual, expected, msg, fn) { | |||
| 589 | 597 | expected[key].test(actual[key])) { | |
| 590 | 598 | continue; | |
| 591 | 599 | } | |
| 592 | - compareExceptionKey(actual, expected, key, msg, keys, fn); | ||
| 600 | + compareExceptionKey(actual, expected, key, message, keys, fn); | ||
| 593 | 601 | } | |
| 594 | - return true; | ||
| 602 | + return; | ||
| 595 | 603 | } | |
| 604 | + | ||
| 596 | 605 | // Guard instanceof against arrow functions as they don't have a prototype. | |
| 606 | + // Check for matching Error classes. | ||
| 597 | 607 | if (expected.prototype !== undefined && actual instanceof expected) { | |
| 598 | - return true; | ||
| 608 | + return; | ||
| 599 | 609 | } | |
| 600 | 610 | if (Error.isPrototypeOf(expected)) { | |
| 601 | - return false; | ||
| 611 | + throw actual; | ||
| 612 | + } | ||
| 613 | + | ||
| 614 | + // Check validation functions return value. | ||
| 615 | + const res = expected.call({}, actual); | ||
| 616 | + if (res !== true) { | ||
| 617 | + throw actual; | ||
| 602 | 618 | } | |
| 603 | - return expected.call({}, actual) === true; | ||
| 604 | 619 | } | |
| 605 | 620 | ||
| 606 | 621 | function getActual(fn) { | |
@@ -695,9 +710,31 @@ function expectsError(stackStartFn, actual, error, message) { | |||
| 695 | 710 | stackStartFn | |
| 696 | 711 | }); | |
| 697 | 712 | } | |
| 698 | - if (error && !expectedException(actual, error, message, stackStartFn)) { | ||
| 699 | - throw actual; | ||
| 713 | + | ||
| 714 | + if (!error) | ||
| 715 | + return; | ||
| 716 | + | ||
| 717 | + expectedException(actual, error, message, stackStartFn); | ||
| 718 | + } | ||
| 719 | + | ||
| 720 | + function hasMatchingError(actual, expected) { | ||
| 721 | + if (typeof expected !== 'function') { | ||
| 722 | + if (isRegExp(expected)) { | ||
| 723 | + const str = String(actual); | ||
| 724 | + return expected.test(str); | ||
| 725 | + } | ||
| 726 | + throw new ERR_INVALID_ARG_TYPE( | ||
| 727 | + 'expected', ['Function', 'RegExp'], expected | ||
| 728 | + ); | ||
| 700 | 729 | } | |
| 730 | + // Guard instanceof against arrow functions as they don't have a prototype. | ||
| 731 | + if (expected.prototype !== undefined && actual instanceof expected) { | ||
| 732 | + return true; | ||
| 733 | + } | ||
| 734 | + if (Error.isPrototypeOf(expected)) { | ||
| 735 | + return false; | ||
| 736 | + } | ||
| 737 | + return expected.call({}, actual) === true; | ||
| 701 | 738 | } | |
| 702 | 739 | ||
| 703 | 740 | function expectsNoError(stackStartFn, actual, error, message) { | |
@@ -709,7 +746,7 @@ function expectsNoError(stackStartFn, actual, error, message) { | |||
| 709 | 746 | error = undefined; | |
| 710 | 747 | } | |
| 711 | 748 | ||
| 712 | - if (!error || expectedException(actual, error)) { | ||
| 749 | + if (!error || hasMatchingError(actual, error)) { | ||
| 713 | 750 | const details = message ? `: ${message}` : '.'; | |
| 714 | 751 | const fnType = stackStartFn.name === 'doesNotReject' ? | |
| 715 | 752 | 'rejection' : 'exception'; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -182,7 +182,27 @@ assert.throws( | |||
| 182 | 182 | } | |
| 183 | 183 | ||
| 184 | 184 | // Use a RegExp to validate the error message. | |
| 185 | - a.throws(() => thrower(TypeError), /\[object Object\]/); | ||
| 185 | + { | ||
| 186 | + a.throws(() => thrower(TypeError), /\[object Object\]/); | ||
| 187 | + | ||
| 188 | + const symbol = Symbol('foo'); | ||
| 189 | + a.throws(() => { | ||
| 190 | + throw symbol; | ||
| 191 | + }, /foo/); | ||
| 192 | + | ||
| 193 | + a.throws(() => { | ||
| 194 | + a.throws(() => { | ||
| 195 | + throw symbol; | ||
| 196 | + }, /abc/); | ||
| 197 | + }, { | ||
| 198 | + message: 'The input did not match the regular expression /abc/. ' + | ||
| 199 | + "Input:\n\n'Symbol(foo)'\n", | ||
| 200 | + code: 'ERR_ASSERTION', | ||
| 201 | + operator: 'throws', | ||
| 202 | + actual: symbol, | ||
| 203 | + expected: /abc/ | ||
| 204 | + }); | ||
| 205 | + } | ||
| 186 | 206 | ||
| 187 | 207 | // Use a fn to validate the error object. | |
| 188 | 208 | a.throws(() => thrower(TypeError), (err) => { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments