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: rejects & resolves breaks with thenable objects #3456

Merged
merged 1 commit into from May 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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