foobarcode foobarcode - 3 months ago 21
Node.js Question

nodejs deep equal with differences

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....

Answer

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.

Comments