سؤال

I'm using Karma with Jasmine for my tests. In some tests, I have large objects that the test relies on. When I do something like

expect(obj).toEqual(expectedObj);

and obj != expectedObj, I get an error message in my terminal. But this error is really long, because it includes both of the objects, and it's very hard to see, in what parts the two objects are different.

So, is there any highlighter for the terminal, that can be used along with karma? This way, it would be much more easy to figure out, what's wrong.

هل كانت مفيدة؟

المحلول

I had the same problem and what did it for me was karma-jasmine-diff-reporter.

Just install it:

npm install karma-jasmine-diff-reporter --save-dev

and configure it as a reporter, eg:

// karma.conf.js 
module.exports = function(config) {
  config.set({     

    reporters: ['jasmine-diff']     

  });
};

You can configure it to pretty print:

    // karma.conf.js 
    module.exports = function(config) {
      config.set({     

        reporters: ['jasmine-diff'],     

        jasmineDiffReporter: {
            pretty: true, // 2 spaces by default for one indent level
            matchers: {
                toEqual: {
                    pretty: false   // disable pretty print for toEqual
                }
            }
        }         
      });
    };

Output will be something like this:

Output example

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top