Is there a assertion library that will show me what are the differences between two objects when compared deeply ?
I've tried using chai but it just tells me that the objects are different but not where. Same thing for node's assert....
Is there a assertion library that will show me what are the differences between two objects when compared deeply ?
I've tried using chai but it just tells me that the objects are different but not where. Same thing for node's assert....
Substack's difflet is probably what you need
Update: but wait, there is more: https://github.com/andreyvit/json-diff https://github.com/algesten/jsondiff https://github.com/samsonjs/json-diff
Using chai 1.5.0 and mocha 1.8.1, the following works for me:
var expect = require('chai').expect;
it("shows a diff of arrays", function() {
expect([1,2,3]).to.deep.equal([1,2,3, {}]);
});
it("shows a diff of objects", function() {
expect({foo: "bar"}).to.deep.equal({foo: "bar", baz: "bub"});
});
results in:
✖ 2 of 2 tests failed:
1) shows a diff of arrays:
actual expected
1 | [
2 | 1,
3 | 2,
4 | 3,
5 | {}
6 | ]
2) shows a diff of objects:
actual expected
{
"foo": "bar",
"baz": "bub"
}
What does not show here is that the output is highlighted red/green where lines are unexpected/missing.
Based on this StackOverflow answer, I believe the issue was occuring for me because my tests were asynchronous.
I got diffs working correctly again by using the following pattern:
try {
expect(true).to.equal(false);
done(); // success: call done with no parameter to indicate that it() is done()
} catch(e) {
done(e); // failure: call done with an error Object to indicate that it() failed
}
Yes there is: assert-diff
You can use it like this:
var assert = require('assert-diff')
it('diff deep equal with message', function() {
assert.deepEqual({pow: "boom", same: true, foo: 2}, {same: true, bar: 2, pow: "bang"}, "this should fail")
})
results in:
1) diff deep equal with message:
AssertionError: this should fail
{
- bar: 2
+ foo: 2
- pow: "bang"
+ pow: "boom"
}