Skip to content

Commit 1908bd6

Browse files
committed
fix: tweak asymmetric error.cause diff
1 parent 5de6d0a commit 1908bd6

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

packages/utils/src/diff/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,8 @@ export function replaceAsymmetricMatcher(
300300
} {
301301
// handle asymmetric Error.cause diff
302302
if (
303-
actual instanceof Error
304-
&& expected instanceof Error
305-
&& typeof actual.cause !== 'undefined'
306-
&& typeof expected.cause === 'undefined'
303+
(actual instanceof Error && typeof actual.cause !== 'undefined')
304+
&& !(expected instanceof Error && typeof expected.cause !== 'undefined')
307305
) {
308306
delete actual.cause
309307
return {

test/core/test/__snapshots__/jest-expect.test.ts.snap

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,24 @@ exports[`error equality 7`] = `
428428
`;
429429
430430
exports[`error equality 8`] = `
431+
{
432+
"actual": "[Error: hello]",
433+
"diff": "- Expected
434+
+ Received
435+
436+
- Object {
437+
- "something": "else",
438+
+ Error {
439+
+ "message": "hello",
440+
}",
441+
"expected": "Object {
442+
"something": "else",
443+
}",
444+
"message": "expected Error: hello { cause: 'x' } to deeply equal { something: 'else' }",
445+
}
446+
`;
447+
448+
exports[`error equality 9`] = `
431449
{
432450
"actual": "[AggregateError: outer]",
433451
"diff": "- Expected
@@ -449,7 +467,7 @@ exports[`error equality 8`] = `
449467
}
450468
`;
451469
452-
exports[`error equality 9`] = `
470+
exports[`error equality 10`] = `
453471
{
454472
"actual": "undefined",
455473
"diff": undefined,

test/core/test/jest-expect.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,14 @@ it('error equality', () => {
14461446
snapshotError(() => expect(e1).toEqual(e2))
14471447
}
14481448

1449+
{
1450+
// different cause
1451+
const e1 = new Error('hello', { cause: 'x' })
1452+
const e2 = { something: 'else' }
1453+
expect(e1).toEqual(e2)
1454+
snapshotError(() => expect(e1).toEqual(e2))
1455+
}
1456+
14491457
{
14501458
// AggregateError (pass)
14511459
const e1 = new AggregateError([new Error('inner')], 'outer', { cause: 'x' })

0 commit comments

Comments
 (0)