Automating Tests for Your Node.js Application
Tags: node, javascript, devops, cicd
Published: 2023-02-08 06:12:30

Are you a Node.js developer looking to streamline your testing process? Automated testing can be a great way to ensure that your application is free of bugs and is running as expected. This blog post will provide an overview of automated testing for Node.js applications and tips for getting started.

## Automated Testing
Automated testing is a key component of software development, which helps ensure the product's quality before it is released. Automated testing is the process of checking the accuracy and correctness of a software application by running tests against it. Unlike manual testing, automated tests are executed repeatedly and quickly to ensure that the code works as expected. Automated testing can be conducted for unit tests, integration tests, system tests, performance tests and more. By using automated testing tools such as mocha, chai, etc. (in Node.js), developers can easily create automated tests to check the functionality of their applications. Automated tests are also useful in ensuring that the code is bug-free and working efficiently in production.

## Understanding Unit Tests in Node.js
![nodejs unittests](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4tpi0748l7o3n9w2fk3r.png) Unit testing is a software testing method where individual units or components of a software application are tested in isolation to ensure that they work as intended. In Node.js applications, unit testing is an important aspect of the development process as it helps to identify and fix bugs early on, making the application more stable and easier to maintain. Unit testing also facilitates continuous integration and deployment, allowing developers to confidently make changes to the codebase. Popular libraries for unit testing in Node.js include Jest, Mocha, and Chai. 

These testing frameworks provide various tools for writing and running tests, making it easier for developers to write and maintain tests for their applications. It is recommended to write unit tests for all major functionalities of the application to ensure that the application works as expected and to prevent regressions in future updates. Additionally, tests can be run in parallel using Node.js for faster results. Unit testing is a great way to ensure that your code is working as expected before deploying it to production, and its advantages include more reliable code and fewer bugs.

## Advantages of Automated Testing
**Automated testing in Node.js is important for several reasons**:
- **Increases code quality**: Automated testing helps to catch bugs and errors early on in the development process, improving the overall quality of the code.
- **Saves time**: Automated tests can be run repeatedly and quickly, saving time and effort compared to manual testing.

- **Facilitates continuous integration and deployment**: Automated tests help ensure that changes to the codebase do not break existing functionality, making it easier to integrate changes and deploy new releases.

- **Improves collaboration**: Automated tests can be shared among team members, helping to ensure that everyone is working with the same understanding of the code.

- **Increases confidence**: Automated tests provide a safety net that ensures that changes to the codebase do not break existing functionality, giving developers the confidence to make changes with ease.

In conclusion, automated testing is essential for ensuring the quality and stability of Node.js applications and helps to save time and effort in the development process.

## The Need for a CI Server
![need for ci](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9okovctho3mjh0xswz8.png) We need a Continuous integration (CI) server to run automated tests repeatedly. Continuous integration is a software development practice in which developers regularly integrate code into a shared repository, and automated builds and tests are run on the resulting changes. The goal of CI is to catch integration and compatibility issues as early as possible in the development process and to provide rapid feedback to developers. This helps ensure that the code remains stable and deployable at all times. 

Building our own CI server is not at all a good option because of the complexities involved. Hence we can always utilize the already available CI/CD tools/platforms in the market, such as Harness, CircleCI, GitHub Actions, etc. 

**To run automated tests through continuous integration, the following steps are typically followed**:
- Set up a version control system (such as Git) to manage code changes and store the repository.
- Configure a CI tool that integrates with the version control system and runs tests automatically whenever changes are pushed to the repository.
- Write automated tests for the code and store them in the repository along with the code. These tests should cover various aspects of the code, such as its functionality, performance, and security.
- Set up the CI tool to run the tests every time changes are pushed to the repository. This can be done by defining a pipeline or build configuration that specifies the steps to build, test, and deploy the code.
- When a change is pushed to the repository, the CI tool automatically builds the code, runs the tests, and reports the results. If the tests fail, the CI tool can alert the developers and prevent the code from being deployed until the issues are fixed.
- Continuously monitor the results of the automated tests and use the feedback to improve the code and the CI process.
Today, we will show you how to use Harness to run automated tests. 

## Prerequisites
- Free [Harness cloud account](https://app.harness.io/auth/#/signup/?module=ci&utm_source=internal&utm_medium=social&utm_campaign=devadvocacy&utm_content=pavan_notes_article&utm_term=get-started) to set up continuous integration 
- [Node.js](https://nodejs.org/en/download/) installed 
- GitHub account -  We will refer to our [sample notes application repository](https://github.com/pavanbelagatti/notes-app-cicd). You can fork it and use it in this tutorial.

## Tutorial
####Step 1: Clone & run the sample application locally

Clone the [sample notes application](https://github.com/pavanbelagatti/notes-app-cicd) using the below command 

```
git clone https://github.com/pavanbelagatti/notes-app-cicd.git
```
 
Then, enter the code folder with the below command

```
cd notes-app-cicd
```
Install the dependencies required for the project with the below command

```
npm install
```
Run the application using the below command

```
node app.js
```
You should see your application running on your local terminal http://localhost:3000/
![app running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1so748j469vecg0cwmon.png)

####Step 2: Run the tests locally

We have added simple mocha test cases to the project.
Go to the root of the sample application and run the following command

```
npm test 
```
You should see the test cases passing.
![testing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5nxpcjlsdagu9h321qq6.png)

####Step 3: Set up a continuous integration (CI) tool to automate the tests

Harness is a sophisticated software delivery platform with a CI module that helps developers set up continuous integration in no time. 

Sign up for the [free Harness cloud account](https://www.harness.io/products/continuous-integration?utm_source=internal&utm_medium=social&utm_campaign=devadvocacy&utm_content=pavan_notes_article&utm_term=get-started). Once you sign up and verify your account, you will be presented with a welcome message and project creation set up. Proceed to create a project.
![welcome message](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ka5v5f8nsf45pq5qzr4r.png)

Add the name to the project, save and continue.
![about project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/le3tesjggkqvnl0329uf.png)

Select the ‘Continuous Integration’ module and start your free plan.
![harness continuous integration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/as4bbrgdrtr33q6j977l.png)

You can start creating your first pipeline. 
![Harness pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7k0mcu4a240oyg9kz4is.png)

Click on ‘Get Started’ and connect your SCM tool/platform where the application code is present.
![code repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4bfveqtgat0nm05zwdti.png)

Once your SCM integration connection is successful, pick your repository and begin your pipeline configuration. 
![repo list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/unxydfprh5mia7g9at1c.png)

Since we are working with a Node.js application, let’s select Node.js and proceed to create the pipeline. 
![configure pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ofuiaquhfxc4vhsopgo2.png)

Once you click on ‘Create Pipeline’, you will see the pipeline studio as shown below with default settings. 
![pipeline studio](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rd5gjs67jusilpehdjmv.png)

You can click on ‘Build Node App’ to see the pipeline configuration. You should see the following page.
![build node app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9hlzznwfmbaxz8rk16r8.png)

If you click on the ‘Build Node App’ under the ‘Execution’ tab, you should see the ‘Run’ step configured for you by default.
![configure run step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dedkmgf0uxbjh4dy4mtb.png)

You can see node and npm commands in the ‘Command’ tab.

You can keep the commands mentioned as it is or replace and add only the important commands. 

We will just keep `npm install` and `npm test`

Apply changes, save the configuration and run the pipeline. You should see the tests passing.
![run pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/567yf1xpcj8khlojjrwz.png)

![all steps passing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8vcatrxmq16jf3kqrod.png)

You can add even more tests to your pipeline using the 'Run' step. 
For example, let's also add a test to find vulnerabilities in the Node.js modules. We can do it by adding the auditjs command.
Add one more 'Run' step and configure the step as below,

![audit modules](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qav1mv7d20kbfo65asmi.png)

Below are the commands used in the 'command' tab,

```
npm install -g auditjs
auditjs iq --version
```
####Step 4: Automate your tests 
Add a trigger to run the tests repeatedly whenever someone pushes the code to your repository.

You can click the ‘Triggers’ tab in the pipeline studio and add your desired trigger.
![add trigger](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bllaalh2cdbzb44mlrx3.png)

![add new trigger](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0jnvznymjkzqiokixcjy.png)

Click on ‘Add New Trigger’ and select GitHub from the Triggers list.
![triggers list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bsy3u50uaafo05jfg0hm.png)

Add the required details and continue. As you can see, we are selecting ‘Push’ as our event. So whenever any push happens to our repository, the pipeline should trigger and run the tests.
![trigger setup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ym9gaq0w57p23tr6050p.png)

You can customize and experiment with your trigger. Finally, you can see your configured trigger in the ‘Triggers’ tab.
![listing trigger](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/71gprp4z6591nca9utdq.png)

Now, whenever anybody pushes any code changes to your main repository, the pipeline automatically gets triggered and runs the tests.

Let’s go and add some text to your README.MD file, and as soon as you add your text, the build gets triggered and runs the tests specified. You can see the build getting triggered.
![build triggered](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1jfkb0p1s9sqts1vcowe.png)

Finally, you can see if the tests are passing.

**Congratulations!** We successfully configured a CI pipeline for our sample application.