1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 22:48:11 +00:00

LibJS: Test function toBeCloseTo takes an optional precision argument

Modeled after the equivalent Jest function.
https://jestjs.io/docs/expect#tobeclosetonumber-numdigits
This commit is contained in:
Sam Kravitz 2023-08-02 18:22:54 -05:00 committed by Andreas Kling
parent 73c8650ea0
commit c97eec030c
2 changed files with 17 additions and 3 deletions

View file

@ -86,8 +86,7 @@ class ExpectationError extends Error {
});
}
// FIXME: Take a precision argument like jest's toBeCloseTo matcher
toBeCloseTo(value) {
toBeCloseTo(value, precision = 5) {
this.__expect(
typeof this.target === "number",
() => `toBeCloseTo: expected target of type number, got ${typeof this.target}`
@ -96,9 +95,14 @@ class ExpectationError extends Error {
typeof value === "number",
() => `toBeCloseTo: expected argument of type number, got ${typeof value}`
);
this.__expect(
typeof precision === "number",
() => `toBeCloseTo: expected precision of type number, got ${typeof precision}`
);
const epsilon = 10 ** -precision / 2;
this.__doMatcher(() => {
this.__expect(Math.abs(this.target - value) < 0.000001);
this.__expect(Math.abs(this.target - value) < epsilon);
});
}