Skip to content

Commit

Permalink
Fix some broken expectations in RN integration tests (facebook#47615)
Browse files Browse the repository at this point in the history
Summary:

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
  • Loading branch information
rubennorte authored and facebook-github-bot committed Nov 14, 2024
1 parent 1afde8b commit 088cf18
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions jest/integration/runtime/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,29 @@ 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)}`,
);
}
}

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}`,
);
}
}
Expand All @@ -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);
Expand Down

0 comments on commit 088cf18

Please sign in to comment.