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
-
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.
-
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.
-
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.
-
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.
-
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!