Skip to content

Commit

Permalink
Add .toBeDefined and .toBeUndefined (facebook#49168)
Browse files Browse the repository at this point in the history
Summary:

Changelog: [Internal]
Add missing jest expect apis

Differential Revision: D69119330
  • Loading branch information
andrewdacenko authored and facebook-github-bot committed Feb 4, 2025
1 parent cea1da9 commit c677835
Show file tree
Hide file tree
Showing 2 changed files with 48 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 @@ -117,6 +117,24 @@ class Expect {
}
}

toBeDefined(): void {
const pass = this.#received !== undefined;
if (!this.#isExpectedResult(pass)) {
throw new ErrorWithCustomBlame(
`Expected ${String(this.#received)}${this.#maybeNotLabel()} to be defined`,
).blameToPreviousFrame();
}
}

toBeUndefined(): void {
const pass = this.#received === undefined;
if (!this.#isExpectedResult(pass)) {
throw new ErrorWithCustomBlame(
`Expected ${String(this.#received)}${this.#maybeNotLabel()} to be undefined`,
).blameToPreviousFrame();
}
}

toBeNull(): void {
const pass = this.#received == null;
if (!this.#isExpectedResult(pass)) {
Expand Down
30 changes: 30 additions & 0 deletions packages/react-native-fantom/src/__tests__/expect-itest.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,36 @@ describe('expect', () => {
}).toThrow();
});

test('toBeDefined', () => {
expect(null).toBeDefined();
expect(false).toBeDefined();
expect('value').toBeDefined();
expect(undefined).not.toBeDefined();

expect(() => {
expect({}).not.toBeDefined();
}).toThrow();

expect(() => {
expect(undefined).toBeDefined();
}).toThrow();
});

test('toBeUndefined', () => {
expect(undefined).toBeUndefined();
expect(null).not.toBeUndefined();
expect(false).not.toBeUndefined();
expect('value').not.toBeUndefined();

expect(() => {
expect(undefined).not.toBeUndefined();
}).toThrow();

expect(() => {
expect({}).toBeUndefined();
}).toThrow();
});

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

0 comments on commit c677835

Please sign in to comment.