npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chai-highlight

v0.2.0

Published

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

Downloads

3

Readme

chai-highlight

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.

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>/);
highlight.setMarkStyles('<i>', '</i>');

// Use Chai as before.
expect(1).to.be.ok;

Custom Styles

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 setStyles() and setMarkStyles() methods, as illustrated in the above example.

The setStyles() 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\[/.

The setMarkStyles() method takes two arguments:

  1. markStyle. The style used by marked NPM module names. The default is '\x1B[46m'.

  2. restoreMarkStyle. The sequence used to restore the style to the default for non-marked text. The default is '\x1B[49m'.

You can set any number of these in a setStyles() or setMarkStyles() 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