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

Function printDiffOrStringify

packages/jest-matcher-utils/src/index.ts:320–394  ·  packages/jest-matcher-utils/src/index.ts::printDiffOrStringify
(
  expected: unknown,
  received: unknown,
  expectedLabel: string,
  receivedLabel: string,
  expand: boolean, // CLI options: true if `--expand` or false if `--no-expand`
)

Source from the content-addressed store, hash-verified

318const MAX_DIFF_STRING_LENGTH = 20_000;
319
320export const printDiffOrStringify = (
321 expected: unknown,
322 received: unknown,
323 expectedLabel: string,
324 receivedLabel: string,
325 expand: boolean, class="cm">// CLI options: true if `--expand` or false if `--no-expand`
326): string => {
327 if (
328 typeof expected === class="st">'string' &&
329 typeof received === class="st">'string' &&
330 expected.length > 0 &&
331 received.length > 0 &&
332 expected.length <= MAX_DIFF_STRING_LENGTH &&
333 received.length <= MAX_DIFF_STRING_LENGTH &&
334 expected !== received
335 ) {
336 if (expected.includes(class="st">'\n') || received.includes(class="st">'\n')) {
337 return diffStringsUnified(expected, received, {
338 aAnnotation: expectedLabel,
339 bAnnotation: receivedLabel,
340 changeLineTrailingSpaceColor: chalk.bgYellow,
341 commonLineTrailingSpaceColor: chalk.bgYellow,
342 emptyFirstOrLastLinePlaceholder: class="st">'↵', class="cm">// U+21B5
343 expand,
344 includeChangeCounts: true,
345 });
346 }
347
348 const diffs = diffStringsRaw(expected, received, true);
349 const hasCommonDiff = diffs.some(diff => diff[0] === DIFF_EQUAL);
350
351 const printLabel = getLabelPrinter(expectedLabel, receivedLabel);
352 const expectedLine =
353 printLabel(expectedLabel) +
354 printExpected(
355 getCommonAndChangedSubstrings(diffs, DIFF_DELETE, hasCommonDiff),
356 );
357 const receivedLine =
358 printLabel(receivedLabel) +
359 printReceived(
360 getCommonAndChangedSubstrings(diffs, DIFF_INSERT, hasCommonDiff),
361 );
362
363 return `${expectedLine}\n${receivedLine}`;
364 }
365
366 if (isLineDiffable(expected, received)) {
367 const {replacedExpected, replacedReceived} =
368 replaceMatchedToAsymmetricMatcher(expected, received, [], []);
369 const difference = diffDefault(replacedExpected, replacedReceived, {
370 aAnnotation: expectedLabel,
371 bAnnotation: receivedLabel,
372 expand,
373 includeChangeCounts: true,
374 });
375
376 if (
377 typeof difference === class="st">'string' &&

Callers 8

testDiffOrStringifyFunction · 0.90
toBeFunction · 0.90
toEqualFunction · 0.90
toHavePropertyFunction · 0.90
toMatchObjectFunction · 0.90
toStrictEqualFunction · 0.90
toThrowExpectedObjectFunction · 0.90
index.test.tsFile · 0.85

Calls 9

diffStringsUnifiedFunction · 0.90
diffStringsRawFunction · 0.90
getLabelPrinterFunction · 0.85
stringifyFunction · 0.85
printExpectedFunction · 0.70
printReceivedFunction · 0.70
isLineDiffableFunction · 0.70

Tested by 1

testDiffOrStringifyFunction · 0.72