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.
Hey @Turbo87 thanks for the issue.
We've got a discussion going on over here: https://github.com/chaijs/chai/issues/644 which would probably cater your needs. I'll close this for now, but if you dont think https://github.com/chaijs/chai/issues/644 is the right solution then let me know here or over on that issue 馃槃
I'm not sure if #644 will enable what I would like to do. Basically I'm testing some computation-heavy code that results in a large JSON object with several numbers that need to be checked. I then load a second JSON object fixture and would like to compare those two objects with as little code as possible.
expect(result).to.roughly(0.0001).deep.equal(expected);
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.
how about something roughly like this:
function compare(a, b) {
if (typeof a === 'number' && typeof b === 'number') {
// using the expect() semantics here might not be the best idea...
expect(a).to.be.closeTo(b, 0.0001);
} else {
return this._super(a, b);
}
}
expect(result).to.deep.equal(expected, compare);
Hopefully that will be somewhat close to what we end up with, with #644. Comparators will be able to be passed to deep-eql, which we will use to fuel #644 馃槃
You should comment in #644 with the above example, and we can track it in that issue - as it is still going to be a ways off but would be useful to track in one place.
@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
Great work @Turbo87 馃槃
Most helpful comment
@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