From b487e65869d85ff4d41397bb000468531ce6f97e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Thu, 5 Dec 2024 05:18:08 -0800 Subject: [PATCH] Add support for expect() .toBeLessThan and .toBeGreaterThan (#48086) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48086 Changelog: [internal] Just adding a bit of coverage for the `expect` API adding `toBeLessThan` and `toBeGreaterThan`. Reviewed By: sammy-SC Differential Revision: D66753268 fbshipit-source-id: 6a26f558f985ccbb5eb0daacecd93759841149e9 --- jest/integration/runtime/expect.js | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/jest/integration/runtime/expect.js b/jest/integration/runtime/expect.js index 98c28ba549d..5e6728950eb 100644 --- a/jest/integration/runtime/expect.js +++ b/jest/integration/runtime/expect.js @@ -165,6 +165,27 @@ class Expect { } } + toBeGreaterThan(expected: number): void { + if (typeof this.#received !== 'number') { + throw new ErrorWithCustomBlame( + `Expected ${String(this.#received)} to be a number but it was a ${typeof this.#received}`, + ).blameToPreviousFrame(); + } + + if (typeof expected !== 'number') { + throw new ErrorWithCustomBlame( + `Expected ${String(expected)} to be a number but it was a ${typeof expected}`, + ).blameToPreviousFrame(); + } + + const pass = this.#received > expected; + if (!this.#isExpectedResult(pass)) { + throw new ErrorWithCustomBlame( + `Expected ${String(this.#received)}${this.#maybeNotLabel()} to be greater than or equal to ${expected}`, + ).blameToPreviousFrame(); + } + } + toBeGreaterThanOrEqual(expected: number): void { if (typeof this.#received !== 'number') { throw new ErrorWithCustomBlame( @@ -186,6 +207,27 @@ class Expect { } } + toBeLessThan(expected: number): void { + if (typeof this.#received !== 'number') { + throw new ErrorWithCustomBlame( + `Expected ${String(this.#received)} to be a number but it was a ${typeof this.#received}`, + ).blameToPreviousFrame(); + } + + if (typeof expected !== 'number') { + throw new ErrorWithCustomBlame( + `Expected ${String(expected)} to be a number but it was a ${typeof expected}`, + ).blameToPreviousFrame(); + } + + const pass = this.#received < expected; + if (!this.#isExpectedResult(pass)) { + throw new ErrorWithCustomBlame( + `Expected ${String(this.#received)}${this.#maybeNotLabel()} to be less than or equal to ${expected}`, + ).blameToPreviousFrame(); + } + } + toBeLessThanOrEqual(expected: number): void { if (typeof this.#received !== 'number') { throw new ErrorWithCustomBlame(