Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(expect): print full error if promise is rejected #4467

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
const _error = new AssertionError(
`promise rejected "${utils.inspect(err)}" instead of resolving`,
{ showDiff: false },
)
) as Error
_error.cause = err
_error.stack = (error.stack as string).replace(error.message, _error.message)
throw _error
},
Expand Down Expand Up @@ -712,8 +713,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
(value: any) => {
const _error = new AssertionError(
`promise resolved "${utils.inspect(value)}" instead of rejecting`,
{ showDiff: false },
)
{ showDiff: true, expected: new Error('rejected promise'), actual: value },
) as any
_error.stack = (error.stack as string).replace(error.message, _error.message)
throw _error
},
Expand Down
2 changes: 2 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,8 @@ describe('async expect', () => {
}
catch (err: any) {
expect(err.message).toMatchInlineSnapshot(`"promise rejected "Error: some error { foo: { bar: 42 } }" instead of resolving"`)
expect(err.cause).toBeDefined()
expect(err.cause.message).toMatchInlineSnapshot(`"some error"`)
expect(err.stack).toContain('jest-expect.test.ts')
}
})
Expand Down
Loading