Make error message more descriptive #106

alexeypetrushin opened this Issue Nov 2, 2012 · 20 comments


None yet
7 participants

Sometimes error messages for hash comparison looks like that

  currentPage      : 2
  perPage          : 3
  pagesCount       : 3
  currentPagePath  : '/tag/News?page=2'
  firstPagePath    : '/tag/News'
  previousPagePath : '/tag/News'
  nextPagePath     : '/tag/News?page=3'
  lastPagePath     : '/tag/News?page=3'
expected { Object (currentPage, perPage, ...) } to deeply equal { Object (currentPage, perPage, ...) }

looking at such message it's hard to understand what's wrong there.

Maybe add more details?


logicalparadox commented Nov 8, 2012

A few versions ago chai would display both objects in full in the error message and it became to hard to read. As such I reduced it to what you see now. I don't think going back on that change is a good idea, but perhaps there is a better way. Ultimately, I had hoped that test runners/reporters would take up an initiative to do object diffs, but that has not yet occurred.

Open to suggestions.

Yea, new output looks nice, but although old output was huge and ugly - it still was easier to understand what's wrong. Because now in case of error You has to:

  • it's unclear which of expect clauses hasn't been meet - so, You has comment out different clauses and run test multiple time to find the exact clause that hasn't been meet.
  • next You has to put console.log(myObj) and see the result.

I believe the good option would be, as You suggested to show the difference - then error message will be small and descriptive.


domenic commented Nov 25, 2012

Related: #101

Could we make this configurable, perhaps? It's really quite hard to see what's going on when I get messages like:

stacktrace: "expected [ Array(14) ] to deeply equal [ Array(13) ]"

domenic commented Dec 21, 2012

@semperos are you using Mocha? Does it give a diff view? In general I agree with you that the current messages are a disaster, and want to get them fixed if we need to. But @logicalparadox's point that test runners could potentially display the diffs is a good one.

I agree. Are there currently runners that show diff's for things other than strings? I am using Mocha.

At present, I simply keep a few helper functions around to diff arrays and objects myself, and use them to format the optional error messages that assertion libraries allow adding in to individual assertions.


logicalparadox commented Dec 21, 2012

Mocha recently added support for object diffs using JSON.stringify, but a flag has to be manually set for this to occur. I have enabled this flag for equal and .eql / .deep.equal. Please update to chai 1.4.1.

As for another suggestion, which is to make the "short messages" configurable... I agree this should be an option, but currently the utility that manages this doesn't have access to the primary export for which this setting would be stored. I will open a separate issue to explore that more in the future.

Screen Shot 2012-12-21 at 1 01 04 PM

Could you explain how to enable this feature? I still only see this supported for strings in the official docs, and wasn't able to find a reference to how to enable it in Mocha itself.

Nevermind. I see this only works for plain objects, not for arrays, which is what I was working against. Will have to stick to custom messages (or a different library).


logicalparadox commented Dec 21, 2012

Mocha handles this by JSON.stringify for whatever the expected and actual are (code). We send these over for some assertions, but alway for .equal and .deep.equal assertions. I can confirm this works for simple arrays, and should work for anything that can be JSON.stringify...

Make sure you have the latest chai and mocha. I'm using mocha 1.7.4 for the screenshots.

Screen Shot 2012-12-21 at 2 35 55 PM


logicalparadox commented Dec 21, 2012

And to confirm, this requires no addition configuration.

Thanks for stepping through everything.

Using the same versions of Mocha and Chai, comparing two arrays of a simple string values, I see Array(13) where I'd expect to see the fleshed-out output your screenshot shows. Not sure where the difference lies. I'm sure it's something with my setup.


logicalparadox commented Dec 21, 2012

I just tested and can confirm that these will still show up if you have chai.Assertion.includeStack = true. Are you working in the browser?


logicalparadox commented Dec 21, 2012

Looks like I missed this feature for the browser build. Refactoring now...


logicalparadox commented Dec 21, 2012

Ok. Looks like I rushed that last update and missed the browser version. I am not sure if that was why you were not seeing these diffs, but it has been resolve with the shortest time between path versions. Try 1.4.2 and let me know if that works out for you.

I was testing in both Node and the browser. Very possible I was only
looking at the output from the browser run. No longer in front of my
computer, but will give it a shot as soon as possible.

On Friday, December 21, 2012, Jake Luer wrote:

Ok. Looks like I rushed that last update and missed the browser version. I
am not sure if that was why you were not seeing these diffs, but it has
been resolve with the shortest time between path versions. Try 1.4.2 and
let me know if that works out for you.

Reply to this email directly or view it on GitHub

Duznet commented Nov 10, 2013

I'm using Mocha 1.14 and Chai 1.8.1 in browser and have the same problem.

When I use expect(obj1).to.eql(obj2) it displays the error message like AssertionError: expected { Object (vy, vx, ...) } to deeply equal { Object (x, y, ...) }

And I tried to insert chai.Assertion.includeStack = true; but it did not help.
So I don't know how to fix that and to get full message with all object properties

DId anyone figure out a solution to this?

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

@paulyoung yes same thing for me, "DONT forget to return that PROMISE!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment