Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/i-like-robots/chai-html

☕️ A focussed HTML assertions plugin for Chai.
https://github.com/i-like-robots/chai-html

assertions chai chai-html javascript nodejs

Last synced: 2 months ago
JSON representation

☕️ A focussed HTML assertions plugin for Chai.

Awesome Lists containing this project

README

        

# chai-html

[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/i-like-robots/chai-html/blob/main/LICENSE) ![build status](https://github.com/i-like-robots/chai-html/actions/workflows/test.yml/badge.svg?branch=main) [![npm version](https://img.shields.io/npm/v/chai-html.svg?style=flat)](https://www.npmjs.com/package/chai-html)

A focussed HTML assertions plugin for [Chai](http://chaijs.com/).

## Installation

This is a [Node.js] module available through the [npm] registry. Node.js 18 or higher is required.

```sh
$ npm install --save-dev chai-html
```

👋 _Please note_ this package is for Chai v5 and is [ESM](https://nodejs.org/api/esm.html) only. For Chai v4 and CommonJS support use [version 2][v2] of this package.

[Node.js]: https://nodejs.org/en/
[npm]: https://www.npmjs.com/
[v2]: https://github.com/i-like-robots/chai-html/tree/v2

## Usage

```js
import { use, expect } from 'chai'
import chaiHtml from 'chai-html'

// Register the plugin
use(chaiHtml)

// Write assertions!
expect('

').html.to.equal('
')
expect('

Hello World!

').html.to.not.equal('

Hallo Welt!

')
```

HTML assertions will throw an error directing you to the change, so instead of squinting at a long HTML string you'll get something useful:

```js
expect('').html.to.equal('
')
// throws: tag was changed to tag

expect('').html.to.equal('...')
// throws: attribute [alt="..."] has been added

expect('

Hello World!

Hej!').html.to.equal('

Hello World!

')
// throws: text " Hej!" has been removed
```

### .ignoringComments

Add the `ignoringComments` flag to the chain to ignore HTML comments.

```js
expect('

').html.ignoringComments.to.equal('
')
```

## How does it work?

Underneath this plugin uses [parse5](https://github.com/inikulin/parse5) to parse the given HTML strings and normalize the generated trees before being compared. This means that although the two strings of markup may not be the same they should generate equivalent structures.

## Credits

This plugin is inspired by the similar [chai-xml](https://www.npmjs.com/package/chai-xml) plugin.

## License

This package is MIT licensed.