Fix some broken expectations in RN integration tests (#47615)

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

Changelog: [internal]

Fixes `.not` not being applied in some cases, and no logging the "not" label in case of error, and `.toBe` being flipped.

Reviewed By: javache

Differential Revision: D65952221

fbshipit-source-id: 030c0597b661d34e6324d69e5676375150a74f2e
This commit is contained in:
Rubén Norte 2024-11-15 03:00:17 -08:00 committed by Facebook GitHub Bot
parent 9a60038a40
commit 48b361688f

View File

@ -71,19 +71,19 @@ class Expect {
}
toBe(expected: mixed): void {
const pass = this.#received !== expected;
if (this.#isExpectedResult(pass)) {
const pass = this.#received === expected;
if (!this.#isExpectedResult(pass)) {
throw new Error(
`Expected ${String(expected)} but received ${String(this.#received)}.`,
`Expected${this.#maybeNotLabel()} ${String(expected)} but received ${String(this.#received)}.`,
);
}
}
toBeInstanceOf(expected: Class<mixed>): void {
const pass = this.#received instanceof expected;
if (!pass) {
if (!this.#isExpectedResult(pass)) {
throw new Error(
`expected ${String(this.#received)} to be an instance of ${String(expected)}`,
`expected ${String(this.#received)}${this.#maybeNotLabel()} to be an instance of ${String(expected)}`,
);
}
}
@ -91,9 +91,9 @@ class Expect {
toBeCloseTo(expected: number, precision: number = 2): void {
const pass =
Math.abs(expected - Number(this.#received)) < Math.pow(10, -precision);
if (!pass) {
if (!this.#isExpectedResult(pass)) {
throw new Error(
`expected ${String(this.#received)} to be close to ${expected}`,
`expected ${String(this.#received)}${this.#maybeNotLabel()} to be close to ${expected}`,
);
}
}
@ -110,14 +110,20 @@ class Expect {
} catch {
pass = true;
}
if (!pass) {
throw new Error(`expected ${String(this.#received)} to throw`);
if (!this.#isExpectedResult(pass)) {
throw new Error(
`expected ${String(this.#received)}${this.#maybeNotLabel()} to throw`,
);
}
}
#isExpectedResult(pass: boolean): boolean {
return this.#isNot ? !pass : pass;
}
#maybeNotLabel(): string {
return this.#isNot ? ' not' : '';
}
}
global.expect = (received: mixed) => new Expect(received);