Skip to content

Commit

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

Differential Revision: D69123117
  • Loading branch information
andrewdacenko authored and facebook-github-bot committed Feb 4, 2025
1 parent c5a65e0 commit c597b2c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/react-native-fantom/runtime/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ class Expect {
}
}

toStrictEqual(expected: mixed): void {
let expectedType: mixed =
typeof expected === 'object' && expected !== null
? Object.getPrototypeOf(expected)
: null;
let receivedType: mixed =
typeof this.#received === 'object' && this.#received !== null
? Object.getPrototypeOf(this.#received)
: null;
const pass =
deepEqual(this.#received, expected, {strict: true}) &&
expectedType === receivedType;
if (!this.#isExpectedResult(pass)) {
throw new ErrorWithCustomBlame(
`Expected${this.#maybeNotLabel()} to strictly equal:\n${
diff(expected, this.#received, {
contextLines: 1,
expand: false,
omitAnnotationLines: true,
}) ?? 'Failed to compare outputs'
}`,
).blameToPreviousFrame();
}
}

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

test('toStrictEqual', () => {
class LaCroix {
flavor: string;
constructor(flavor: string) {
this.flavor = flavor;
}
}

expect({a: undefined, b: 2}).not.toStrictEqual({b: 2});
expect([2, undefined]).not.toStrictEqual([2]);
expect([2]).not.toStrictEqual([2, undefined]);
// This is part of spec https://jestjs.io/docs/expect#tostrictequalvalue
// eslint-disable-next-line no-sparse-arrays
expect([, 2]).not.toStrictEqual([undefined, 2]);
expect(new LaCroix('lemon')).not.toStrictEqual({flavor: 'lemon'});
});

test('toBeInstanceOf', () => {
class Class {}

Expand Down

0 comments on commit c597b2c

Please sign in to comment.