MCPcopy
hub / github.com/vitest-dev/vitest / printDiffOrStringify

Function printDiffOrStringify

packages/utils/src/diff/index.ts:272–330  ·  view source on GitHub ↗
(
  received: unknown,
  expected: unknown,
  options?: DiffOptions,
  memory?: StringifiedMemory,
)

Source from the content-addressed store, hash-verified

270}
271
272export function printDiffOrStringify(
273 received: unknown,
274 expected: unknown,
275 options?: DiffOptions,
276 memory?: StringifiedMemory,
277): string | undefined {
278 const { aAnnotation, bAnnotation } = normalizeDiffOptions(options)
279
280 if (
281 typeof expected === 'string'
282 && typeof received === 'string'
283 && expected.length > 0
284 && received.length > 0
285 && expected.length <= MAX_DIFF_STRING_LENGTH
286 && received.length <= MAX_DIFF_STRING_LENGTH
287 && expected !== received
288 ) {
289 if (expected.includes('\n') || received.includes('\n')) {
290 return diffStringsUnified(expected, received, options)
291 }
292
293 const [diffs] = diffStringsRaw(expected, received, true)
294 const hasCommonDiff = diffs.some(diff => diff[0] === DIFF_EQUAL)
295
296 const printLabel = getLabelPrinter(aAnnotation, bAnnotation)
297 const expectedLine
298 = printLabel(aAnnotation)
299 + printExpected(
300 getCommonAndChangedSubstrings(diffs, DIFF_DELETE, hasCommonDiff),
301 )
302 const receivedLine
303 = printLabel(bAnnotation)
304 + printReceived(
305 getCommonAndChangedSubstrings(diffs, DIFF_INSERT, hasCommonDiff),
306 )
307
308 return `${expectedLine}\n${receivedLine}`
309 }
310
311 // if (isLineDiffable(expected, received)) {
312 const clonedExpected = deepClone(expected, { forceWritable: true })
313 const clonedReceived = deepClone(received, { forceWritable: true })
314 const { replacedExpected, replacedActual } = replaceAsymmetricMatcher(clonedReceived, clonedExpected)
315 const memorize = memory ? createMemorize(memory) : DEFAULT_MEMORIZE
316 const difference = diff(replacedExpected, replacedActual, options, memorize)
317
318 return difference
319 // }
320
321 // const printLabel = getLabelPrinter(aAnnotation, bAnnotation)
322 // const expectedLine = printLabel(aAnnotation) + printExpected(expected)
323 // const receivedLine
324 // = printLabel(bAnnotation)
325 // + (stringify(expected) === stringify(received)
326 // ? 'serializes to the same string'
327 // : printReceived(received))
328
329 // return `${expectedLine}\n${receivedLine}`

Callers 2

processErrorFunction · 0.90
diff.test.tsFile · 0.90

Calls 11

normalizeDiffOptionsFunction · 0.90
diffStringsUnifiedFunction · 0.90
diffStringsRawFunction · 0.90
deepCloneFunction · 0.90
getLabelPrinterFunction · 0.85
replaceAsymmetricMatcherFunction · 0.85
createMemorizeFunction · 0.85
diffFunction · 0.85
printExpectedFunction · 0.70
printReceivedFunction · 0.70

Tested by

no test coverage detected