What is chai?
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework. It provides functions to express assertions in a readable language, supporting both 'should' and 'expect' style syntaxes, as well as a traditional 'assert' style interface.
What are chai's main functionalities?
Should Style Assertions
Allows you to write assertions in a more human-readable form, which reads like English. The 'should' style extends each object with a 'should' property to start your chain.
chai.should();
var should = chai.should();
should.exist(value, 'value should not be null or undefined');
Expect Style Assertions
Provides a function called 'expect' to make assertions about values. This style is similar to 'should' but does not extend any objects, which is useful for working with libraries that don't allow object extensions.
var expect = chai.expect;
expect(value).to.exist.and.be.an('object');
Assert Style Assertions
Offers a more traditional testing interface, which is similar to Node.js' core 'assert' module but with more capabilities and better error messages.
var assert = chai.assert;
assert.typeOf(value, 'string', 'value is a string');
Chainable Language Chains
Provides chainable getters to improve the readability of your assertions, allowing you to write more expressive tests.
expect(value).to.be.an('array').that.includes(3);
Plugins
Supports plugins to extend its core functionalities, which can be used to add new assertions, matchers, or methods to the library.
var chaiHttp = require('chai-http');
chai.use(chaiHttp);
Other packages similar to chai
jest
Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It works out of the box for any React project and includes its own assertion library, which provides a similar but more integrated testing experience compared to Chai.
jasmine
Jasmine is a behavior-driven development framework for testing JavaScript code. It does not rely on browsers, DOM, or any JavaScript framework, making it suitable for websites, Node.js projects, or anywhere JavaScript can run. It has its own assertion syntax that is somewhat similar to Chai's 'expect' syntax.
expect.js
Expect.js is a minimalistic BDD-style assertions library that can be considered an alternative to Chai's 'expect' interface. It provides a similar API but is no longer actively maintained.
chai
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.
For more information or to download plugins, view the documentation.
What is Chai?
Chai is an assertion library, similar to Node's built-in assert
. It makes testing much easier by giving you lots of assertions you can run against your code.
Installation
Node.js
chai
is available on npm. To install it, type:
$ npm install --save-dev chai
Browsers
You can also use it within the browser; install via npm and use the chai.js
file found within the download. For example:
<script src="./node_modules/chai/chai.js"></script>
Usage
Import the library in your code, and then pick one of the styles you'd like to use - either assert
, expect
or should
:
var chai = require('chai');
var assert = chai.assert;
var expect = chai.expect;
var should = chai.should();
Pre-Native Modules Usage (registers the chai testing style globally)
require('chai/register-assert');
require('chai/register-expect');
require('chai/register-should');
Pre-Native Modules Usage (as local variables)
const { assert } = require('chai');
const { expect } = require('chai');
const { should } = require('chai');
should();
const { expect, use } = require('chai');
Native Modules Usage (registers the chai testing style globally)
import 'chai/register-assert';
import 'chai/register-expect';
import 'chai/register-should';
Native Modules Usage (local import only)
import { assert } from 'chai';
import { expect } from 'chai';
import { should } from 'chai';
should();
Usage with Mocha
mocha spec.js -r chai/register-assert
mocha spec.js -r chai/register-expect
mocha spec.js -r chai/register-should
Read more about these styles in our docs.
Plugins
Chai offers a robust Plugin architecture for extending Chai's assertions and interfaces.
- Need a plugin? View the official plugin list.
- Want to build a plugin? Read the plugin api documentation.
- Have a plugin and want it listed? Simply add the following keywords to your package.json:
chai-plugin
browser
if your plugin works in the browser as well as Node.jsbrowser-only
if your plugin does not work with Node.js
Related Projects
Contributing
Thank you very much for considering to contribute!
Please make sure you follow our Code Of Conduct and we also strongly recommend reading our Contributing Guide.
Here are a few issues other contributors frequently ran into when opening pull requests:
- Please do not commit changes to the
chai.js
build. We do it once per release. - Before pushing your commits, please make sure you rebase them.
Contributors
Please see the full
Contributors Graph for our
list of contributors.
Core Contributors
Feel free to reach out to any of the core contributors with your questions or
concerns. We will do our best to respond in a timely manner.