(
received: object,
expectedPath: string | Array<string>,
expectedValue?: unknown,
)
| 709 | }, |
| 710 | |
| 711 | toHaveProperty( |
| 712 | received: object, |
| 713 | expectedPath: string | Array<string>, |
| 714 | expectedValue?: unknown, |
| 715 | ) { |
| 716 | const matcherName = 'toHaveProperty'; |
| 717 | const expectedArgument = 'path'; |
| 718 | const hasValue = arguments.length === 3; |
| 719 | const options: MatcherHintOptions = { |
| 720 | isNot: this.isNot, |
| 721 | promise: this.promise, |
| 722 | secondArgument: hasValue ? 'value' : '', |
| 723 | }; |
| 724 | |
| 725 | if (received === null || received === undefined) { |
| 726 | throw new Error( |
| 727 | matcherErrorMessage( |
| 728 | matcherHint(matcherName, undefined, expectedArgument, options), |
| 729 | `${RECEIVED_COLOR('received')} value must not be null nor undefined`, |
| 730 | printWithType('Received', received, printReceived), |
| 731 | ), |
| 732 | ); |
| 733 | } |
| 734 | |
| 735 | const expectedPathType = getType(expectedPath); |
| 736 | |
| 737 | if (expectedPathType !== 'string' && expectedPathType !== 'array') { |
| 738 | throw new Error( |
| 739 | matcherErrorMessage( |
| 740 | matcherHint(matcherName, undefined, expectedArgument, options), |
| 741 | `${EXPECTED_COLOR('expected')} path must be a string or array`, |
| 742 | printWithType('Expected', expectedPath, printExpected), |
| 743 | ), |
| 744 | ); |
| 745 | } |
| 746 | |
| 747 | const expectedPathLength = |
| 748 | typeof expectedPath === 'string' |
| 749 | ? pathAsArray(expectedPath).length |
| 750 | : expectedPath.length; |
| 751 | |
| 752 | if (expectedPathType === 'array' && expectedPathLength === 0) { |
| 753 | throw new Error( |
| 754 | matcherErrorMessage( |
| 755 | matcherHint(matcherName, undefined, expectedArgument, options), |
| 756 | `${EXPECTED_COLOR('expected')} path must not be an empty array`, |
| 757 | printWithType('Expected', expectedPath, printExpected), |
| 758 | ), |
| 759 | ); |
| 760 | } |
| 761 | |
| 762 | const result = getPath(received, expectedPath); |
| 763 | const {lastTraversedObject, endPropIsDefined, hasEndProp, value} = result; |
| 764 | const receivedPath = result.traversedPath; |
| 765 | const hasCompletePath = receivedPath.length === expectedPathLength; |
| 766 | const receivedValue = hasCompletePath ? result.value : lastTraversedObject; |
| 767 | |
| 768 | const pass = |
nothing calls this directly
no test coverage detected