Ava: Is there a way to test numbers which are approximately equal?

Created on 30 May 2017  路  9Comments  路  Source: avajs/ava

Ava works like a charm for me, thanks!

I'm currently writing tests which involve comparing numbers, which needs some special treatment. Since numbers can have round off errors, this can lead to cases where tests which should be ok actually fail, like:

test('test', t => {
  t.is(0.1 + 0.2, 0.3)   // should be ok, but fails due to round-off errors
})

It would be great to be able to check whether numbers are approximately equal, both in tests like t.is as well as t.deepEqual. Maybe in the form of methods like approxEqual(value, expected, epsilon) and approxDeepEqual(value, expected, epsilon), or some global setting allowing to configure how numbers are compared (exact or approx). Right now I've written my own helper functions for this, but I think it would be great to have this built in by default.

Most helpful comment

The reason that I come up with this feature request is that I found myself writing helper functions for this in my AVA tests multiple times (in totally unrelated projects). That's why I think this _may_ be something more people than just me have to deal with and hence would be convenient to have out of the box.

A solution like t.truthy(approxDeepEqual(value, expected)) works quite ok. AVA still outputs very useful info in case of a failing assertion like this (i.e. the actual values of both value and expected, and not just false !== true).

I would love to have a solution though which can output the diff of a failed approx deep equal assertion, though, like deepEqual of AVA does. That's one of the things I love so much about AVA :)

Is there a way to write an extension for AVA?

All 9 comments

This seems to be a bit beyond basic AVA. Maybe what you need is an npm package that, given two floats, tells you if they are approximately equal to a given degree (I'm sure this exists).

This can be solved by modules or external assertions. Maybe Chai has something for this.

The reason that I come up with this feature request is that I found myself writing helper functions for this in my AVA tests multiple times (in totally unrelated projects). That's why I think this _may_ be something more people than just me have to deal with and hence would be convenient to have out of the box.

A solution like t.truthy(approxDeepEqual(value, expected)) works quite ok. AVA still outputs very useful info in case of a failing assertion like this (i.e. the actual values of both value and expected, and not just false !== true).

I would love to have a solution though which can output the diff of a failed approx deep equal assertion, though, like deepEqual of AVA does. That's one of the things I love so much about AVA :)

Is there a way to write an extension for AVA?

Is there a way to write an extension for AVA?

Any assertion that throws an error causes the test to fail. #1277 tracks integrating other assertion libraries with AVA's error output.

Cool, thanks Mark

@josdejong How are you approaching this nowadays? Would you recommend a specific package?

There where I use Ava I've created a util function to test whether two numbers are approximately equal. In other projects I use Jest, which has a method .toBeCloseTo(number, numDigits?)

Was this page helpful?
0 / 5 - 0 ratings