Skip to content

Commit

Permalink
fix(expect): fix toBeDefined with expect.poll
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Sep 25, 2024
1 parent 8723242 commit dc78d14
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",

Check failure on line 389 in packages/expect/src/jest-expect.ts

View workflow job for this annotation

GitHub Actions / Lint: node-latest, ubuntu-latest

Strings must use singlequote
'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()

Check failure on line 92 in test/core/test/expect-poll.test.ts

View workflow job for this annotation

GitHub Actions / Lint: node-latest, ubuntu-latest

Missing trailing comma
).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()

Check failure on line 101 in test/core/test/expect-poll.test.ts

View workflow job for this annotation

GitHub Actions / Lint: node-latest, ubuntu-latest

Missing trailing comma
).rejects.toThrowError(expect.objectContaining({
message: 'Matcher did not succeed in 100ms',
cause: expect.objectContaining({
message: 'expected undefined to be defined',
}),
}))
})

0 comments on commit dc78d14

Please sign in to comment.