Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deep Diff Test Failures in Karma with Mocha

I am running the following test:

describe("objects", function () {
    it("should equal", function () {
        var a = {
            a: 1,
            b: 2,
            c: {
                a: 1,
                b: 2,
                c: {
                    a: 1,
                    b: 2,
                    x: 3
                }
            }
        };

        var b = {
            a: 1,
            b: 2,
            c: {
                a: 1,
                b: 2,
                c: {
                    a: 1,
                    b: 2,
                    x: 4
                }
            }
        };
        a.should.deep.equal(b);
    });
});

The test fails as expected, but the error message is not at all helpful.

AssertionError: expected { Object (a, b, ...) } to deeply equal { Object (a, b, ...) }

How would I get it so that it outputs a prettified json comparison instead?

Libraries I am currently using:

  • karma 0.12.1
  • karma-mocha 0.1.6
  • karma-mocha-reporter 0.3.0
  • karma-chai 0.1.0
like image 614
Ken Hirakawa Avatar asked Aug 22 '14 00:08

Ken Hirakawa


1 Answers

You can change where the message gets truncated with the following:

chai.config.truncateThreshold = 0

So for your example:

var chai = require('chai');
var expect = chai.expect;

chai.config.truncateThreshold = 0;

describe.only("objects", function () {
    it("should equal", function () {
        var a = {
            a: 1,
            b: 2,
            c: {
                a: 1,
                b: 2,
                c: {
                    a: 1,
                    b: 2,
                    x: 3
                }
            }
        };

        var b = {
            a: 1,
            b: 2,
            c: {
                a: 1,
                b: 2,
                c: {
                    a: 1,
                    b: 2,
                    x: 4
                }
            }
        };
        expect(a).to.deep.equal(b);
    });
});

Which will result in:

AssertionError: expected { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2, x: 3 } } } to deeply equal { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2, x: 4 } } }
+ expected - actual

     "b": 2
     "c": {
       "a": 1
       "b": 2
+      "x": 4
-      "x": 3
     }
   }
 }
like image 102
Adrian Lynch Avatar answered Sep 29 '22 20:09

Adrian Lynch