.to.deep.equal() with tolerance
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:8
- Comments:8 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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
You can use closeTo individual properties and assert that they are close to a number, within a given delta. For example:
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:
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.