Chai is a Behavior-Driven Development (BDD) and Test-Driven Development (TDD) assertion library for Node.js and the browser. It can be delightfully paired with any JavaScript testing framework. As an assertion library, similar to Node's built-in assert
, Chai provides developers with a range of assertions that can be used to test the code base, making testing much easier.
Chai can be installed and used in a Node.js environment or within the browser. For Node.js, it can be installed using npm with the command $ npm install --save-dev chai
. After installation, you can import Chai into your code with var chai = require('chai')
. Chai provides three styles of assertions - assert
, expect
, and should
. For example, you can use Chai's assert-style assertions in your code like this:
var chai = require('chai');
var assert = chai.assert;
assert.equal(foo, 'bar', 'foo equals bar');
For browser usage, you can install it via npm and use the chai.js
file found within the download. For example:
<script src="./node_modules/chai/chai.js"></script>
The comprehensive documentation for Chai can be found at http://chaijs.com. The documentation includes installation instructions, detailed usage guides highlighting the three styles of assertions (Assert, Expect, and Should), and also information on how to build a plugin or to find the appropriate plugin from the official plugin list. You can learn more about Chai's robust Plugin architecture which enables you to extend Chai's assertions and interfaces. The documentation is an excellent resource to get started with Chai and to dive deeper into this assertion library.