From 78ff3de8364869f5a9ef21c11c9f3552856987db Mon Sep 17 00:00:00 2001 From: Andrew Datsenko Date: Tue, 4 Feb 2025 10:06:15 -0800 Subject: [PATCH] [skip ci] Add `.toBeFalsy` and `.toBeTruthy` (#49169) Summary: Changelog: [Internal] Add missing jest expect apis Reviewed By: rubennorte Differential Revision: D69119852 --- .../react-native-fantom/runtime/expect.js | 18 +++++++++ .../src/__tests__/expect-itest.js | 39 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/packages/react-native-fantom/runtime/expect.js b/packages/react-native-fantom/runtime/expect.js index 2a23b53f8ee9..05d99538dc1c 100644 --- a/packages/react-native-fantom/runtime/expect.js +++ b/packages/react-native-fantom/runtime/expect.js @@ -144,6 +144,24 @@ class Expect { } } + toBeFalsy(): void { + const pass = Boolean(this.#received) === false; + if (!this.#isExpectedResult(pass)) { + throw new ErrorWithCustomBlame( + `Expected ${String(this.#received)}${this.#maybeNotLabel()} to be falsy`, + ).blameToPreviousFrame(); + } + } + + toBeTruthy(): void { + const pass = Boolean(this.#received); + if (!this.#isExpectedResult(pass)) { + throw new ErrorWithCustomBlame( + `Expected ${String(this.#received)}${this.#maybeNotLabel()} to be truthy`, + ).blameToPreviousFrame(); + } + } + toThrow(expected?: string): void { if (expected != null && typeof expected !== 'string') { throw new ErrorWithCustomBlame( diff --git a/packages/react-native-fantom/src/__tests__/expect-itest.js b/packages/react-native-fantom/src/__tests__/expect-itest.js index 35d0f91fd03f..a7edafea6996 100644 --- a/packages/react-native-fantom/src/__tests__/expect-itest.js +++ b/packages/react-native-fantom/src/__tests__/expect-itest.js @@ -178,6 +178,45 @@ describe('expect', () => { }).toThrow(); }); + test('toBeFalsy', () => { + expect(false).toBeFalsy(); + expect(0).toBeFalsy(); + expect('').toBeFalsy(); + expect(null).toBeFalsy(); + expect(undefined).toBeFalsy(); + expect(NaN).toBeFalsy(); + expect([]).not.toBeFalsy(); + expect(['']).not.toBeFalsy(); + + expect(() => { + expect(true).toBeFalsy(); + }).toThrow(); + + expect(() => { + expect(false).not.toBeFalsy(); + }).toThrow(); + }); + + test('toBeTruthy', () => { + expect(true).toBeTruthy(); + expect([]).toBeTruthy(); + expect('a').toBeTruthy(); + expect(false).not.toBeTruthy(); + expect(0).not.toBeTruthy(); + expect('').not.toBeTruthy(); + expect(null).not.toBeTruthy(); + expect(undefined).not.toBeTruthy(); + expect(NaN).not.toBeTruthy(); + + expect(() => { + expect(false).toBeTruthy(); + }).toThrow(); + + expect(() => { + expect(true).not.toBeTruthy(); + }).toThrow(); + }); + ['toBeCalled', 'toHaveBeenCalled'].map(toHaveBeenCalledAlias => test(toHaveBeenCalledAlias, () => { const fn = jest.fn();