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

Function replaceAsymmetricMatcher

packages/utils/src/diff/index.ts:332–414  ·  view source on GitHub ↗
(
  actual: any,
  expected: any,
  actualReplaced: WeakSet<WeakKey> = new WeakSet(),
  expectedReplaced: WeakSet<WeakKey> = new WeakSet(),
)

Source from the content-addressed store, hash-verified

330}
331
332export function replaceAsymmetricMatcher(
333 actual: any,
334 expected: any,
335 actualReplaced: WeakSet<WeakKey> = new WeakSet(),
336 expectedReplaced: WeakSet<WeakKey> = new WeakSet(),
337): {
338 replacedActual: any
339 replacedExpected: any
340} {
341 // handle asymmetric Error.cause diff
342 if (
343 actual instanceof Error
344 && expected instanceof Error
345 && typeof actual.cause !== 'undefined'
346 && typeof expected.cause === 'undefined'
347 ) {
348 delete actual.cause
349 return {
350 replacedActual: actual,
351 replacedExpected: expected,
352 }
353 }
354 if (!isReplaceable(actual, expected)) {
355 return { replacedActual: actual, replacedExpected: expected }
356 }
357 if (actualReplaced.has(actual) || expectedReplaced.has(expected)) {
358 return { replacedActual: actual, replacedExpected: expected }
359 }
360 actualReplaced.add(actual)
361 expectedReplaced.add(expected)
362 getOwnProperties(expected).forEach((key) => {
363 const expectedValue = expected[key]
364 const actualValue = actual[key]
365 if (isAsymmetricMatcher(expectedValue)) {
366 if (expectedValue.asymmetricMatch(actualValue)) {
367 // When matcher matches, replace expected with actual value
368 // so they appear the same in the diff
369 expected[key] = actualValue
370 }
371 else if ('sample' in expectedValue && expectedValue.sample !== undefined && isReplaceable(actualValue, expectedValue.sample)) {
372 // For container matchers (ArrayContaining, ObjectContaining), unwrap and recursively process
373 // Matcher doesn't match: unwrap but keep structure to show mismatch
374 const replaced = replaceAsymmetricMatcher(
375 actualValue,
376 expectedValue.sample,
377 actualReplaced,
378 expectedReplaced,
379 )
380 actual[key] = replaced.replacedActual
381 expected[key] = replaced.replacedExpected
382 }
383 }
384 else if (isAsymmetricMatcher(actualValue)) {
385 if (actualValue.asymmetricMatch(expectedValue)) {
386 actual[key] = expectedValue
387 }
388 else if ('sample' in actualValue && actualValue.sample !== undefined && isReplaceable(actualValue.sample, expectedValue)) {
389 const replaced = replaceAsymmetricMatcher(

Callers 2

printDiffOrStringifyFunction · 0.85

Calls 6

getOwnPropertiesFunction · 0.90
isReplaceableFunction · 0.85
isAsymmetricMatcherFunction · 0.85
hasMethod · 0.45
addMethod · 0.45
asymmetricMatchMethod · 0.45

Tested by 1