# Introduction to Unit Testing in Node JS

![](https://cdn-images-1.medium.com/max/2400/1*7SsVit8zDEgpmEXTyU5ISA.jpeg align="center")

Photo by [Cameron Venti](https://unsplash.com/@ventiviews?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/tech?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)

This blog explains with an example, how to do unit testing in node js. To know why we do unit testing or testing in general, visit this blog, [Unit testing — why? by Corneliu Dascalu.](https://medium.com/@corneliu/unit-testing-why-3490d08e89f2)

First I will give a brief introduction to unit testing then we will jump to the coding part.

> **Unit testing** is testing the individual components(units) of the software. It is carried out by the developer during the development process. It tests the functioning of the individual components rather than the whole codebase.

Now I will start with the code. Initialise the node project with package.json file using the following command.

```plaintext
npm init --yes
```

The following is our index.js. We are creating the class employee which is being exported.

Now, we will write unit tests for the code in index.js.

Install [mocha](https://mochajs.org/) and [chai](https://www.chaijs.com/) as development dependencies. These are npm libraries used for testing. Mocha is a Javascript test framework and chai is an assertion library.

```plaintext
npm install mocha chai --save-dev
```

Create the index.test.js file. Change the test property inside the scripts section in package.json to “mocha index.test.js”

```plaintext
"test": "mocha index.test.js"
```

Now, we will write the test cases. We will use **“describe”** to group the test cases related to a unit and **“it”** is used to create a test case. We can declare *describe blocks* inside another *describe block*(i.e nested *describe blocks* are possible). There can be multiple *it blocks* in a single *describe block*. We use assertion methods provided by chai library to test the functioning of units for various test cases and values.

[Documentation for assertion methods in chai library.](https://www.chaijs.com/api/assert/)

Here is the code for index.test.js. I have created some sample test cases. You will need to create test cases according to your need. The aim should be to test both the edge conditions and normal conditions where the code might break and change the code accordingly. Here I am not checking for all the possible test cases. I have just demonstrated for some cases.

To run the test script use the following command.

```plaintext
npm test
```

Here are the results.

![](https://cdn-images-1.medium.com/max/1600/1*3VGHq7WAd_akskvs9uFCgA.png align="center")

As we can see from the results 4 test cases passed and 1 failed. Now the developer can change his code accordingly.

So, this was basic unit testing. Here the code is very small and we can check manually how much of the code is being tested (i.e **code coverage**). But for large codebases, it will be very difficult. We have an npm library called [istanbul](https://www.npmjs.com/package/istanbul) that calculates the code coverage for us.

Install istabul as a development dependency using the following command

```plaintext
npm install istanbul --save-dev
```

Now add the following line under scripts in package.json

```plaintext
"coverage": "istanbul cover _mocha index.test.js -x *.test.js",
"showcoverage": "xdg-open coverage/lcov-report/index.html"
```

package.json finally looks like this:-

Use the following command to show the code coverage in the console.

```plaintext
npm run coverage
```

Use the following command to display the results in the browser in a more interactive way.

```plaintext
npm run showcoverage
```

---

This blog is was a very basic introduction to unit testing. There is a lot more to it than this but I think these tools and methods will be really helpful to you.

**Resources and References**

* [Github repo of this projec](https://github.com/subhrapaladhi/unit-testing-basics)t
    
* [Intro To JavaScript Unit Testing With Mocha JS & Chai](https://www.youtube.com/watch?v=MLTRHc5dk6s)
    
* [Unit testing — why?](https://medium.com/@corneliu/unit-testing-why-3490d08e89f2)
    
* [Chai assertion library](https://www.chaijs.com/api/assert/)
