raimohanska
raimohanska

Reputation: 3405

Formatting objects in chai.expect errors

When test fails, where I'm comparing two objects using expect(x).to.deep.equal(y), I'd like to see the actual values in my browser test report. Instead, I see something like this:

AssertionError: expected { Object (x, y, ...) } to deeply equal { Object (x, y, ...) }

So it doesn't really show anything useful.

Is there a way to customize how chai.js formats these objects?

Upvotes: 10

Views: 1621

Answers (2)

wolverdude
wolverdude

Reputation: 1663

You can now configure the max length before an object gets truncated as per the docs:

chai.config.truncateThreshold = 0; // disable truncating

Upvotes: 7

gustavohenke
gustavohenke

Reputation: 41440

Not really. This is hardcoded into Chai.
The following function is their object formatter (source here), which does exactly what you posted:

} else if (type === '[object Object]') {
  var keys = Object.keys(obj)
    , kstr = keys.length > 2
      ? keys.splice(0, 2).join(', ') + ', ...'
      : keys.join(', ');
  return '{ Object (' + kstr + ') }';

Upvotes: 0

Related Questions