Skip to content

Commit

Permalink
fix(expect): print full error if promise is rejected (vitest-dev#4467)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored and LorenzoBloedow committed Dec 19, 2023
1 parent 4af9d90 commit 633b382
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
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

0 comments on commit 633b382

Please sign in to comment.