Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/i-like-robots/chai-html
- Owner: i-like-robots
- License: mit
- Created: 2015-11-04T12:10:22.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2024-10-01T07:12:57.000Z (4 months ago)
- Last Synced: 2024-11-08T14:00:20.322Z (2 months ago)
- Topics: assertions, chai, chai-html, javascript, nodejs
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/chai-html
- Size: 167 KB
- Stars: 6
- Watchers: 3
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- Changelog: changelog.md
- License: LICENSE
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!
').html.to.equal('')
expect('
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 tagexpect('').html.to.equal('')
// throws: attribute [alt="..."] has been addedexpect('
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
').html.ignoringComments.to.equal('')
expect('
```## 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.