Nodes deeply equal with differences

Is there a statement library that will show me what the differences between the two objects are in a deep comparison?

I tried to use chai, but it just tells me that the objects are different, but not there. Same thing for node assert ....

+6
source share
4 answers

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"}); }); 

leads to:

 โœ– 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 is not shown here is that the output is highlighted in red / green, where the lines are unexpected / missing.

+2
source

Substack difflet is probably what you need

Update: but wait, there is still: https://github.com/andreyvit/json-diff https://github.com/algesten/jsondiff https://github.com/samsonjs/json-diff

+3
source

Based on fooobar.com/questions/77348 / ... , I believe that the problem arose for me because my tests were asynchronous.

I got diffs again 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 } 
+2
source

Yes there is: assert-diff

You can use it as follows:

 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") }) 

leads to:

 1) diff deep equal with message: AssertionError: this should fail { - bar: 2 + foo: 2 - pow: "bang" + pow: "boom" } 
0
source

All Articles