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

Function printSnapshotAndReceived

packages/jest-snapshot/src/printSnapshot.ts:238–335  ·  view source on GitHub ↗
(
  a: string, // snapshot without extra line breaks
  b: string, // received serialized but without extra line breaks
  received: unknown,
  expand: boolean, // CLI options: true if `--expand` or false if `--no-expand`
  snapshotFormat?: SnapshotFormat,
)

Source from the content-addressed store, hash-verified

236const MAX_DIFF_STRING_LENGTH = 20_000;
237
238export const printSnapshotAndReceived = (
239 a: string, // snapshot without extra line breaks
240 b: string, // received serialized but without extra line breaks
241 received: unknown,
242 expand: boolean, // CLI options: true if `--expand` or false if `--no-expand`
243 snapshotFormat?: SnapshotFormat,
244): string => {
245 const aAnnotation = 'Snapshot';
246 const bAnnotation = 'Received';
247 const aColor = aSnapshotColor;
248 const bColor = bReceivedColor;
249 const options = {
250 aAnnotation,
251 aColor,
252 bAnnotation,
253 bColor,
254 changeLineTrailingSpaceColor: noColor,
255 commonLineTrailingSpaceColor: chalk.bgYellow,
256 emptyFirstOrLastLinePlaceholder: '↵', // U+21B5
257 expand,
258 includeChangeCounts: true,
259 };
260
261 if (typeof received === 'string') {
262 if (
263 a.length >= 2 &&
264 a.startsWith('"') &&
265 a.endsWith('"') &&
266 b === prettyFormat(received)
267 ) {
268 // If snapshot looks like default serialization of a string
269 // and received is string which has default serialization.
270
271 if (!a.includes('\n') && !b.includes('\n')) {
272 // If neither string is multiline,
273 // display as labels and quoted strings.
274 let aQuoted = a;
275 let bQuoted = b;
276
277 if (
278 a.length - 2 <= MAX_DIFF_STRING_LENGTH &&
279 b.length - 2 <= MAX_DIFF_STRING_LENGTH
280 ) {
281 const diffs = diffStringsRaw(a.slice(1, -1), b.slice(1, -1), true);
282 const hasCommon = diffs.some(diff => diff[0] === DIFF_EQUAL);
283 aQuoted = `"${joinDiffs(diffs, DIFF_DELETE, hasCommon)}"`;
284 bQuoted = `"${joinDiffs(diffs, DIFF_INSERT, hasCommon)}"`;
285 }
286
287 const printLabel = getLabelPrinter(aAnnotation, bAnnotation);
288 return `${printLabel(aAnnotation) + aColor(aQuoted)}\n${printLabel(
289 bAnnotation,
290 )}${bColor(bQuoted)}`;
291 }
292
293 // Else either string is multiline, so display as unquoted strings.
294 a = deserializeString(a); // hypothetical expected string
295 b = received; // not serialized

Callers 4

_toMatchSnapshotFunction · 0.90
testWithStringifyFunction · 0.90
testWithoutStringifyFunction · 0.90

Calls 10

diffStringsRawFunction · 0.90
getLabelPrinterFunction · 0.90
deserializeStringFunction · 0.90
diffStringsUnifiedFunction · 0.90
diffLinesUnifiedFunction · 0.90
serializeFunction · 0.90
dedentLinesFunction · 0.90
diffLinesUnified2Function · 0.90
joinDiffsFunction · 0.85
isLineDiffableFunction · 0.70

Tested by 2

testWithStringifyFunction · 0.72
testWithoutStringifyFunction · 0.72