| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 749bc16 commit 7d6c963
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,6 +25,7 @@ const { | |||
| 25 | 25 | ObjectAssign, | |
| 26 | 26 | ObjectIs, | |
| 27 | 27 | ObjectKeys, | |
| 28 | + ObjectPrototypeIsPrototypeOf, | ||
| 28 | 29 | Map, | |
| 29 | 30 | } = primordials; | |
| 30 | 31 | ||
@@ -571,6 +572,9 @@ function compareExceptionKey(actual, expected, key, message, keys, fn) { | |||
| 571 | 572 | } | |
| 572 | 573 | ||
| 573 | 574 | function expectedException(actual, expected, message, fn) { | |
| 575 | + let generatedMessage = false; | ||
| 576 | + let throwError = false; | ||
| 577 | + | ||
| 574 | 578 | if (typeof expected !== 'function') { | |
| 575 | 579 | // Handle regular expressions. | |
| 576 | 580 | if (isRegExp(expected)) { | |
@@ -583,20 +587,9 @@ function expectedException(actual, expected, message, fn) { | |||
| 583 | 587 | message = 'The input did not match the regular expression ' + | |
| 584 | 588 | `${inspect(expected)}. Input:\n\n${inspect(str)}\n`; | |
| 585 | 589 | } | |
| 586 | - | ||
| 587 | - const err = new AssertionError({ | ||
| 588 | - actual, | ||
| 589 | - expected, | ||
| 590 | - message, | ||
| 591 | - operator: fn.name, | ||
| 592 | - stackStartFn: fn | ||
| 593 | - }); | ||
| 594 | - err.generatedMessage = generatedMessage; | ||
| 595 | - throw err; | ||
| 596 | - } | ||
| 597 | - | ||
| 598 | - // Handle primitives properly. | ||
| 599 | - if (typeof actual !== 'object' || actual === null) { | ||
| 590 | + throwError = true; | ||
| 591 | + // Handle primitives properly. | ||
| 592 | + } else if (typeof actual !== 'object' || actual === null) { | ||
| 600 | 593 | const err = new AssertionError({ | |
| 601 | 594 | actual, | |
| 602 | 595 | expected, | |
@@ -606,43 +599,52 @@ function expectedException(actual, expected, message, fn) { | |||
| 606 | 599 | }); | |
| 607 | 600 | err.operator = fn.name; | |
| 608 | 601 | throw err; | |
| 609 | - } | ||
| 610 | - | ||
| 611 | - // Handle validation objects. | ||
| 612 | - const keys = ObjectKeys(expected); | ||
| 613 | - // Special handle errors to make sure the name and the message are compared | ||
| 614 | - // as well. | ||
| 615 | - if (expected instanceof Error) { | ||
| 616 | - keys.push('name', 'message'); | ||
| 617 | - } else if (keys.length === 0) { | ||
| 618 | - throw new ERR_INVALID_ARG_VALUE('error', | ||
| 619 | - expected, 'may not be an empty object'); | ||
| 620 | - } | ||
| 621 | - if (isDeepEqual === undefined) lazyLoadComparison(); | ||
| 622 | - for (const key of keys) { | ||
| 623 | - if (typeof actual[key] === 'string' && | ||
| 624 | - isRegExp(expected[key]) && | ||
| 625 | - expected[key].test(actual[key])) { | ||
| 626 | - continue; | ||
| 602 | + } else { | ||
| 603 | + // Handle validation objects. | ||
| 604 | + const keys = ObjectKeys(expected); | ||
| 605 | + // Special handle errors to make sure the name and the message are | ||
| 606 | + // compared as well. | ||
| 607 | + if (expected instanceof Error) { | ||
| 608 | + keys.push('name', 'message'); | ||
| 609 | + } else if (keys.length === 0) { | ||
| 610 | + throw new ERR_INVALID_ARG_VALUE('error', | ||
| 611 | + expected, 'may not be an empty object'); | ||
| 612 | + } | ||
| 613 | + if (isDeepEqual === undefined) lazyLoadComparison(); | ||
| 614 | + for (const key of keys) { | ||
| 615 | + if (typeof actual[key] === 'string' && | ||
| 616 | + isRegExp(expected[key]) && | ||
| 617 | + expected[key].test(actual[key])) { | ||
| 618 | + continue; | ||
| 619 | + } | ||
| 620 | + compareExceptionKey(actual, expected, key, message, keys, fn); | ||
| 627 | 621 | } | |
| 628 | - compareExceptionKey(actual, expected, key, message, keys, fn); | ||
| 622 | + return; | ||
| 629 | 623 | } | |
| 630 | - return; | ||
| 631 | - } | ||
| 632 | - | ||
| 633 | 624 | // Guard instanceof against arrow functions as they don't have a prototype. | |
| 634 | 625 | // Check for matching Error classes. | |
| 635 | - if (expected.prototype !== undefined && actual instanceof expected) { | ||
| 626 | + } else if (expected.prototype !== undefined && actual instanceof expected) { | ||
| 636 | 627 | return; | |
| 637 | - } | ||
| 638 | - if (Error.isPrototypeOf(expected)) { | ||
| 628 | + } else if (ObjectPrototypeIsPrototypeOf(Error, expected)) { | ||
| 639 | 629 | throw actual; | |
| 630 | + } else { | ||
| 631 | + // Check validation functions return value. | ||
| 632 | + const res = expected.call({}, actual); | ||
| 633 | + if (res !== true) { | ||
| 634 | + throw actual; | ||
| 635 | + } | ||
| 640 | 636 | } | |
| 641 | 637 | ||
| 642 | - // Check validation functions return value. | ||
| 643 | - const res = expected.call({}, actual); | ||
| 644 | - if (res !== true) { | ||
| 645 | - throw actual; | ||
| 638 | + if (throwError) { | ||
| 639 | + const err = new AssertionError({ | ||
| 640 | + actual, | ||
| 641 | + expected, | ||
| 642 | + message, | ||
| 643 | + operator: fn.name, | ||
| 644 | + stackStartFn: fn | ||
| 645 | + }); | ||
| 646 | + err.generatedMessage = generatedMessage; | ||
| 647 | + throw err; | ||
| 646 | 648 | } | |
| 647 | 649 | } | |
| 648 | 650 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments