Chai.js plugin to highlight values in error messages Chai generates
Chai.js plugin to highlight values in error messages and NPM module names in stack traces that Chai generates.
This plugin uses best efforts to identify objects, arrays, strings, regular expressions, numbers, and other important elements (eg, the word not) comprising a Chai error message, and highlight them so you can easily differentiate them from surrounding text and inspect and compare their values.
This plugin also marks NPM module names in the accompanied stack traces, so that you can see at a glance whether each source file in the stack trace is your own code or from a third-party module, and in the latter case, which module.
This example shows Mocha's error presentation with this plugin being used and not used, side by side. When using this plugin, it's easier to quickly grasp the essence of the error message, and it's also easier to compare the printed values to see why the expectation is violated.
Note that we are using the
containAtIndex assertion from Chai Common
in this example. This plugin doesn't support styling error messages generated by
include assertions in Chai's core, because they are chainable
methods. See Caveats below for more info.
npm install chai-highlight
var chai = require'chai'expect = chaiexpecthighlight = require'chai-highlight';chaiusehighlight;// Set custom styles, use HTML instead of ANSI color codes.highlightsetStyles'<b>' '</b>' /<b>/;highlightsetMarkStyles'<i>' '</i>';// Use Chai as before.expect1tobeok;
For value highlights in error messages, the default style is bold, which is a sensible default because it works well with different color schemes. But you can easily change it to anything you want, without being limited to ANSI color codes at all.
For NPM module name markings in stack traces, the default style is a cyan background.
Custom styles are set using the
setMarkStyles() methods, as
illustrated in the above example.
setStyles() method takes three arguments:
highlightStyle. The style used by highlighted text. The default is
restoreStyle. The sequence used to restore the style to the default for non-highlighted text. The default is
stylePattern. A regular expression. If the error message matches this regex, we don't style it. This is intended to not style error messages already styled by assertion authors. The default is
setMarkStyles() method takes two arguments:
markStyle. The style used by marked NPM module names. The default is
restoreMarkStyle. The sequence used to restore the style to the default for non-marked text. The default is
You can set any number of these in a
setMarkStyles() call, if
a falsy value is passed in, that parameter isn't changed.
This plugin is implemented by overwriting all assertion properties and methods in the
Assertion object. Don't worry, for passing tests, the impact on performance should
be minimal. But the problem is that, Chai doesn't support overwriting chainable methods,
so error messages generated by them aren't styled.
Luckily, there are only a few of them in Chai's core, including