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

Function createToHaveLastReturnedWithMatcher

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

Source from the content-addressed store, hash-verified

856 };
857
858const createToHaveLastReturnedWithMatcher = (): MatcherFunction<[unknown]> =>
859 function (received: any, expected): SyncExpectationResult {
860 const expectedArgument = 'expected';
861 const options: MatcherHintOptions = {
862 isNot: this.isNot,
863 promise: this.promise,
864 };
865 ensureMock(received, 'toHaveLastReturnedWith', expectedArgument, options);
866
867 const receivedName = received.getMockName();
868
869 const {calls, results} = received.mock;
870 const iLast = results.length - 1;
871
872 const pass = iLast >= 0 && isEqualReturn(expected, results[iLast]);
873
874 const message = pass
875 ? () => {
876 const indexedResults: Array<IndexedResult> = [];
877 if (iLast > 0) {
878 // Display preceding result as context.
879 indexedResults.push([iLast - 1, results[iLast - 1]]);
880 }
881 indexedResults.push([iLast, results[iLast]]);
882
883 return (
884 // eslint-disable-next-line prefer-template
885 matcherHint(
886 'toHaveLastReturnedWith',
887 receivedName,
888 expectedArgument,
889 options,
890 ) +
891 '\n\n' +
892 `Expected: not ${printExpected(expected)}\n` +
893 (results.length === 1 &&
894 results[0].type === 'return' &&
895 stringify(results[0].value) === stringify(expected)
896 ? ''
897 : printReceivedResults(
898 'Received: ',
899 expected,
900 indexedResults,
901 results.length === 1,
902 iLast,
903 )) +
904 printNumberOfReturns(countReturns(results), calls.length)
905 );
906 }
907 : () => {
908 const indexedResults: Array<IndexedResult> = [];
909 if (iLast >= 0) {
910 if (iLast > 0) {
911 let i = iLast - 1;
912 // Is there a preceding result that is equal to expected value?
913 while (i >= 0 && !isEqualReturn(expected, results[i])) {
914 i -= 1;
915 }

Callers 1

spyMatchers.tsFile · 0.85

Calls 9

matcherHintFunction · 0.90
printExpectedFunction · 0.90
stringifyFunction · 0.90
ensureMockFunction · 0.85
isEqualReturnFunction · 0.85
printReceivedResultsFunction · 0.85
printNumberOfReturnsFunction · 0.85
countReturnsFunction · 0.85
getMockNameMethod · 0.80

Tested by

no test coverage detected