Skip to content

Commit

Permalink
fix: tweak asymmetric error.cause diff
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Oct 8, 2024
1 parent 5de6d0a commit 1908bd6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
6 changes: 2 additions & 4 deletions packages/utils/src/diff/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,8 @@ export function replaceAsymmetricMatcher(
} {
// handle asymmetric Error.cause diff
if (
actual instanceof Error
&& expected instanceof Error
&& typeof actual.cause !== 'undefined'
&& typeof expected.cause === 'undefined'
(actual instanceof Error && typeof actual.cause !== 'undefined')
&& !(expected instanceof Error && typeof expected.cause !== 'undefined')
) {
delete actual.cause
return {
Expand Down
20 changes: 19 additions & 1 deletion test/core/test/__snapshots__/jest-expect.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,24 @@ exports[`error equality 7`] = `
`;
exports[`error equality 8`] = `
{
"actual": "[Error: hello]",
"diff": "- Expected
+ Received
- Object {
- "something": "else",
+ Error {
+ "message": "hello",
}",
"expected": "Object {
"something": "else",
}",
"message": "expected Error: hello { cause: 'x' } to deeply equal { something: 'else' }",
}
`;
exports[`error equality 9`] = `
{
"actual": "[AggregateError: outer]",
"diff": "- Expected
Expand All @@ -449,7 +467,7 @@ exports[`error equality 8`] = `
}
`;
exports[`error equality 9`] = `
exports[`error equality 10`] = `
{
"actual": "undefined",
"diff": undefined,
Expand Down
8 changes: 8 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,14 @@ it('error equality', () => {
snapshotError(() => expect(e1).toEqual(e2))
}

{
// different cause
const e1 = new Error('hello', { cause: 'x' })
const e2 = { something: 'else' }
expect(e1).toEqual(e2)

Check failure on line 1453 in test/core/test/jest-expect.test.ts

View workflow job for this annotation

GitHub Actions / Build&Test: node-22, ubuntu-latest

test/jest-expect.test.ts > error equality

AssertionError: expected Error: hello { cause: 'x' } to deeply equal { something: 'else' } - Expected + Received - Object { - "something": "else", + Error { + "message": "hello", } ❯ test/jest-expect.test.ts:1453:16

Check failure on line 1453 in test/core/test/jest-expect.test.ts

View workflow job for this annotation

GitHub Actions / Build&Test: node-22, ubuntu-latest

test/jest-expect.test.ts > error equality

AssertionError: expected Error: hello { cause: 'x' } to deeply equal { something: 'else' } - Expected + Received - Object { - "something": "else", + Error { + "message": "hello", } ❯ test/jest-expect.test.ts:1453:16

Check failure on line 1453 in test/core/test/jest-expect.test.ts

View workflow job for this annotation

GitHub Actions / Build&Test: node-20, macos-latest

test/jest-expect.test.ts > error equality

AssertionError: expected Error: hello { cause: 'x' } to deeply equal { something: 'else' } - Expected + Received - Object { - "something": "else", + Error { + "message": "hello", } ❯ test/jest-expect.test.ts:1453:16

Check failure on line 1453 in test/core/test/jest-expect.test.ts

View workflow job for this annotation

GitHub Actions / Build&Test: node-20, macos-latest

test/jest-expect.test.ts > error equality

AssertionError: expected Error: hello { cause: 'x' } to deeply equal { something: 'else' } - Expected + Received - Object { - "something": "else", + Error { + "message": "hello", } ❯ test/jest-expect.test.ts:1453:16

Check failure on line 1453 in test/core/test/jest-expect.test.ts

View workflow job for this annotation

GitHub Actions / Build&Test: node-20, windows-latest

test/jest-expect.test.ts > error equality

AssertionError: expected Error: hello { cause: 'x' } to deeply equal { something: 'else' } - Expected + Received - Object { - "something": "else", + Error { + "message": "hello", } ❯ test/jest-expect.test.ts:1453:16

Check failure on line 1453 in test/core/test/jest-expect.test.ts

View workflow job for this annotation

GitHub Actions / Build&Test: node-20, windows-latest

test/jest-expect.test.ts > error equality

AssertionError: expected Error: hello { cause: 'x' } to deeply equal { something: 'else' } - Expected + Received - Object { - "something": "else", + Error { + "message": "hello", } ❯ test/jest-expect.test.ts:1453:16
snapshotError(() => expect(e1).toEqual(e2))
}

{
// AggregateError (pass)
const e1 = new AggregateError([new Error('inner')], 'outer', { cause: 'x' })
Expand Down

0 comments on commit 1908bd6

Please sign in to comment.