Add .toBeFalsy and .toBeTruthy (#49169)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/49169

Changelog: [Internal]
Add missing jest expect apis

Reviewed By: rubennorte

Differential Revision: D69119852

fbshipit-source-id: dfda47dcc61f17ecbd205863d83b268005c74a83
This commit is contained in:
Andrew Datsenko
2025-02-04 10:54:19 -08:00
committed by Facebook GitHub Bot
parent 92fede1a1d
commit a702238bed
2 changed files with 57 additions and 0 deletions
+18
View File
@@ -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(
@@ -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();