Chai.js plugin to highlight values in error messages Chai generates
Want to see pretty graphs? Log in now!
npm install chai-highlight
|1||downloads in the last week|
|4||downloads in the last month|
|Last Published By|
|Version||0.1.0 last updated 8 months ago|
|Keywords||chai, highlight, error, message, value, customize|
Chai.js plugin to highlight values in error messages Chai generates.
This plugin uses best efforts to identify objects, arrays, strings, regular expressions, numbers, and other important elements comprising a Chai error message (eg, the word not), and highlight them so you can easily differentiate them from surrounding text and inspect and compare their values.
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 = chai.expect, highlight = require('chai-highlight'); chai.use(highlight); // Set custom styles, use HTML instead of ANSI color codes. highlight.setStyles('<b>', '</b>', /<b>/); // Use Chai as before. expect(1).to.be.ok;
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.
Custom styles are set using the
setStyles() method, as illustrated in the above
example. This 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
You can set any number of these in a
setStyles() 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