Skip to content

Commit

Permalink
fix: handle asymmetric cause diff
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Oct 7, 2024
1 parent 898ce93 commit 755cfb8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
14 changes: 13 additions & 1 deletion packages/utils/src/diff/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ export function printDiffOrStringify(
// if (isLineDiffable(expected, received)) {
const clonedExpected = deepClone(expected, { forceWritable: true })
const clonedReceived = deepClone(received, { forceWritable: true })
// TODO: strip `Error.cause` like asymmetric matcher
const { replacedExpected, replacedActual } = replaceAsymmetricMatcher(clonedExpected, clonedReceived)
const difference = diff(replacedExpected, replacedActual, options)

Expand All @@ -299,6 +298,19 @@ export function replaceAsymmetricMatcher(
replacedActual: any
replacedExpected: any
} {
// handle asymmetric Error.cause diff
if (
actual instanceof Error
&& expected instanceof Error
&& typeof actual.cause !== 'undefined'
&& typeof expected.cause === 'undefined'
) {
delete actual.cause
return {
replacedActual: actual,
replacedExpected: expected,
}
}
if (!isReplaceable(actual, expected)) {
return { replacedActual: actual, replacedExpected: expected }
}
Expand Down
1 change: 0 additions & 1 deletion test/core/test/__snapshots__/jest-expect.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ exports[`error equality 6`] = `
Error {
- "message": "world",
+ "message": "hello",
+ "cause": "x",
}",
"expected": "[Error: world]",
"message": "expected Error: hello { cause: 'x' } to deeply equal Error: world",
Expand Down
7 changes: 6 additions & 1 deletion test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,6 @@ it('error equality', () => {

{
// different cause (fail by other props)
// TODO: strip `cause` on actual side from diff
const e1 = new Error('hello', { cause: 'x' })
const e2 = new Error('world')
snapshotError(() => expect(e1).toEqual(e2))
Expand All @@ -1453,6 +1452,12 @@ it('error equality', () => {
// TODO: cyclic
})

it.skip('repro', () => {
const e1 = new Error('hello', { cause: 'x' })
const e2 = new Error('world')
expect(e1).toEqual(e2)
})

it('toHaveBeenNthCalledWith error', () => {
const fn = vi.fn()
fn('World')
Expand Down

0 comments on commit 755cfb8

Please sign in to comment.