Table of contents
1.
Introduction
2.
Chai.js installation
2.1.
Node.js
2.2.
Browser
3.
Test Cases in Chai
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Getting Started with Chai.js

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Testing is done on any application, website, or program to check the functionality/behavior of the code. Test cases are written to validate the functionality of the code against customer requirements. The tests can be performed on classes, methods, or other blocks. We have many frameworks and libraries available for testing. We will discuss about Chai - a testing library, in this article.

Chai is a JavaScript library that supports testing. It is a BDD(Behaviour-driven Development)/TDD(Test-driven Development) assertion library for node. Chai provides us with a clear syntax primarily written in English, making it easy for everyone to write test cases with a basic knowledge of Testing. Let’s learn how to install the Chai library and write test cases now.

Chai.js installation

Chai is available for any browser, server like node.js, or framework. Mostly Chai is used along with Mocha, a testing framework similar to Chai. Let’s install the Chai using the commands provided below.

Node.js

We can use the npm(Node Package Manager) to install Chai in Node.js. Create a folder in which you want to create the test files and run the following command.

npm install chai


Source

After the installation is done successfully, add Chai to DevDependencies in the package.json file. 

"devDependencies": {
  "chai": "*",
}


You can also add any other frameworks or libraries according to your requirements. 

Browser

You can also include the Chai browser directly into your code by including the Script tags given below.

<script src="chai.js" type="text/javascript"></script>


You can include Chai.js of your preferred version in this way without any installation. Remember to check if your browser is compatible with the version you include in your code. Now we are done with the installation. Let’s learn how to write the basic test cases in Chai.

Test Cases in Chai

We have to create a file with the code we want to test first to write test cases. So let’s start with a basic arithmetic operations code for better understanding.

class ArithmeticOperations {
  static addition(num1, num2) {
    return num1 + num2;
  }
  static substraction(num1, num2) {
    return num1 - num2;
  }
  static multiplication(num1, num2) {
    return num1 * num2;
  }
  static division(num1, num2) {
    return num1/num2;
  }
}

module.exports = ArithmeticOperations ;
You can also try this code with Online Javascript Compiler
Run Code


Create a JavaScript file with any name a .js extension inside the project folder. We should create a class ArithmeticOperations with static methods addition, subtraction, multiplication, and division inside this file. The methods will return the result of the arithmetic operations respectively.

Now let’s learn how to write test cases for the above code.

const chai = require('chai');
const calculations = require('../app/calculations')

chai.should();

describe('Addition of two numbers', () => {
  it('Equals 4', () => {
    const total = calculations.add(2, 2);
    total.should.equal(4);
  });
  it('Equals -4', () => {
    const total = calculations.add(-8, 4);
    total.should.equal(-4);
  });
});

describe('Subtraction of two numbers', () => {
  it('Equals 5', () => {
    const total = calculations.substract(7, 2);
    total.should.equal(5);
  });
  it('Equals 0', () => {
    const total = calculations.substract(0, 0);
    total.should.equal(0);
  });
});

describe('Division of two numbers', () => {
  it('Equals 5', () => {
    const total = calculations.divide(40, 8);
    total.should.equal(5);
  });
  it('Equals 0', () => {
    const total = calculations.divide(10, 0);
    total.should.not.equal(0);
  });
});

describe('Multiplication of two numbers', () => {
  it('Equals 0', () => {
    const total = calculations.multiply(17, 0);
    total.should.equal(0);
  });
  it('Equals 100', () => {
    const total = calculations.multiply(10, 10);
    total.should.equal(100);
  });
});

 

Create a file inside a new folder named “tests” for test cases and save the file with a .js extension. To run these test cases, run the command given below.

npm test


In the above code, the test cases check if the operations are true with the given possible inputs in the test file. The Chai.should() method tells Chai the way to execute. In other words. When we provide the should() method to the total, it tells what tests cases should evaluate and when we give should.not it tells Chai what should not be performed.

The tests cases are grouped with a description using the describe() method, and the tests are created and executed individually using it() method. The total.should method inside it() evaluates the value of test inputs and gives us the expected result. You can get the following input when you run them using the npm test in the IDE or editor.

Addition of two numbers
    ✓ Equals 4
    ✓ Equals -4
Subtraction of two numbers
    ✓ Equals 5
    ✓ Equals 0
Division of two numbers
    ✓ Equals 5
    ✓ Equals 0
Multiplication of two numbers
    ✓ Equals 0
    ✓ Equals 100

FAQs

  1. What is Chai?
    Chai is a JavaScript library that supports testing. It is a BDD(Behaviour-driven Development)/TDD(Test-driven Development) assertion library for node, which provides a clear syntax primarily written in English.
     
  2. Which is better Chai or Mocha?
    Both Mocha and Chai are used for testing that runs on node.js servers and browsers. In most cases, both are used together for better testing. So we suggest you use them together which will give you more powerful tests.
     
  3. How do I know what version of chai I have?
    You can check the version using Chai -v in node.js. In the browser, when you include the build into your test suite, the latest version will be available for hot-linking at the chai.js website.
     
  4. What does describe function do in Chai?
    The describe function in chai groups the test cases according to their functionality, provides a description to the group, and executes them.
     
  5. Are Mocha and chai the same?
    Mocha is a testing framework, whereas Chai is an assertion library used alongside frameworks like Mocha.

Key Takeaways

We have discussed Chai, its installation, and how to create test cases for the JavaScript code with basic functions. You can now use Chai to write the basic unit tests.

Hey Ninjas! We hope this blog helped you enhance your knowledge of Chai and testing. If you want to learn more, check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. Do upvote our blog to help the other ninjas grow.

Happy Coding!

Live masterclass