| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d47b678 commit 5700cd1
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -563,6 +563,7 @@ function compareExceptionKey(actual, expected, key, message, keys, fn) { | |||
| 563 | 563 | ||
| 564 | 564 | function expectedException(actual, expected, message, fn) { | |
| 565 | 565 | let generatedMessage = false; | |
| 566 | + let throwError = false; | ||
| 566 | 567 | ||
| 567 | 568 | if (typeof expected !== 'function') { | |
| 568 | 569 | // Handle regular expressions. | |
@@ -576,20 +577,9 @@ function expectedException(actual, expected, message, fn) { | |||
| 576 | 577 | message = 'The input did not match the regular expression ' + | |
| 577 | 578 | `${inspect(expected)}. Input:\n\n${inspect(str)}\n`; | |
| 578 | 579 | } | |
| 579 | - | ||
| 580 | - const err = new AssertionError({ | ||
| 581 | - actual, | ||
| 582 | - expected, | ||
| 583 | - message, | ||
| 584 | - operator: fn.name, | ||
| 585 | - stackStartFn: fn | ||
| 586 | - }); | ||
| 587 | - err.generatedMessage = generatedMessage; | ||
| 588 | - throw err; | ||
| 589 | - } | ||
| 590 | - | ||
| 591 | - // Handle primitives properly. | ||
| 592 | - if (typeof actual !== 'object' || actual === null) { | ||
| 580 | + throwError = true; | ||
| 581 | + // Handle primitives properly. | ||
| 582 | + } else if (typeof actual !== 'object' || actual === null) { | ||
| 593 | 583 | const err = new AssertionError({ | |
| 594 | 584 | actual, | |
| 595 | 585 | expected, | |
@@ -599,36 +589,33 @@ function expectedException(actual, expected, message, fn) { | |||
| 599 | 589 | }); | |
| 600 | 590 | err.operator = fn.name; | |
| 601 | 591 | throw err; | |
| 602 | - } | ||
| 603 | - | ||
| 604 | - // Handle validation objects. | ||
| 605 | - const keys = Object.keys(expected); | ||
| 606 | - // Special handle errors to make sure the name and the message are compared | ||
| 607 | - // as well. | ||
| 608 | - if (expected instanceof Error) { | ||
| 609 | - keys.push('name', 'message'); | ||
| 610 | - } else if (keys.length === 0) { | ||
| 611 | - throw new ERR_INVALID_ARG_VALUE('error', | ||
| 612 | - expected, 'may not be an empty object'); | ||
| 613 | - } | ||
| 614 | - if (isDeepEqual === undefined) lazyLoadComparison(); | ||
| 615 | - for (const key of keys) { | ||
| 616 | - if (typeof actual[key] === 'string' && | ||
| 617 | - isRegExp(expected[key]) && | ||
| 618 | - expected[key].test(actual[key])) { | ||
| 619 | - continue; | ||
| 592 | + } else { | ||
| 593 | + // Handle validation objects. | ||
| 594 | + const keys = Object.keys(expected); | ||
| 595 | + // Special handle errors to make sure the name and the message are | ||
| 596 | + // compared as well. | ||
| 597 | + if (expected instanceof Error) { | ||
| 598 | + keys.push('name', 'message'); | ||
| 599 | + } else if (keys.length === 0) { | ||
| 600 | + throw new ERR_INVALID_ARG_VALUE('error', | ||
| 601 | + expected, 'may not be an empty object'); | ||
| 620 | 602 | } | |
| 621 | - compareExceptionKey(actual, expected, key, message, keys, fn); | ||
| 603 | + if (isDeepEqual === undefined) lazyLoadComparison(); | ||
| 604 | + for (const key of keys) { | ||
| 605 | + if (typeof actual[key] === 'string' && | ||
| 606 | + isRegExp(expected[key]) && | ||
| 607 | + expected[key].test(actual[key])) { | ||
| 608 | + continue; | ||
| 609 | + } | ||
| 610 | + compareExceptionKey(actual, expected, key, message, keys, fn); | ||
| 611 | + } | ||
| 612 | + return; | ||
| 622 | 613 | } | |
| 623 | - return; | ||
| 624 | - } | ||
| 625 | - | ||
| 626 | 614 | // Guard instanceof against arrow functions as they don't have a prototype. | |
| 627 | 615 | // Check for matching Error classes. | |
| 628 | - if (expected.prototype !== undefined && actual instanceof expected) { | ||
| 616 | + } else if (expected.prototype !== undefined && actual instanceof expected) { | ||
| 629 | 617 | return; | |
| 630 | - } | ||
| 631 | - if (ObjectPrototype.isPrototypeOf(Error, expected)) { | ||
| 618 | + } else if (ObjectPrototype.isPrototypeOf(Error, expected)) { | ||
| 632 | 619 | if (!message) { | |
| 633 | 620 | generatedMessage = true; | |
| 634 | 621 | message = 'The error is expected to be an instance of ' + | |
@@ -639,26 +626,22 @@ function expectedException(actual, expected, message, fn) { | |||
| 639 | 626 | message += `"${inspect(actual, { depth: -1 })}"`; | |
| 640 | 627 | } | |
| 641 | 628 | } | |
| 642 | - const err = new AssertionError({ | ||
| 643 | - actual, | ||
| 644 | - expected, | ||
| 645 | - message, | ||
| 646 | - operator: fn.name, | ||
| 647 | - stackStartFn: fn | ||
| 648 | - }); | ||
| 649 | - err.generatedMessage = generatedMessage; | ||
| 650 | - throw err; | ||
| 629 | + throwError = true; | ||
| 630 | + } else { | ||
| 631 | + // Check validation functions return value. | ||
| 632 | + const res = expected.call({}, actual); | ||
| 633 | + if (res !== true) { | ||
| 634 | + if (!message) { | ||
| 635 | + generatedMessage = true; | ||
| 636 | + const name = expected.name ? `"${expected.name}" ` : ''; | ||
| 637 | + message = `The ${name}validation function is expected to return` + | ||
| 638 | + ` "true". Received ${inspect(res)}`; | ||
| 639 | + } | ||
| 640 | + throwError = true; | ||
| 641 | + } | ||
| 651 | 642 | } | |
| 652 | 643 | ||
| 653 | - // Check validation functions return value. | ||
| 654 | - const res = expected.call({}, actual); | ||
| 655 | - if (res !== true) { | ||
| 656 | - if (!message) { | ||
| 657 | - generatedMessage = true; | ||
| 658 | - const name = expected.name ? `"${expected.name}" ` : ''; | ||
| 659 | - message = `The ${name}validation function is expected to return "true".` + | ||
| 660 | - ` Received ${inspect(res)}`; | ||
| 661 | - } | ||
| 644 | + if (throwError) { | ||
| 662 | 645 | const err = new AssertionError({ | |
| 663 | 646 | actual, | |
| 664 | 647 | expected, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments