From dc78d14651e7fbf53beef42096bec84260742be2 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Wed, 25 Sep 2024 11:45:58 +0900 Subject: [PATCH] fix(expect): fix `toBeDefined` with `expect.poll` --- packages/expect/src/jest-expect.ts | 15 +++++++-------- test/core/test/expect-poll.test.ts | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/packages/expect/src/jest-expect.ts b/packages/expect/src/jest-expect.ts index 886a7c0b4274..e365c9b1e4d0 100644 --- a/packages/expect/src/jest-expect.ts +++ b/packages/expect/src/jest-expect.ts @@ -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', diff --git a/test/core/test/expect-poll.test.ts b/test/core/test/expect-poll.test.ts index 20fb6a81e9e8..f8b922b8d105 100644 --- a/test/core/test/expect-poll.test.ts +++ b/test/core/test/expect-poll.test.ts @@ -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', + }), + })) +})