How can I get Chai to show actual and expected values using toString ()
I recently switched from should.js to chai.js as I found the former was causing clutter in browser based testing. This change did not require any changes in my test suite as the syntax is supported, but I see that the output of the failed tests no longer shows me the actual and expected values in a useful way:
AssertionError: expected [ Array(9) ] to deeply equal [ Array(9) ]
I can get it to spit out a representation of these values by adding this line:
chai.config.truncateThreshold = 0;
However, this leads to the fact that each value is exhaustively displayed, including functions, and including prototype properties. Also pretty useless.
So, is there some way I am missing for chai to behave like should.js, where the actual / expected values are shown using the toString () method?
source to share
One way to get Chai (v1.10.0) to display actual and expected values with .toString()
is to schedule it utils.objDisplay
at runtime.
Basic meaning:
chai.use(function(_chai,utils) {
utils.objDisplay = function(obj) { return obj+''; };
});
However, this is a little more difficult to do in practice due to the way Chai creates duplicate (ie:) !==
references to internal modules; in this case, you must also install the patch utils.getMessage
.
Here is a fiddle showing a generic patch alongside a trivial example of custom formatting for objects Array
: https://jsfiddle.net/humbletim/oc1tnqpy/
source to share