Configuring a unit test to check for errors on the Node.js application

- By Abhijit Kini

BACK TO BLOG HOME

In the previous tutorial, I ran a basic Continuous Integration (CI) on a Node.js application. As the next logical step, I'll configure a simple unit test to check for errors in my code. As per the guidelines of CI, my expectation is that for every code commit or pull request, this unit test runs automatically and immediately checks for errors. In this tutorial I will set up a unit test, configure the shippable.yml, and verify the unit test to complete a successful CI.

Before you begin:

Ensure you have -

  • Installed Node.js on your Mac/PC
  • A GitHub or BitBucket account
  • Completed the previous tutorial of running a basic CI on a Node.js app
  • Signed into Shippable, authorized access to public repositories hosted on your source control provider

I'll continue to use a simple Node.js app, available to download from GitHub.

Step 1: Configure the unit test

I have configured a simple unit test in the file test.js, which checks the text published in index.js. See the highlighted text below:

Configuring a unit test for Continuous integration

Step 2: Configure the shippable.yml file

Next, I'll update the shippable.yml file to run the npm test in the the build section:

Updating Shippable yml file with unit test

Step 3: Verify unit test

In order to to verify the unit test and show how Shippable notifies you when the unit test fails, I have deliberately introduced an error in the 'index.js' as shown below.

Introducing an error to check the unit test

Now the npm test will fail and notify you the action you need to take to resolve it. Let's review error shows up on Shippable. Since I make a change to the file 'index.js' locally, I'll push this to my 'nodejs' GitHub repo by running the following commands from the local 'nodejs' directory using Terminal.

  • git add .
  • git commit -m "added error deliberately to check unit test"
  • git push origin master

This code commit triggers a build on Shippable and the error is shown below:

Unit test displays error as part of Continuous Integration process

Step 4: Complete a successful CI build

Seeing the error, I know what to fix. I correct the error on my local 'index.js' file using my text editor by replacing "2" with "to". Then I push this code change from my local 'nodejs' directory to my 'nodejs' GitHub repository by running the commands in my Terminal:

  • git add .
  • git commit -m "fixed error - replaced 2 with to"
  • git push origin master

This action automatically triggers build and in the picture below, you'll see the unit test completed successfully and we get a green build - a successful CI run.

Successfully configure a unit test for Continuous Integration

Now I have successfully configured a unit test for the Node.js app and have got a successful green build on Shippable. In the next tutorial, I'll run CI for a node.js app hosted on a private repository.


  Try Shippable

Topics: continuous integration (CI), nodejs, CI/CD, npm, unit test