()
| 1097 | }; |
| 1098 | |
| 1099 | const createToHaveNthReturnedWithMatcher = (): MatcherFunction< |
| 1100 | [number, unknown] |
| 1101 | > => |
| 1102 | function (received: any, nth, expected): SyncExpectationResult { |
| 1103 | const expectedArgument = 'n'; |
| 1104 | const options: MatcherHintOptions = { |
| 1105 | expectedColor: (arg: string) => arg, |
| 1106 | isNot: this.isNot, |
| 1107 | promise: this.promise, |
| 1108 | secondArgument: 'expected', |
| 1109 | }; |
| 1110 | ensureMock(received, 'toHaveNthReturnedWith', expectedArgument, options); |
| 1111 | |
| 1112 | if (!Number.isSafeInteger(nth) || nth < 1) { |
| 1113 | throw new Error( |
| 1114 | matcherErrorMessage( |
| 1115 | matcherHint( |
| 1116 | 'toHaveNthReturnedWith', |
| 1117 | undefined, |
| 1118 | expectedArgument, |
| 1119 | options, |
| 1120 | ), |
| 1121 | `${expectedArgument} must be a positive integer`, |
| 1122 | printWithType(expectedArgument, nth, stringify), |
| 1123 | ), |
| 1124 | ); |
| 1125 | } |
| 1126 | |
| 1127 | const receivedName = received.getMockName(); |
| 1128 | const {calls, results} = received.mock; |
| 1129 | const length = results.length; |
| 1130 | const iNth = nth - 1; |
| 1131 | |
| 1132 | const pass = iNth < length && isEqualReturn(expected, results[iNth]); |
| 1133 | |
| 1134 | const message = pass |
| 1135 | ? () => { |
| 1136 | // Display preceding and following results, |
| 1137 | // in case assertions fails because index is off by one. |
| 1138 | const indexedResults: Array<IndexedResult> = []; |
| 1139 | if (iNth - 1 >= 0) { |
| 1140 | indexedResults.push([iNth - 1, results[iNth - 1]]); |
| 1141 | } |
| 1142 | indexedResults.push([iNth, results[iNth]]); |
| 1143 | if (iNth + 1 < length) { |
| 1144 | indexedResults.push([iNth + 1, results[iNth + 1]]); |
| 1145 | } |
| 1146 | |
| 1147 | return ( |
| 1148 | // eslint-disable-next-line prefer-template |
| 1149 | matcherHint( |
| 1150 | 'toHaveNthReturnedWith', |
| 1151 | receivedName, |
| 1152 | expectedArgument, |
| 1153 | options, |
| 1154 | ) + |
| 1155 | '\n\n' + |
| 1156 | `n: ${nth}\n` + |
no test coverage detected