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): fix toBeDefined with expect.poll #6562

Merged
merged 2 commits into from
Sep 25, 2024
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
15 changes: 7 additions & 8 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,13 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
return this.be.null
})
def('toBeDefined', function () {
const negate = utils.flag(this, 'negate')
utils.flag(this, 'negate', false)

if (negate) {
return this.be.undefined
}

return this.not.be.undefined
const obj = utils.flag(this, 'object')
this.assert(
typeof obj !== 'undefined',
'expected #{this} to be defined',
'expected #{this} to be undefined',
obj,
)
})
def(
'toBeTypeOf',
Expand Down
23 changes: 23 additions & 0 deletions test/core/test/expect-poll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,26 @@ test('custom matcher works correctly', async () => {
expect(fn).toHaveBeenCalledTimes(3)
expect(fn).toHaveBeenCalledWith({ poll: true })
})

test('toBeDefined', async () => {
await expect.poll(() => 1).toBeDefined()
await expect.poll(() => undefined).not.toBeDefined()

await expect(() =>
expect.poll(() => 1, { timeout: 100, interval: 10 }).not.toBeDefined(),
).rejects.toThrowError(expect.objectContaining({
message: 'Matcher did not succeed in 100ms',
cause: expect.objectContaining({
message: 'expected 1 to be undefined',
}),
}))

await expect(() =>
expect.poll(() => undefined, { timeout: 100, interval: 10 }).toBeDefined(),
).rejects.toThrowError(expect.objectContaining({
message: 'Matcher did not succeed in 100ms',
cause: expect.objectContaining({
message: 'expected undefined to be defined',
}),
}))
})