As developers, working in teams or individually, we have had times when we spent more time looking for a bug in our code than the time we spent writing the code. As much as this is a common thing among developers, this reduces our productivity. Of course, bugs cannot be removed from software development but with the help of Continuous Integration (CI), it can be minimized to the minimum.

CI is the practice of merging in small code changes frequently — rather than merging in a large change at the end of a development cycle. The goal is to build healthier software by developing and testing in smaller increments. https://docs.travis-ci.com/user/for-beginners/

CI encourages the practice of TEST DRIVEN DEVELOPMENT (TDD) which is a software development ideology that focuses on writing a test for a program before writing the functionality. Following the TDD procedure, an engineer must concentrate on the experiments before really coding anything. That means a developer is pondering the utilization of the product and the structure of UIs to accomplish that. Therefore, the developer is more keen on the interface than in the execution — and that prompts progressively usable programming.

In this writeup, we are going to look at a gentle introduction to Continuous Integration using Travis CI, a continuous integration tool. We are going to build a simple node-js app that will implement a GET HTTP method, test the app locally and then set up Travis CI to help with testing our codes whenever we push to GitHub and create a pull request.

Travis CI bolsters your improvement procedure via consequently building and testing code changes, giving prompt input on the achievement of the change. Travis CI can likewise mechanize different pieces of your improvement procedure by overseeing organizations and warnings.

So let's get started.

Setting Up Our NodeJs App.

Since we are using nodejs we will need to install some packages to help us build the app. first we will install NPM if we don’t have on our system. So in your system create the folder you and give it a name travis_class. Open the folder in the terminal. and the type in this command:

sudo apt install nodejs

This will install Nodejs and NPM on your system. To check if you have both on your system use these commands in your terminal

node -v

npm -v

Now we need to install some packages which we will be using, they include express, chai, mocha, to do that we write the following in our terminal

npm install mocha

npm install chai

npm install express

After this, we initialized an npm project in our folder using npm init in our terminal, after this, we should have a file named package.json. Add these lines of code in your scripts section of the package.json

"scripts": {
"start": "node app.js",
"test": "mocha"
}

After this, we create a test.js file and app.jsin the folder and type in the following code in the test.js file.

const expect = require('chai').expect
const server = require('./api');
const post = require('request')
const get = require('request')

describe('test', () => {
  const body = {};
  before((done) => {
    get('http://localhost:3000/user', (error, res) =>{
      body.response = res.body;
      done();
    })
  })
  it('should return a string', () => {
    expect(body.response).to.equal('ci with travis');
  });
});

Here we wrote a test and we have a specification that our first endpoint should return a response which should be the same thing as ‘ci with Travis’ also the second endpoint response should be equal to ‘Ayo is a good man’. run npm test in your terminal and u will realize that the test will fail. This happens because the specifications are not been met. The next step is to update our app.js file. copy or write these codes in the app.js file.

//this help us to use the express module which is part of the node modules
const express = require('express');

const app = express();

//here we declare a GET HTTP method which takes in the url and also a callback method which has 
//request and response as parameters. 

app.get('/user', (req, res) => {
  res.send('ci with travis');
});



const server = app.listen(3000, () => {
  console.log('App running on port 3000');
});

module.exports = server;

 

Here we tell each endpoint to listen to a get request and respond with ‘ci with Travis’ and ‘Ayo is a good man’ respectively. which should be what we are expecting as the response in the specifications we gave in our test.js.

To learn more about node and express you can check this out the introduction to node-js and express.

if you run the code now u should see that our code is passing and this is great. Next, let’s set up Travis CI. Go to the Travis CI homepage and sign-up for a free account. Create a .travis.yml file in your project folder it should look like this.

language: node_js
node_js:
- "stable"
cache:
directories:
- "node_modules"

The .travis.yml record demonstrated above is the thing that educates Travis on what to fabricate. the language alternative can be whatever language your application is running in and the “node_js”: “stable” demonstrates Travis should utilize a steady version of node. You can also cache your node_modules directory on Travis to avoid installing all dependencies every time a build is triggered but rather it updates packages that have newer versions.

 

Integrating Travis With GitHub

An ordinary work process with Travis and GitHub goes this way.

  • Create a GitHub repo and push the project folder to GitHub.

  • Add the repo to Travis Website
  • Create a new branch for each new features or chores
  • Push your branch to GitHub
  • Create a pull request and Travis CI will be triggered and let us know if the test pass or fail.
  • Add the Travis badge to a README.md file in your GitHub repo.

 

Conclusion

Travis CI helps in creating programs effectively. It guarantees you send clean code that pursues great practice and furthermore identifies if there are conceivable bugs or shortages in your code brought about by a change or refactor in your task.

 

Hi, am Ayooluwa, I build software and in-turn solve problems