Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: rejects & resolves breaks with thenable objects (#3456)
  • Loading branch information
fenghan34 committed May 29, 2023
1 parent 56b9927 commit 4e996ae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/expect/src/utils.ts
@@ -1,6 +1,6 @@
export function recordAsyncExpect(test: any, promise: Promise<any>) {
export function recordAsyncExpect(test: any, promise: Promise<any> | PromiseLike<any>) {
// record promise for test, that resolves before test ends
if (test) {
if (test && promise instanceof Promise) {
// if promise is explicitly awaited, remove it from the list
promise = promise.finally(() => {
const index = test.promises.indexOf(promise)
Expand Down
19 changes: 19 additions & 0 deletions test/core/test/jest-expect.test.ts
Expand Up @@ -720,6 +720,25 @@ describe('async expect', () => {
expect(value).toBe(1)
})
})

it('handle thenable objects', async () => {
await expect({ then: (resolve: any) => resolve(0) }).resolves.toBe(0)
await expect({ then: (_: any, reject: any) => reject(0) }).rejects.toBe(0)

try {
await expect({ then: (resolve: any) => resolve(0) }).rejects.toBe(0)
}
catch (error) {
expect(error).toEqual(new Error('promise resolved "0" instead of rejecting'))
}

try {
await expect({ then: (_: any, reject: any) => reject(0) }).resolves.toBe(0)
}
catch (error) {
expect(error).toEqual(new Error('promise rejected "0" instead of resolving'))
}
})
})

it('compatible with jest', () => {
Expand Down

0 comments on commit 4e996ae

Please sign in to comment.