7

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

foobarcode
  • 2,279
  • 1
  • 23
  • 24
  • What testing framework are you using? Are you open to switching? – David Weldon Dec 10 '12 at 01:51
  • I'm using mocha, I'm open for change yes. But I really like mocha ;) – foobarcode Dec 10 '12 at 22:20
  • Well that's funny - I was going to suggest you use mocha. :) So maybe I'm confused here because mocha gives you string diffs between the actual and expected values on an assertion failure. I think that's independent of the assertion library, though it woks fine for me with should.js. Is that what you want, or are you looking to print diffs even when an assertion does not fail? – David Weldon Dec 10 '12 at 22:29
  • 2
    I've tried should, when I compare two different object it just tells me "Error: AssertionError: expected {....} to equal {....}" without showing me anywhere the actual differences. I'm on windows, so don't know if it could change the behaviour of the library in any way... – foobarcode Dec 10 '12 at 23:02
  • Huh. So for me it looks like the image on the 'String diffs' section from the [home page](http://visionmedia.github.com/mocha/). It would be weird if this problem was specific to windows. Could the reporter make a difference? I'm using the list reporter. – David Weldon Dec 11 '12 at 00:03
  • The string diff actually work as expected, it shows me the differences. But if I try to pass objects, it doesn't...:( – foobarcode Dec 11 '12 at 13:14
  • 1
    I have the same problem, but I haven't found a solution either. – Eloff Mar 09 '13 at 23:26
  • 1
    Did anyone figure out why this was happening? – Paul Young Jun 11 '14 at 16:35

4 Answers4

3

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

Andrey Sidorov
  • 24,905
  • 4
  • 62
  • 75
  • 2
    Thanks for the pointer, if I was just looking for a command line tool that would be perfect. But I'm looking for an assertion function to use during unit testing. – foobarcode Dec 10 '12 at 22:26
2

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.

Elliot Foster
  • 1,664
  • 1
  • 12
  • 11
2

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
}
Community
  • 1
  • 1
Paul Young
  • 1,489
  • 1
  • 15
  • 34
2

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"
 }
pihvi
  • 309
  • 2
  • 5