How to install chai in JavaScript

Chai

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

Prerequisites

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
checking node version
npm -v
checking npm version

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.

    

Installation

To install Chai, we can use npm by running the following command on our terminal:

npm install chai
Installing chai using npm

We can also use yarn, which is another package manager for Node.js

yarn add chai
installing chai using yarn

These commands will install the latest version of Chai in our project and add it to our project's dependencies.

    

Configuration

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:

  1. Install Chai as a dependency in our project by running npm install chai or yarn add chai as shown in the previous section.

  2. Import the Chai library in our test file by adding the following line at the top of the file:

const chai = require('chai');
importing chai
  1. 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();
importing the should assertion style

Or we can use the "expect" style by adding the following line:

const expect = chai.expect;
importing the expect assertion style

Or we can use the "assert" style by adding the following line:

const assert = chai.assert
importing the assert assertion style
  1. Use Chai's assertions in our tests. For example, using the "should" style:

var foo = 'bar';
foo.should.be.a('string');
should assertion style

Or using the "expect" style:

var foo = 'bar';
expect(foo).to.be.a('string');
expect assertion style
var foo = 'bar';
assert.typeOf(foo, 'string');
assert assertion style
  1. Run the tests as usual

Note: We can also use the chai-as-promised package to test promises.

Example

To run a test using Chai in a Node.js application, we can do the following steps:

  1. Initialize NPM: Run the following command to initialize npm in the project:

npm init -y
initialize npm
  1. 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
  1. 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.

  2. 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);
});
});
test.js
  1. 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
installing mocha

In the terminal, navigate to the project directory and run the following command to run the tests:

mocha test.js
using mocha to run the test

The following should be gotten as output showing that the test was passed:

example test
✔ should pass
1 passing (51ms)
output showing that the test was passed

The terminal below can be used to run the commands above:

Terminal 1
Terminal
Loading...

    

Troubleshooting

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
checking the correct version of chai

    

Conclusion

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

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved