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

Function printExpectedReceivedCallsPositive

packages/expect/src/spyMatchers.ts:139–217  ·  view source on GitHub ↗
(
  expected: Array<unknown>,
  indexedCalls: Array<IndexedCall>,
  expand: boolean,
  isOnlyCall: boolean,
  iExpectedCall?: number,
)

Source from the content-addressed store, hash-verified

137};
138
139const printExpectedReceivedCallsPositive = (
140 expected: Array<unknown>,
141 indexedCalls: Array<IndexedCall>,
142 expand: boolean,
143 isOnlyCall: boolean,
144 iExpectedCall?: number,
145) => {
146 const expectedLine = `Expected: ${printExpectedArgs(expected)}\n`;
147 if (indexedCalls.length === 0) {
148 return expectedLine;
149 }
150
151 const label = 'Received: ';
152 if (isOnlyCall && (iExpectedCall === 0 || iExpectedCall === undefined)) {
153 const received = indexedCalls[0][1];
154
155 if (isLineDiffableCall(expected, received)) {
156 // Display diff without indentation.
157 const lines = [
158 EXPECTED_COLOR('- Expected'),
159 RECEIVED_COLOR('+ Received'),
160 '',
161 ];
162
163 const length = Math.max(expected.length, received.length);
164 for (let i = 0; i < length; i += 1) {
165 if (i < expected.length && i < received.length) {
166 if (isEqualValue(expected[i], received[i])) {
167 lines.push(` ${printCommon(received[i])},`);
168 continue;
169 }
170
171 if (isLineDiffableArg(expected[i], received[i])) {
172 const difference = diff(expected[i], received[i], {expand});
173 if (
174 typeof difference === 'string' &&
175 difference.includes('- Expected') &&
176 difference.includes('+ Received')
177 ) {
178 // Omit annotation in case multiple args have diff.
179 lines.push(`${difference.split('\n').slice(3).join('\n')},`);
180 continue;
181 }
182 }
183 }
184
185 if (i < expected.length) {
186 lines.push(`${EXPECTED_COLOR(`- ${stringify(expected[i])}`)},`);
187 }
188 if (i < received.length) {
189 lines.push(`${RECEIVED_COLOR(`+ ${stringify(received[i])}`)},`);
190 }
191 }
192
193 return `${lines.join('\n')}\n`;
194 }
195
196 return `${expectedLine + label + printReceivedArgs(received, expected)}\n`;

Calls 10

diffFunction · 0.90
stringifyFunction · 0.90
printExpectedArgsFunction · 0.85
isLineDiffableCallFunction · 0.85
isEqualValueFunction · 0.85
printCommonFunction · 0.85
isLineDiffableArgFunction · 0.85
printReceivedArgsFunction · 0.85
getRightAlignedPrinterFunction · 0.85
printDiffCallFunction · 0.85

Tested by

no test coverage detected