Skip to content

Commit 13684b0

Browse files
authored
Align Fantom equality and numeric matchers with Jest (#59891)
Summary: Use Jest-compatible equality, strict null checks, numeric input validation, precision thresholds and infinity handling in Fantom. Preserve negation and error attribution, and update conformance tests to distinguish omitted native tree children from null. Changelog: [Internal] Differential Revision: D130666410 Pull Request resolved: #59891
1 parent 0e4af15 commit 13684b0

2 files changed

Lines changed: 238 additions & 4 deletions

File tree

‎private/react-native-fantom/runtime/expect.js‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class Expect {
133133
}
134134

135135
toBe(expected: unknown): void {
136-
const pass = this.#received === expected;
136+
const pass = Object.is(this.#received, expected);
137137
if (!this.#isExpectedResult(pass)) {
138138
throw new ErrorWithCustomBlame(
139139
`Expected${this.#maybeNotLabel()} ${String(expected)} but received ${String(this.#received)}.`,
@@ -150,9 +150,23 @@ class Expect {
150150
}
151151
}
152152

153-
toBeCloseTo(expected: number, precision: number = 2): void {
153+
toBeCloseTo(expected: unknown, precision: number = 2): void {
154+
const received = this.#received;
155+
if (typeof expected !== 'number') {
156+
throw new ErrorWithCustomBlame(
157+
`Expected ${String(expected)} to be a number but it was a ${typeof expected}`,
158+
).blameToPreviousFrame();
159+
}
160+
if (typeof received !== 'number') {
161+
throw new ErrorWithCustomBlame(
162+
`Expected ${String(received)} to be a number but it was a ${typeof received}`,
163+
).blameToPreviousFrame();
164+
}
165+
154166
const pass =
155-
Math.abs(expected - Number(this.#received)) < Math.pow(10, -precision);
167+
(received === Infinity && expected === Infinity) ||
168+
(received === -Infinity && expected === -Infinity) ||
169+
Math.abs(expected - received) < Math.pow(10, -precision) / 2;
156170
if (!this.#isExpectedResult(pass)) {
157171
throw new ErrorWithCustomBlame(
158172
`Expected ${String(this.#received)}${this.#maybeNotLabel()} to be close to ${expected}`,
@@ -179,7 +193,7 @@ class Expect {
179193
}
180194

181195
toBeNull(): void {
182-
const pass = this.#received == null;
196+
const pass = Object.is(this.#received, null);
183197
if (!this.#isExpectedResult(pass)) {
184198
throw new ErrorWithCustomBlame(
185199
`Expected ${String(this.#received)}${this.#maybeNotLabel()} to be null`,

‎private/react-native-fantom/src/__tests__/expect-itest.js‎

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @format
99
*/
1010

11+
import fantomExpect from '../../runtime/expect';
1112
import * as React from 'react';
1213

1314
function ensureError(fn: () => void): void {
@@ -20,7 +21,226 @@ function ensureError(fn: () => void): void {
2021
throw new Error(`Expected function to throw, but it didn't`);
2122
}
2223

24+
function assertMatcherResult(assertion: () => void, pass: boolean): void {
25+
let threw = false;
26+
try {
27+
assertion();
28+
} catch (error: unknown) {
29+
if (!(error instanceof Error)) {
30+
throw new Error(`Unexpected thrown value: ${String(error)}`);
31+
}
32+
threw = true;
33+
}
34+
expect(threw).toBe(!pass);
35+
}
36+
37+
function assertCallerBlame(stack: ?string, caller: string): void {
38+
const firstJavaScriptFrame = stack
39+
?.split('\n')
40+
.find(line => /^\s*at .+ \(.+:\d+:\d+\)$/.test(line));
41+
expect(firstJavaScriptFrame).toContain(caller);
42+
}
43+
44+
function failEqualityAtCaller(): void {
45+
expect(0).toBe(-0);
46+
}
47+
48+
function failNullAtCaller(): void {
49+
expect(null).not.toBeNull();
50+
}
51+
52+
function failCloseToAtCaller(): void {
53+
expect(Infinity).not.toBeCloseTo(Infinity);
54+
}
55+
56+
function failNumericAtCaller(): void {
57+
expect('1').not.toBeCloseTo(1);
58+
}
59+
2360
describe('expect', () => {
61+
const equalityCases: Array<[unknown, unknown, boolean]> = [
62+
[NaN, NaN, true],
63+
[NaN, 0, false],
64+
[0, NaN, false],
65+
[0, -0, false],
66+
[-0, 0, false],
67+
[0, 0, true],
68+
[-0, -0, true],
69+
[1, 1, true],
70+
[1, '1', false],
71+
];
72+
for (const [index, [received, expected, pass]] of equalityCases.entries()) {
73+
test(`toBe edge case ${index}`, () => {
74+
assertMatcherResult(() => expect(received).toBe(expected), pass);
75+
assertMatcherResult(() => expect(received).not.toBe(expected), !pass);
76+
assertMatcherResult(
77+
() => fantomExpect(received).not.not.toBe(expected),
78+
pass,
79+
);
80+
});
81+
}
82+
83+
const nullCases: Array<[unknown, boolean]> = [
84+
[null, true],
85+
[undefined, false],
86+
[0, false],
87+
[false, false],
88+
['', false],
89+
];
90+
for (const [index, [received, pass]] of nullCases.entries()) {
91+
test(`toBeNull edge case ${index}`, () => {
92+
assertMatcherResult(() => expect(received).toBeNull(), pass);
93+
assertMatcherResult(() => expect(received).not.toBeNull(), !pass);
94+
});
95+
}
96+
97+
const closeToCases: Array<[number, number, number, boolean]> = [
98+
[0.004, 0, 2, true],
99+
[0.005, 0, 2, false],
100+
[0.006, 0, 2, false],
101+
[-0.004, 0, 2, true],
102+
[-0.005, 0, 2, false],
103+
[0.49, 0, 0, true],
104+
[0.5, 0, 0, false],
105+
[0.0004, 0, 3, true],
106+
[0.0005, 0, 3, false],
107+
[4, 0, -1, true],
108+
[5, 0, -1, false],
109+
[0.2 + 0.1, 0.3, 2, true],
110+
[0, -0, 2, true],
111+
[NaN, NaN, 2, false],
112+
[NaN, 0, 2, false],
113+
[0, NaN, 2, false],
114+
[Infinity, Infinity, 2, true],
115+
[-Infinity, -Infinity, 2, true],
116+
[Infinity, -Infinity, 2, false],
117+
[-Infinity, Infinity, 2, false],
118+
[Infinity, 1, 2, false],
119+
[1, Infinity, 2, false],
120+
[-Infinity, 1, 2, false],
121+
[1, -Infinity, 2, false],
122+
];
123+
for (const [
124+
index,
125+
[received, expected, precision, pass],
126+
] of closeToCases.entries()) {
127+
test(`toBeCloseTo edge case ${index}`, () => {
128+
assertMatcherResult(
129+
() => expect(received).toBeCloseTo(expected, precision),
130+
pass,
131+
);
132+
assertMatcherResult(
133+
() => expect(received).not.toBeCloseTo(expected, precision),
134+
!pass,
135+
);
136+
});
137+
}
138+
139+
test('toBeCloseTo defaults to two digits of precision', () => {
140+
assertMatcherResult(() => expect(0.004).toBeCloseTo(0), true);
141+
assertMatcherResult(() => expect(0.005).not.toBeCloseTo(0), true);
142+
assertMatcherResult(() => expect(0.005).toBeCloseTo(0), false);
143+
assertMatcherResult(() => expect(0.004).not.toBeCloseTo(0), false);
144+
});
145+
146+
const nonnumericCases: Array<unknown> = [
147+
'1',
148+
null,
149+
undefined,
150+
true,
151+
[],
152+
Object(1),
153+
Symbol('1'),
154+
];
155+
for (const [index, value] of nonnumericCases.entries()) {
156+
test(`toBeCloseTo rejects nonnumeric argument ${index}`, () => {
157+
const message = `Expected ${String(value)} to be a number but it was a ${typeof value}`;
158+
expect(() => expect(value).toBeCloseTo(1)).toThrow(message);
159+
expect(() => expect(value).not.toBeCloseTo(1)).toThrow(message);
160+
expect(() => fantomExpect(1).toBeCloseTo(value)).toThrow(message);
161+
expect(() => fantomExpect(1).not.toBeCloseTo(value)).toThrow(message);
162+
});
163+
}
164+
165+
test('toBeCloseTo does not coerce received values', () => {
166+
const valueOf = jest.fn(() => 1);
167+
expect(() => expect({valueOf}).toBeCloseTo(1)).toThrow(
168+
'Expected [object Object] to be a number but it was a object',
169+
);
170+
expect(valueOf).not.toHaveBeenCalled();
171+
});
172+
173+
const blameCases: Array<[() => void, string, string]> = [
174+
[
175+
failEqualityAtCaller,
176+
'failEqualityAtCaller',
177+
'Expected 0 but received 0.',
178+
],
179+
[failNullAtCaller, 'failNullAtCaller', 'Expected null not to be null'],
180+
[
181+
failCloseToAtCaller,
182+
'failCloseToAtCaller',
183+
'Expected Infinity not to be close to Infinity',
184+
],
185+
[
186+
failNumericAtCaller,
187+
'failNumericAtCaller',
188+
'Expected 1 to be a number but it was a string',
189+
],
190+
];
191+
for (const [assertion, caller, message] of blameCases) {
192+
test(`preserves native matcher blame at ${caller}`, () => {
193+
let failure: ?Error;
194+
try {
195+
assertion();
196+
} catch (error: unknown) {
197+
if (!(error instanceof Error)) {
198+
throw new Error(`Unexpected thrown value: ${String(error)}`);
199+
}
200+
failure = error;
201+
}
202+
if (failure == null) {
203+
throw new Error('Expected matcher failure');
204+
}
205+
expect(failure.name).toBe('Error');
206+
expect(failure.message).toBe(message);
207+
const stack = failure.stack;
208+
assertCallerBlame(stack, caller);
209+
expect(failure.stack).toBe(stack);
210+
211+
// Legacy Hermes can retain native construction frames before the first
212+
// JavaScript frame. Exercise that prefix through the real error class.
213+
failure.stack = [
214+
`Error: ${message}`,
215+
' at construct (native)',
216+
' at apply (native)',
217+
...Array.from(
218+
{length: 6},
219+
(_, index) => ` at helper${index} (expect.js:1:1)`,
220+
),
221+
` at ${caller} (expect-itest.js:1:1)`,
222+
].join('\n');
223+
const nativePrefixedStack = failure.stack;
224+
assertCallerBlame(nativePrefixedStack, caller);
225+
expect(failure.stack).toBe(nativePrefixedStack);
226+
});
227+
}
228+
229+
test('caller blame rejects earlier JavaScript helper frames', () => {
230+
ensureError(() =>
231+
assertCallerBlame(
232+
[
233+
'Error: matcher failed',
234+
' at construct (native)',
235+
' at apply (native)',
236+
' at toBe (expect.js:1:1)',
237+
' at caller (expect-itest.js:1:1)',
238+
].join('\n'),
239+
'caller',
240+
),
241+
);
242+
});
243+
24244
test('toThrow', () => {
25245
expect(() => {
26246
throw new Error();

0 commit comments

Comments
 (0)