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

Function createToHaveBeenNthCalledWithMatcher

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

Source from the content-addressed store, hash-verified

948 };
949
950const createToHaveBeenNthCalledWithMatcher = (): MatcherFunction<
951 [number, ...Array<unknown>]
952> =>
953 function (received: any, nth, ...expected): SyncExpectationResult {
954 const expectedArgument = 'n';
955 const options: MatcherHintOptions = {
956 expectedColor: (arg: string) => arg,
957 isNot: this.isNot,
958 promise: this.promise,
959 secondArgument: '...expected',
960 };
961 ensureMockOrSpy(
962 received,
963 'toHaveBeenNthCalledWith',
964 expectedArgument,
965 options,
966 );
967
968 if (!Number.isSafeInteger(nth) || nth < 1) {
969 throw new Error(
970 matcherErrorMessage(
971 matcherHint(
972 'toHaveBeenNthCalledWith',
973 undefined,
974 expectedArgument,
975 options,
976 ),
977 `${expectedArgument} must be a positive integer`,
978 printWithType(expectedArgument, nth, stringify),
979 ),
980 );
981 }
982
983 const receivedIsSpy = isSpy(received);
984 const receivedName = receivedIsSpy ? 'spy' : received.getMockName();
985
986 const calls = receivedIsSpy
987 ? received.calls.all().map((x: any) => x.args)
988 : received.mock.calls;
989 const length = calls.length;
990 const iNth = nth - 1;
991
992 const pass = iNth < length && isEqualCall(expected, calls[iNth]);
993
994 const message = pass
995 ? () => {
996 // Display preceding and following calls,
997 // in case assertions fails because index is off by one.
998 const indexedCalls: Array<IndexedCall> = [];
999 if (iNth - 1 >= 0) {
1000 indexedCalls.push([iNth - 1, calls[iNth - 1]]);
1001 }
1002 indexedCalls.push([iNth, calls[iNth]]);
1003 if (iNth + 1 < length) {
1004 indexedCalls.push([iNth + 1, calls[iNth + 1]]);
1005 }
1006
1007 return (

Callers 1

spyMatchers.tsFile · 0.85

Calls 13

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

Tested by

no test coverage detected