Skip to content

Commit

Permalink
Add .toBeFalsy and .toBeTruthy
Browse files Browse the repository at this point in the history
Summary:
Changelog: [Internal]
Add missing jest expect apis

Differential Revision: D69119852
  • Loading branch information
andrewdacenko authored and facebook-github-bot committed Feb 4, 2025
1 parent c677835 commit 0eb2aea
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/react-native-fantom/runtime/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
23 changes: 23 additions & 0 deletions packages/react-native-fantom/src/__tests__/expect-itest.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,29 @@ 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();
});

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();
});

['toBeCalled', 'toHaveBeenCalled'].map(toHaveBeenCalledAlias =>
test(toHaveBeenCalledAlias, () => {
const fn = jest.fn();
Expand Down

0 comments on commit 0eb2aea

Please sign in to comment.