question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.to.deep.equal() with tolerance

See original GitHub issue

I would like to use expect(a).to.deep.equal(b) to compare two JSON objects, but the objects contain floating-point numbers that are only roughly the same. Is there some way to compare those two objects with a tolerance when comparing numbers? i.e. when the algorithm compares numbers it will accept a difference of up to 0.001% or something like that.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:8
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
Turbo87commented, May 18, 2016

@keithamus FWIW I implemented my first example as a chai plugin: https://github.com/Turbo87/chai-roughly

it’s using my branch from https://github.com/chaijs/deep-eql/pull/10 underneath

1reaction
keithamuscommented, May 18, 2016

You can use closeTo individual properties and assert that they are close to a number, within a given delta. For example:

expect(1.5).to.be.closeTo(1, 0.5);

Deep Equal will currently only compare like for like, but the plan with #644 is to get it to offer that kind of flexibility, so you could write something like:

expect({
  foo: 1.5
}).to.deep.equal({
  foo: expect.to.be.closeTo(1, 0.5)
});

While your example would solve your specific use case, I’m not sure it would be generic enough to put into chai core. Having said that, when #644 is done there could be the scope for you to create a plugin to modify how deep.equal compares values - so your example code would be possible then.

Read more comments on GitHub >

github_iconTop Results From Across the Web

chai-almost - Chai Assertion Library
Extends chai with assertions that allow numbers to be “almost” equal (as in, within some tolerance of one another). This is useful in...
Read more >
How can I test floating-point equality using chai?
We're using Chai's BDD API to write unit tests. How can we assert floating point equality?
Read more >
Equal Constraint | NUnit Docs
An EqualConstraint is used to test whether an actual value is equal to the expected value supplied in its constructor, optionally within a...
Read more >
cmp - Go Packages
DeepEqual for comparing whether two values are semantically equal. ... as equal so long as they are within some tolerance of each other....
Read more >
stopping_tolerance — H2O 3.38.0.3 documentation
stopping_tolerance ¶ · Available in: GBM, DRF, Deep Learning, GLM, GAM, AutoML, XGBoost, Isolation Forest · Hyperparameter: yes ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found