Testing a REST API in Node JS with Express using Mocha and Chai 😎
Tags: javascript, testing, webdev
Published: 2020-12-06 21:40:06

# Table Of Contents
* [What is Unit testing](#chapter-1)
* [Why using Unit testing? ](#chapter-2)
* [Who use Unit testing? ](#chapter-3)        
* [Conclusion](#chapter-4)

# What is Unit testing ?! <a name="chapter-1"></a>
 Is a software testing method by which individual **units of source code** and typically automated tests written and run by software developers to ensure that a section of an application  (known as the "unit") meets its design and behaves as intended.
___

# Why using Unit testing <a name="chapter-2"></a>
Testing is one of those things that people either love or hate. **Usually testing is something that is hated**

![haa](https://media.giphy.com/media/KGSxFwJJHQPsKzzFba/giphy.gif)

+ Until you work on a project with good tests and you **realize how amazing** they are.
I will talk about the code you need in order to write tests in JavaScript using **chai and mocha**, as well as show you some pitfalls of testing.

+ At the end of the article I will breakdown the importance of testing and some **best practices** you can adhere to in order to make your tests amazing.
___

# Who use Unit testing ?! <a name="chapter-3"></a>
To get started in this trip, I have simple REST APIs  already created (Get, Post, etc.)

![Git API](https://dev-to-uploads.s3.amazonaws.com/i/wcpy6dq0x49jy6rshawn.PNG)

+ And these are very simple server-side Nodejs with Express APIs [GitHub repository link](https://github.com/mohamedlotfe/unit-testing-api-nodejs/tree/master)
in our case we have a route get all tasks which response with a small array of tasks.

+ As we mentioned at the beginning of this article we gonna talking about testing and how to test on JavaScript and an the easiest way to that in my opinion is to use both library(chai and mocha)

**Chai** is an assertion library for NodeJS and browser with three styles [ should, Expect,  Assert] 
--
**Mocha** is a testing framework for JavaScript, each is an incredibly and well-built testing library for JavaScript 
--
___


![Lets Start](https://media.giphy.com/media/XyaQAnihoZBU3GmFPl/giphy.gif)

### 1.In order to **start** this all we need to do is to **install** two libraries with
####`run npm i mocha chai --save--dev`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6i237vjxtmtfsy8u8o8x.PNG)


### 2.Now that is done downloading we come here where **we have test script** and we can change this by typing in `"test": "mocha"`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ldmy6dasv9418zr9ilk3.PNG)


###3.So to get started creating first test all need to do is **create folder called test** and add **a new file task.js** give it the exact same name as file you want to test in our case we will test 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/sifj5x5n1mzeqtse19al.PNG)


###4.Now inside this file all need to do to **import routes file (task.js)** including all APIs and in order to write test we need to **import chai then adding Assertion** 

'![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/iz497vd3gvofkfymohbf.PNG)


###5.Now we are able to call our RESTful APIs and in order to use **mocha** describe our test let's call it `tasks API`, then we define an arrow function so first task to describe Get API let's call it `Get-All-Tasks` then user **It** to describe what our API do `it should Get all the tasks` 

###6.Here we use **chai** with `chai.request(server)` and **test** our Get API `.get("/api/tasks")` the we **expect** using `.end((err, response)` which will receive error and response so we **expect** a successful results so response should have **status 200** and **body should be an array with 3 tasks** 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s9e41twrq2hgm1lsw26h.PNG)

###7.finally call `Done()`, and **run** using `npm test` and the test run successfully

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e5luxocqvaet6hhnzqiu.PNG)

![celebrate](https://media.giphy.com/media/26tPplGWjN0xLybiU/giphy.gif)

### If you need to request wrong route to show API response we make small changes

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x411l5dlkdi8f1znqi3b.PNG)

### Test the GET (by Id) route the same steps but we will add task id and in response we will check for returned properties  

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/z9o2wk42d0t2tuyj2hos.PNG)
___

#Conclusion <a name="chapter-4"></a>

###**Testing** one of the most important skills you can known as a developer, It's something a lot of people don't teach or focus on but if you known testing, It's gonna set you apart from every other developers that doesn't known testing and give you extra leg up when you **applying for jobs**