How to have mocha show entire object in diff on assertion error?
You can add --inline-diffs
to the mocha command, which will show the entire object with line numbers and inline diffs:
mocha --inline-diffs YourSpec.js
The documentation is a bit misleading: https://mochajs.org/#diffs
As far as I know, there's no built-in way to get Chai or Mocha to produce diffs that would add to the context provided by the diff some fields that are not responsible for the test failure. And I'm not aware of any extension that would exactly what you are looking for. So I only know of workarounds.
If you set truncateThreshold
configuration setting to a larger value, or to 0
if you don't want any truncation, the failure message that appears before the diff will show whole objects. So if I add this to your code:
chai.config.truncateThreshold = 0; // 0 means "don't truncate, ever".
(The configuration options are covered on this documentation page.)
Then the error I get is:
AssertionError: expected { a: 1,
troiz: 0,
bar: 0,
baz: 2,
poit: 3,
narf: 4,
fizzbuzz: 117,
fizz: 5,
buzz: 4,
waldo: 115,
mos: 85465,
important: 'THIS IS IMPORTANT' } to deeply equal { a: 0,
troiz: 0,
bar: 0,
baz: 2,
poit: 3,
narf: 4,
fizzbuzz: 117,
fizz: 5,
buzz: 4,
waldo: 115,
mos: 85465,
important: 'THIS IS IMPORTANT' }
+ expected - actual
{
- "a": 1
+ "a": 0
"bar": 0
"baz": 2
"buzz": 4
"fizz": 5
Moreover, a way to get custom information in the error message is to set a custom message with the assertion, for instance:
expect(actual).to.deep.equal(
expected,
`failed equality test on object with important field set to: ${actual.important}`)
The custom message can be as verbose or terse as you need it to be.
If there is only one field that matters to you for differentiating objects, a custom message may be sufficient to get the information you need to track the problem.
For arrays of objects, you could perform a comparison by iterating through the actual
and expected
arrays and comparing each member. It would then essentially work like described above. For sure, this has disadvantages: if items 1 and 10 are different, for instance, you'll get a report only for item 1 because the test will end with that comparison failure. When you fix this problem and run the test again, you'll get a report for item 10. Whether or not in practice this is a major problem depends the kind of data you are testing.
One thing I've done in cases where the default diff algorithm was not doing what I wanted was to import a diffing library that I configured to my liking, and then use the library to perform diffs between objects that I cared about and combine the results into a final report that is then checked with an assertion.
Again, I'm not aware of a library that would do specifically what you are looking for. However, I could imagine iterating through the actual
and expected
arrays to produce one diff report per pair and then combining them into a larger report that contains identifying information.