How to setup Continuous Integration for your NodeJS project
Tags: node, ci, devops, travisci
Published: 2020-10-23 17:34:44

![Photo by Yancy Min on Unsplash](https://dev-to-uploads.s3.amazonaws.com/i/1d3yxt3x6kwzbxnejda2.jpeg)

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](https://dev-to-uploads.s3.amazonaws.com/i/te7wi6fghmriyq24wn0n.png) 

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:
- How to Unit Test with NodeJS?: https://medium.com/serverlessguru/how-to-unit-test-with-nodejs-76967019ba56
- Testing in Node.js Using Mocha and Chai: https://medium.com/better-programming/testing-in-node-js-using-mocha-and-chai-part-1-d5a9e91f4b06

# 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](https://travis-ci.org/) and login with your GitHub account.

![Screenshot 2020-10-22 at 2.53.25 PM](https://dev-to-uploads.s3.amazonaws.com/i/730jazajmke8013g86mf.png)
 
Click on the '+' on the left sidebar, next to the 'My Repositories' tab.

![1_YJVmrMKrFx1DMn8UafuLiA](https://dev-to-uploads.s3.amazonaws.com/i/7b15cwhsg23ry9cbfsfj.png)
 
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"
```
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.