question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chai's deepEqual does not show an object diff when run in mocha with Karma

See original GitHub issue

When using mocha and chai in a Node.js environment, when an assertion using deepEquals fails, you get a detailed output showing piece by piece what is different between the expected and actual values.

Take for example, these tests, located in a file, tests/objects_are_equal.js:

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

describe('nice output', function(){
  it('should fail this test with a nice output when objects are not equal', function(){
    var objectA = {
      a: 'some string',
      b: 9,
      c: 20,
      d: 100,
    },
    objectB = {
      a: 'some other string',
      b: 10,
      g: '100',
    };

    expect(objectA).to.eql(objectB);
  });
  it('should fail this test with a nice output when arrays are not equal', function(){
    var arrayA = ['a', 6, 10, 9, 20, '15', 20, 30, 45, 60],
      arrayB = ['c', 2, '8', '15', 8, 30, '8', 5, 10, 'a', 8, '60'];

    expect(arrayA).to.eql(arrayB);
  });
});

If you run the above tests with mocha in a node.js environment, with the command mocha, you get the following output: screenshot 2015-10-08 10 11 32

But if you run them using karma-mocha, you get the following output: screenshot 2015-10-08 10 12 51

This makes it really challenging to debug why object comparisons didn’t work, and I find myself having to do console.log on the objects in the test to see what is different between objects, which is not ideal.

According to an issue in chai:

…with diffing…it is in the test harness domain, so if you are not seeing useful diffs, I’d kindly ask you raise the issue with your test harness, for example Mocha.

I’m not sure if this bug belongs in karma-mocha or karma.

The bug still happens if the tests run in Chrome or Phantomjs, and if I load chai.js directly onto the page instead of via a karma-chai.

For the reference, here is my karma.conf file:

module.exports = function(config) {
  config.set({
    basePath: '',

    frameworks: ['mocha', 'chai'],

    files: [
      'test/*.js'
    ],

    reporters: ['progress'],

    colors: true,

    logLevel: config.LOG_INFO,

    browsers: ['Chrome'],
  })
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

49reactions
ItsCosmocommented, Mar 25, 2016

If you use karma-mocha-reporter, there is a “showDiff” configuration option:

mochaReporter: {
    showDiff: true
}
5reactions
NickHeinercommented, May 8, 2017

@ItsCosmo wow, it’s so much better with that option set. Thanks! I wonder why that isn’t the default.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deep Diff Test Failures in Karma with Mocha - Stack Overflow
The test fails as expected, but the error message is not at all helpful. AssertionError: expected { Object (a, b, ...) } to...
Read more >
Getting Started with Node.js and Mocha - Semaphore Tutorial
Mocha is a simple, extensible and fast testing library for Node.js. This article will walk you through its installation, configuration and usage.
Read more >
Is there any way to see a diff on a deep equal assertion failure?
Using the example data structure, 'deep equal', 'equal', and 'like' all provide the expected diff. Perhaps stringification improved at some point in chai/mocha?...
Read more >
API Reference - Chai Assertion Library
Asserts that value is an object of type 'Object' (as revealed by Object.prototype.toString ). The assertion does not match subclassed objects.
Read more >
Unit testing Node.js applications using Mocha, Chai, and Sinon
Properly written tests ensure that new features do not introduce changes ... Mocha is a feature-rich JavaScript test framework running on ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found