DEV Community

Kevin
Kevin

Posted on

How to setup Continuous Integration for your NodeJS project

Photo by Yancy Min on Unsplash

Continuous Integration (CI) is a software development practice of merging your code with the main branch frequently. This aims to help developers catch bugs early, and also to prevent a developer's worst nightmare, known as 'merge hell'. Merge hell happens when you are about to merge with the main branch, but you are met with a long list of merge conflicts. Imagine after working for on a feature for months and you're finally done, but now you have to untangle this mess of merge conflicts.

1_LjDom9HWO5bSvL0Ub_xvpg

CI aims to help solve this issue by having developers merge their code with the main branch once or multiple times a day. The principle is: if it hurts, do it often, then it will not hurt so much. By merging code multiple times a day, this means that even if there is a merge conflict, it would be a relatively small one, since the changes is only up till the previous day at most. This makes the merge conflict easy to resolve, and developers can easily get on with their day. It also allows developers to catch issues and solve them early.

With developers merging more frequently, sometimes the code being merged could be half written code, or code that have not been tested extensively. That is why another important aspect of CI is the use of automated testing. Automated testing ensures that the code being merged does not contain any bugs or breaking changes that would affect the work of other developers. This ensures that problems are resolved before they are merged into the main branch.

In this tutorial, I will be sharing how you can setup CI for your project by implementing automated testing into your NodeJS project. For more information about writing test cases for NodeJS, I recommend reading the following articles:

Implementing automated testing in GitHub for your NodeJS project

Pre-requisites:

  • Your project should be on GitHub
  • Your project should be able to run tests locally using the 'npm test' command

Go to Travis CI and login with your GitHub account.

Screenshot 2020-10-22 at 2.53.25 PM

Click on the '+' on the left sidebar, next to the 'My Repositories' tab.

1_YJVmrMKrFx1DMn8UafuLiA

Search for the repository you want to use, and click on the toggle button on the right.

Travis is now enabled for your project! Whenever a new push is made, Travis will try to build it. However, Travis doesn't know what exactly to do yet, and we need to tell it what to do by using the '.travis.yml' file.

In the root directory of your project, create a new .travis.yml file and enter this into the file:

language: node_js
node_js:
 - "stable"
Enter fullscreen mode Exit fullscreen mode

Now simply add this .travis.yml file into your github repository, and make a new PR and viola! You should see a yellow dot next to your PR, indicating that a Travis build is running, and a green dot if it passes all test cases, or a red cross if it fails your test cases.

Latest comments (0)