MCPcopy
hub / github.com/jestjs/jest / createToHaveBeenLastCalledWithMatcher

Function createToHaveBeenLastCalledWithMatcher

packages/expect/src/spyMatchers.ts:760–856  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

758 };
759
760const createToHaveBeenLastCalledWithMatcher = (): MatcherFunction<
761 Array<unknown>
762> =>
763 function (received: any, ...expected): SyncExpectationResult {
764 const expectedArgument = '...expected';
765 const options: MatcherHintOptions = {
766 isNot: this.isNot,
767 promise: this.promise,
768 };
769 ensureMockOrSpy(
770 received,
771 'toHaveBeenLastCalledWith',
772 expectedArgument,
773 options,
774 );
775
776 const receivedIsSpy = isSpy(received);
777 const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
778
779 const calls = receivedIsSpy
780 ? received.calls.all().map((x: any) => x.args)
781 : received.mock.calls;
782 const iLast = calls.length - 1;
783
784 const pass = iLast >= 0 && isEqualCall(expected, calls[iLast]);
785
786 const message = pass
787 ? () => {
788 const indexedCalls: Array<IndexedCall> = [];
789 if (iLast > 0) {
790 // Display preceding call as context.
791 indexedCalls.push([iLast - 1, calls[iLast - 1]]);
792 }
793 indexedCalls.push([iLast, calls[iLast]]);
794
795 return (
796 // eslint-disable-next-line prefer-template
797 matcherHint(
798 'toHaveBeenLastCalledWith',
799 receivedName,
800 expectedArgument,
801 options,
802 ) +
803 '\n\n' +
804 `Expected: not ${printExpectedArgs(expected)}\n` +
805 (calls.length === 1 && stringify(calls[0]) === stringify(expected)
806 ? ''
807 : printReceivedCallsNegative(
808 expected,
809 indexedCalls,
810 calls.length === 1,
811 iLast,
812 )) +
813 `\nNumber of calls: ${printReceived(calls.length)}`
814 );
815 }
816 : () => {
817 const indexedCalls: Array<IndexedCall> = [];

Callers 1

spyMatchers.tsFile · 0.85

Calls 11

matcherHintFunction · 0.90
stringifyFunction · 0.90
printReceivedFunction · 0.90
ensureMockOrSpyFunction · 0.85
isEqualCallFunction · 0.85
printExpectedArgsFunction · 0.85
getMockNameMethod · 0.80
isSpyFunction · 0.70
isExpandFunction · 0.70

Tested by

no test coverage detected