chai-highlight

Chai.js plugin to highlight values in error messages Chai generates

npm install chai-highlight
4 downloads in the last month

chai-highlight

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.

Example

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.

Example

Note that we are using the containAtIndex assertion from Chai Common in this example. This plugin doesn't support styling error messages generated by the contain and include assertions in Chai's core, because they are chainable methods. See Caveats below for more info.

Install

npm install chai-highlight

Usage

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;

Custom Styles

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:

  1. highlightStyle. The style used by highlighted text. The default is '\x1B[1m'.

  2. restoreStyle. The sequence used to restore the style to the default for non-highlighted text. The default is '\x1B[22m'.

  3. 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 /\x1B\[/.

You can set any number of these in a setStyles() call, if a falsy value is passed in, that parameter isn't changed.

Caveats

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 a, an, contain, include, and length.

License

MIT License

npm loves you