Test-Driven Development (TDDF) For API
Tags: tdd, node, mocha, express
Published: 2020-04-30 00:20:00

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/doznen6q8l5k9emb0eiw.png)

### TDD
Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: requirements are turned into very specific test cases, then the code is improved so that the tests pass. This is opposed to software development that allows code to be added that is not proven to meet requirements.[Wkipedia](https://en.wikipedia.org/wiki/Test-driven_development)

### Why TDD
You can't imagine how TDD is very useful in software development industries. Most individuals programmers usually do not use this approach. However, the trusted and scalable product has to be tested for any expected cases. Hare are some of the benefits of TDD:
 * Test-driven development offers more than just simple validation of correctness, but can also drive the design of a program
 * It allows a programmer to focus on the task at hand as the first goal is to make the test pass
 * TDD can lead to more modularized, flexible, and extensible code.

### You can not talk about TDD without talking about CI/CD
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3xz6uw1o33nz3m14em9s.jpg)
CI is a coding philosophy and set of practices that drive development teams to implement small changes and check-in code to version control repositories frequently. Because most modern applications require developing code in different platforms and tools, the team needs a mechanism to integrate and validate its changes.[Read more](https://www.infoworld.com/article/3271126/what-is-cicd-continuous-integration-and-continuous-delivery-explained.html)

Without talking to much, let see this in practice. For this demo, I am going to create a simple contacts book API.
* Prerequisites:
   * Node js and Express
   * Creating API.
   * Git and Github

Now I will start
* Step 1: 
   * Create Repository on Github mine is "tdd-demo-ass" and clone it to your computer.
   * Create an account on Travis and connect your repository with Travis, after connecting it you have to copy the markdown and add it to your README file.[Read more here](https://docs.travis-ci.com/user/languages/javascript-with-nodejs/)
   * Create account on coveralls.io and connect your repo.[Coveralls](https://docs.coveralls.io/)

* Step 2:
Now that we have Travis, it will help us for CI and then we need where to deploy our app after a successful build, here we can use [Heroku](www.heroku.com).
To be able to perform in continuous delivery, we create pipelines which help to deploy our codes to the codebase automatically when all test pass.

Go to Heroku, create an account, and then create a new pipeline and connect it with your GitHub repo. From there, you can have a review app (where you can test every pull request before merging into the base branch), staging app which you can use whole testing and production app where the final app gets deployed when everything is ready.

* Step 3:

Now we have everything set up, we will start by writing our test cases, which by now will fail.
This is the structure of mocha and chai testing
<pre>
```
const chai = require('chai');
const { expect } = require('chai');
const { describe, it } = require('mocha');
const chaiHttp = require('chai-http');
const { app } = require('../app');

chai.should();
chai.use(chaiHttp);

describe('Route test', () => {
  it('Should return 404 not found route', (done) => {
    chai
      .request(app)
      .get('/api/notfound')
      .end((err, res) => {
        expect(res).to.have.status(404);
        done();
      });
  });

  it('Should return 200 on / request route', (done) => {
    chai
      .request(app)
      .get('/')
      .end((err, res) => {
        expect(res).to.have.status(200);
        done();
      });
  });
});
```
</pre>

Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cuyh9bbwpbcemekfxcs4.JPG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/djrlevnbp4e14thw5s6h.JPG)
From here when we create a pull request of our first branch to merge it into our base branch, the Travis tests fail and the code can not be deployed
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/guvuhybdg4y4yklh6uur.JPG)

*It may sound reckless to start with writing tests, but believe me this will save us from a lot of troubles and also keep our codes maintainable so that none can mess with it in the future.*

* Step 4:

No, it's time to write codes that pass the test. I will create contacts book API with basic CRUD operations and authentication. when I am done, this is what I get when I run the test again!![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/knwouaccsyam1taep22r.JPG)
And now when the PR is merged, my app automatically get deployed through the pipeline we created on Heroku. ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e5inr0sm75s1atzwe69n.JPG) and Travis and Coverage get 100% ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4zge1stfbvuz89f09b69.JPG)

### Conclusion
Now that we have an idea of what happens in CI/CD and TDD development, I believe that this is a required skill for every developer for creating effective and maintainable codes.

### Remember, every API needs documentation, you can get this demo API doc [Here](https://documenter.getpostman.com/view/10965967/SzmY8Lw7?version=latest).
### You can also get the codes for this demo on [GitHub](https://github.com/Francois-MUGOROZI/tdd-demo-ass).