Skip to content

Commit

Permalink
fix(expect): fix toBeDefined with expect.poll (#6562)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Sep 25, 2024
1 parent ea2d429 commit f7da619
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
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',
}),
}))
})

0 comments on commit f7da619

Please sign in to comment.