Continuous Testing of Node.js APIs with Supertest and GitHub CI

Learn how to automate testing of Node.js APIs using Supertest, Chai, Mocha, and GitHub CI

Chandra Bhan , 01 April, 2023

Are you looking for ways to automate the testing of your Node.js APIs?
Look no further! Learn how to use Supertest and GitHub CI to streamline your testing process and ensure reliable API performance. Discover the power of continuous testing today!

Testing is an essential part of software development. It helps ensure that our code is working as expected and helps us catch bugs early in the development process. In this blog post, we'll learn how to create SuperTest test cases for a Node.js API, and then configure and execute them using the GitHub CI tool when pushing to the main branch.

Prerequisites

Before we start, make sure we have the following installed:

  • Node.js
  • SuperTest (npm install supertest --save) 
  • Chai (npm install chai --save)
  • Mocha (npm install mocha –save)

Setting up the project

To get started, create a new Node.js project with the following commands:

$ mkdir nodejs_api
$ cd nodejs_api
$ npm init -y

Next, install the required dependencies:

$ npm install express body-parser supertest chai mocha --save
$ npm install nodemon --save-dev

Create a new file called server.js and add the following code:

Create user entity in Node.js app with user/create and user/list APIs,

Find full code in the repository, NodeJS Supertest CI Repo.

Writing SuperTest test cases

First, let's create a user.js file in the nodejs_api/test directory.

We will create some test cases:

First for the GET /user/list endpoint and second for the POST /user/create endpoint, and third again for the list endpoint.

Here's an example of what the file might look like:

Here, we only start the Node.js API server and use the endpoint URLs to execute the tests.
Before executing these test cases we need to add test script in our package.json file

 "scripts": {
   "test": "mocha"
 },

Now to test our APIs on local system run following command in /nodejs_api/ directory

$ npm test test/user.js 

You will see the testing results for your APIs,

Configuring GitHub CI tool

Now that we have our SuperTest test cases, we need to configure GitHub CI tool to execute them when we push to the main branch. Here are the steps to follow:  

  • Create a new file called .github/workflows/nodejs.yml in the root directory of your project.  
  • Add the following code to the nodejs.yml file:

This YAML code defines a GitHub Actions workflow that executes when we push to the main branch. The workflow runs on an ubuntu-latest virtual machine, and has three steps: 

  • The Checkout repository step checks out the code from the repository. 
  • The Setup Node.js environment step sets up the Node.js environment with version 16.x. ( If get an error of node version deprecated, use latest node version to fix this ).
  • The Install dependencies step installs the project dependencies, including mocha, chai, and supertest. 
  • The Start Node.js API step starts the API in background because we use “&” in start server command.
  • The Run tests step runs the tests using the npm test command. 



Commit and push the nodejs.yml file to the .github/workflows directory. 

Now, when you push to the main branch, GitHub CI tool will execute the tests defined in the test/user.js file. If any test fails, the workflow will fail, and you'll receive a notification in your GitHub repository.  

You will find the testing results for your APIs in Github Actions,

Congratulations! You've successfully configured GitHub CI tool to execute your Node.js API tests when you push to the main branch.


If you'd like to check out the code for yourself, you can find my test repository on GitHub at NodeJS Supertest CI Repo. This repository contains all the code used in my testing, as well as instructions for how to run the tests yourself. Happy testing!

blog comments powered by Disqus