DEV Community

Cover image for Automating Tests for Your Node.js Application
Pavan Belagatti
Pavan Belagatti

Posted on • Updated on

Automating Tests for Your Node.js Application

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

Tutorial

Step 1: Clone & run the sample application locally

Clone the sample notes application using the below command

git clone https://github.com/pavanbelagatti/notes-app-cicd.git
Enter fullscreen mode Exit fullscreen mode

Then, enter the code folder with the below command

cd notes-app-cicd
Enter fullscreen mode Exit fullscreen mode

Install the dependencies required for the project with the below command

npm install
Enter fullscreen mode Exit fullscreen mode

Run the application using the below command

node app.js
Enter fullscreen mode Exit fullscreen mode

You should see your application running on your local terminal http://localhost:3000/
app running

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 
Enter fullscreen mode Exit fullscreen mode

You should see the test cases passing.
testing

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

Add the name to the project, save and continue.
about project

Select the ‘Continuous Integration’ module and start your free plan.
harness continuous integration

You can start creating your first pipeline.
Harness pipeline

Click on ‘Get Started’ and connect your SCM tool/platform where the application code is present.
code repo

Once your SCM integration connection is successful, pick your repository and begin your pipeline configuration.
repo list

Since we are working with a Node.js application, let’s select Node.js and proceed to create the pipeline.
configure pipeline

Once you click on ‘Create Pipeline’, you will see the pipeline studio as shown below with default settings.
pipeline studio

You can click on ‘Build Node App’ to see the pipeline configuration. You should see the following page.
build node app

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

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

all steps passing

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

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

npm install -g auditjs
auditjs iq --version
Enter fullscreen mode Exit fullscreen mode

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

add new trigger

Click on ‘Add New Trigger’ and select GitHub from the Triggers list.
triggers list

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

You can customize and experiment with your trigger. Finally, you can see your configured trigger in the ‘Triggers’ tab.
listing trigger

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

Finally, you can see if the tests are passing.

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

Latest comments (3)

Collapse
 
ucrynet profile image
CryNet

Wow, interesting, I dunno about Github Actions.

Collapse
 
rayjlim profile image
Ray

This sounds like Github Actions

Collapse
 
pavanbelagatti profile image
Pavan Belagatti

This is Harness. A complete CI/CD tool for developers