Chai is a popular assertion library for JavaScript that can be used with any JavaScript testing framework. It provides a simple and readable syntax for writing assertions, making it easy to write tests for our code. In this article, we will show how to install and set up chai in a JavaScript project
Before getting started, we’ll ensure that we have Node.js and npm (Node Package Manager) installed on our machine. We can check this by running the following commands on the terminal:
node -v
npm -v
Once both Node.js is and npm are installed, we can proceed to the installation phase. Else, we can set it up using the Educative course Learn Node.js: The complete course for beginners.
To install Chai, we can use npm
by running the following command on our terminal:
npm install chai
We can also use yarn
, which is another package manager for Node.js
yarn add chai
These commands will install the latest version of Chai in our project and add it to our project's dependencies.
Once Chai is installed, we can configure it to use the assertion style that we prefer. Chai offers three different assertion stylesassert, expect, and should.
To configure Chai, we can use the following steps:
Install Chai as a dependency in our project by running npm install chai
or yarn add chai
as shown in the previous section.
Import the Chai library in our test file by adding the following line at the top of the file:
const chai = require('chai');
Configure Chai to use the preferred assertion style. For example, we can use the "should" style by adding the following line:
const should = chai.should();
Or we can use the "expect" style by adding the following line:
const expect = chai.expect;
Or we can use the "assert" style by adding the following line:
const assert = chai.assert
Use Chai's assertions in our tests. For example, using the "should" style:
var foo = 'bar';foo.should.be.a('string');
Or using the "expect" style:
var foo = 'bar';expect(foo).to.be.a('string');
var foo = 'bar';assert.typeOf(foo, 'string');
Run the tests as usual
Note: We can also use the chai-as-promised package to test promises.
To run a test using Chai in a Node.js application, we can do the following steps:
Initialize NPM: Run the following command to initialize npm in the project:
npm init -y
Install Chai: Run the command npm install --save-dev
chai on the terminal to install Chai as a development dependency for the Node.js project.
npm install --save-dev chai
Choose an assertion style: Chai supports several assertion styles, including expect, should, and assert. We can choose one of these styles based on our personal preference or the style used by our team.
Write tests: For example, using the expect
style. We'll need to create a new file in our project, for example, test.js
and write our tests using Chai.
const chai = require('chai');const expect = chai.expect;describe('example test', () => {it('should pass', () => {expect(1 + 1).to.equal(2);});});
Run the test: Run the test using a test runner, such as Mocha or Jest, by executing the test file in the terminal. For example, if we’re using Mocha, we can run the following command on the Windows terminal to install Mocha globally:
npm i -g mocha
In the terminal, navigate to the project directory and run the following command to run the tests:
mocha test.js
The following should be gotten as output showing that the test was passed:
example test✔ should pass1 passing (51ms)
The terminal below can be used to run the commands above:
If any issue is encountered while installing or setting up Chai, ensure that the latest version of Node.js and npm (or other package managers e.g. yarn) are installed. Also, check if the correct version of Chai is installed in the project. We can check this by running the following command on the terminal:
npm ls chai
Chai is a powerful and easy-to-use assertion library for JavaScript. With its simple and reliable syntax, it makes it easy to write tests for our code. By following the steps outlined in the article, we should be able to install and set up Chai in any JavaScript project and start writing tests with confidence.
Free Resources