Getting started with Unit Testing using Mocha and Chai
There are different type of software testing, they include, unit testing, integration testing, beta testing, smoke testing, alpha testing, etc.
Unit testing is a type of software testing where an individual software component or module or function is been tested.
In this tutorial, we’ll be using Mocha and Chai for unit testing.
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun.
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework. It is often used alongside Mocha.
To get started,
Create a node project, and install mocha and chai
npm install mocha chai
Add this script to your package.json 👇. This script runs all test files inside spec folder
"test": "mocha spec/*.js --exit"
Create two folders, app
to add our app logic and spec
to add our test files. Inside the folders, add calculations.js each, your node project directory should look like this
Open app/calculations.js
and add these lines of code
Open spec/calculations.js
and add these lines of code
To run our test cases,
npm test
chai.should()
extends each object with a should
property to start your chain
total.should.equal(4);
checks if total equals 4 using should.
describe(name, func)
used to group related test cases.
it(name, func)
used to run create individual tests.
Test API Endpoint using chai-http
Chai HTTP provides an interface for live integration testing via superagent. To do this, you must first construct a request to an application or url.
In order to test api endpoints, we need to create endpoints. We need to install express, nodmeon and then chai-http
npm install express nodemon chai-http
Create server.js inside app and spec folders respectively.
We are just creating three (3) endpoints, the index endpoint, date endpoint (returns the current date) and the wildcard endpoint (returns 404 when you hit an invalid endpoint).
Open app/server.js
and add these lines of code
We need to update our package.json scripts
"scripts": {"start": "node app/server.js","dev": "nodemon app/server.js","test": "mocha spec/*.js --exit"},
To start our server,
npm run dev
You should see this in your console
[nodemon] restarting due to changes...
[nodemon] starting `node app/server.js`
App is running on PORT 4000
You can go to your browser or postman to test the different endpoint, http://localhost:4000/
, http://localhost:4000/date
and http://localhost:4000/hello
Open spec/server.js
and add these lines of code
So, we now have 11 test cases in total. To run all the test cases,
npm test
You should see something like this in your console
App is running on PORT 4000Add two numbers
✓ Equals 4
✓ Equals -4Substract two numbers
✓ Equals 5
✓ Equals 0Divide two numbers
✓ Equals 5
✓ Equals 0Multiply two numbers
✓ Equals 0
✓ Equals 100GET /
✓ it should return API is live...GET /date
✓ it should return the current dataPUT /date
✓ it should 404 error because the endpoint is invalid11 passing (52ms)
You can find all the codes on GitHub, https://github.com/Rexben001/mocha-chai-example