Search code examples
javascripttestingtestcafebrowser-automationend-to-end

What is the best way to perform an assertion for numbers that are off by ".1" in testcafe


I need some assistance.This is a weird one.Basically I am writing out some testcafe tests where: a user goes to the order summary page and verifies that the total on the check out page matches the total on the order details page.

The issue: There are instances where the order details total is off by a penny(not a bug worthy of fixing any time soon. according to the developers).So for example, on the checkout page your order total is $3.50. On the order details page the total is $3.51

Is there a way to combat a penny extra in a testcafe assertion?

Here is what my assertion looks like:

await t
        .expect(totalOnCheckoutPage)
        .eql(totalOnOrderDetails);

totalOnCheckoutPage and totalOnOrderDetails are selector variables.


Solution

  • You can write custom util to round. Sample

    function round(x, precision) {
        var y = +x + (precision === undefined ? 0.5 : precision/2);
        return y - (y % (precision === undefined ? 1 : +precision));
    }
    console.log(round(3.51, 0.1))
    console.log(round(3.55, 0.1))

    Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round