UNPKG

@serenity-js/assertions

Version:

Serenity/JS universal assertion library supporting all types of functional tests, including both web and REST API scenarios

39 lines 1.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isCloseTo = void 0; const core_1 = require("@serenity-js/core"); /** * Produces an [expectation](https://serenity-js.org/api/core/class/Expectation/) that is met when the actual value * is within a given ± `absoluteTolerance` range of the `expected` value. * * ## Ensuring that a given floating point number is close to the expected number * * ```ts * import { actorCalled } from '@serenity-js/core' * import { Ensure, isCloseTo } from '@serenity-js/assertions' * * await actorCalled('Iris').attemptsTo( * Ensure.that(10.123, isCloseTo(10, 0.2)) * ) * ``` * * @param expected * @param [absoluteTolerance=1e-9] * Absolute ± tolerance range, defaults to `1e-9` * * @group Expectations */ exports.isCloseTo = core_1.Expectation.define('isCloseTo', (expected, absoluteTolerance = 1e-9) => (0, core_1.d) `have value close to ${expected} ±${absoluteTolerance}`, // eslint-disable-next-line @typescript-eslint/no-inferrable-types (actual, expected, absoluteTolerance = 1e-9) => { // short-circuit exact equality if (actual === expected) { return true; } if (!(Number.isFinite(actual) && Number.isFinite(expected))) { return false; } const difference = Math.abs(actual - expected); return difference <= absoluteTolerance; }); //# sourceMappingURL=isCloseTo.js.map