Continuous Integration and Unit Testing with Buddy
Tags: testing, node, devops, buddy
Published: 2020-12-29 03:18:14

---
title: Continuous Integration and Unit Testing with Buddy
published: true
date: 2020-08-15 01:07:54 UTC
tags: Testing,Nodejs,Devops,Buddy
canonical_url: https://lo-victoria.com/continuous-integration-and-unit-testing-with-buddy
---

Hello everyone! Welcome back to the [The DevOps Series with Buddy](https://hashnode.com/series/the-devops-series-with-buddy-ckbut97o9002nwus16xjmgt80), a series dedicated for beginners to get started with DevOps concepts and integrating them into their projects. This is the 4th article of the series.

If you are new to this series, please check out the articles in the following order:
1. [Introduction to DevOps](https://lo-victoria.com/introduction-to-devops-with-buddy-ckc86jas80080ups179d51nbg)
2. [Managing Team and Roles in DevOps](https://lo-victoria.com/managing-team-and-roles-with-buddy-ckce2u9yx002pbrs19sbv61tt)
3. [Automated Notification and Testing](https://lo-victoria.com/automated-notification-and-testing-with-buddy-ckcw2779r01fx7is10taf6r3a)

For this article, we will continue the topic of automated testing in DevOps by introducing more testing actions you can do with [Buddy CI/CD](https://buddy.works?utm_source=devops_with_buddy&utm_medium=referral&utm_campaign=content_victoria_lo).  In particular, we will be diving into **unit testing** and **integration testing**.

## Unit Testing
The basic principle of unit testing is to be able to **test the smallest unit of code (i.e. function) to make sure it is fit for use**. Unit testing isolates each function as independent code blocks, allowing developers to test whether an individual function can perform what it is intended to do without affecting the rest of the code. This allows **faster bug detection and more efficient error fixes**.

![unitesting.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1595901267939/ljCDfvZlq.png)

### Implement Unit Testing with Buddy
With Buddy, we can implement unit tests easily into our apps using these test frameworks and libraries. For this tutorial, I will demonstrate how to integrate automated [Mocha](https://mochajs.org/) tests into a unit converter API.

In JavaScript, Mocha is a commonly used framework to build tests. We will use it with [Chai](https://www.chaijs.com/), a test assertion library designed to provide descriptive syntax for tests. For details on how to write unit tests with Mocha/Chai, please read this [article](https://lo-victoria.com/introduction-to-unit-testing-with-mocha-and-chai-ckbqjbqqm00s3lgs14w34pdgq).

## Step 1: Add a new pipeline
If you haven't created a Buddy account, go to [buddy.works](https://buddy.works?utm_source=devops_with_buddy&utm_medium=referral&utm_campaign=content_victoria_lo) to set up an account and read the [first article](https://lo-victoria.com/introduction-to-devops-with-buddy-ckc86jas80080ups179d51nbg) to learn how to get started.

![Capture.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1596551631162/WlILUNsXH.png)

Then, we can import a new project to our Buddy workspace and click on '**Add a new Pipeline**'.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1593103213756/lMWsqeeSu.png)

### Automating the process
To run our unit tests every time new changes are pushed, we can do so by setting the Trigger Mode to **On push** under the Settings tab of our pipeline. This allows for faster error detection by saving us time from manually running unit tests every time we add new functions to our project.

![pipelineOnpush.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1596562265087/x5YVIPdc-.png)

### Step 2: Add a Node.js Action
Let's add a Node action into the pipeline. Click on '**Actions**' and as seen in my image below, Buddy detects that my API is a Node.js app and immediately recommends that action. Alternatively, I can search for 'Node' and the action will show up too. 

![addnode.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1596562457404/LVzp8Cc65.png)

Click on the Node action and let's start configuring it.

### Step 3: Configure Action
Under the **'Run'** tab of the Node action, we will see 2 lines by default:
```
npm install
npm test
```
We can write additional npm commands to run tests in our project. In my case, I added a **cd** (change directory) command because my project is enclosed inside the folder named "Unit Converter". Then, I replaced 'npm test' to **npm run unit-test** because I want to run only my unit tests for this action.

![unit-test action.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1595993232505/M8Zk-vAht.png)

Note that the **npm run unit-test** command will run our unit-test script. So make sure that the "scripts" key inside the package.json of our project does contain the unit-test script which has the command to run Mocha like so:
```
"scripts": {
    "unit-test": "mocha ./tests/unit-tests.js"
  },
```
### Useful Tip: Caches
Instead of adding a ```cd "Unit Converter"``` command in our Node action, Buddy allows us to set up a working directory by entering it in the **'Cache'** tab.

![cache.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1596589407463/zZrFZAJky.png)

> For more information, please visit Buddy's documentation on [cache](https://buddy.works/docs/pipelines/cache).

### Conditional Notifications
Additionally, we can add a Notification action to our pipeline so that the team can be notified and view the execution logs if there is an error in the pipeline.

![conditionEmail.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1596561813734/EdYm21nKC.png)

> Read [Automated Notification and Testing with Buddy](https://lo-victoria.com/automated-notification-and-testing-with-buddy-ckcw2779r01fx7is10taf6r3a) article for more details on how to add notifications.

Here's the full view of our pipeline:

![fullPipeLine.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1596562536923/rXPL18KaA.png)

### Step 4: Run the pipeline
We can run the pipeline and our test action. Let's see the results:

![unit test pass.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1595993318858/sK5f-fR_y.png)

Great! We have successfully passed all our unit tests! Our functions are working properly as individual units. Any functions that fail the tests would show an error in our pipeline and send the team the email with the error logs.

## Integration Testing
A higher level of testing that is often performed after unit testing is known as Integration Testing. Instead of testing individual functions as independent units, integration testing combines these units and **tests them as a group**. This type of testing allows developers to check whether individual app components can work together and produce the output as intended.

![3372OS_09_02.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1595994127163/WWGpJRFoF.png)

In a JavaScript setting, integration testing typically involves checking the external services and server-side end of the app. For integration tests, we can use Mocha and Chai again to write them. 

> For details on how to write integration tests with [Mocha](https://mochajs.org/) and [Chai](https://www.chaijs.com/), please read this [article](https://lo-victoria.com/introduction-to-unit-testing-with-mocha-and-chai-ckbqjbqqm00s3lgs14w34pdgq).

### Step 1: Add a new pipeline
The reason why we add a new pipeline is to implement the tests separately. Integration testing is often done recurrently, every 12 or 24 hours. So we automate this new pipeline to run every 24 hours instead of 'On Push'.

![newPipeline.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1596556944990/tNHsb_yaX.png)

### Step 2: Add and Configure Action
Just like what we did for our unit testing, add a Node action and head to the **'Run'** tab of our action. Our npm commands should be:
```
cd "Unit Converter"
npm install
npm run integration-test
```
Here's what our integration testing Node action should look like:

![int test action.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1595993790401/rbpfYoZGQ.png)

This time, we are running our integration-test script instead of the unit-test. Again, make sure the script exists in the package.json with the Mocha command like:
```
"scripts": {
    "unit-test": "mocha ./tests/unit-tests.js",
    "integration-test": "mocha ./tests/integration-tests.js
  },
```
Here's the full view of our integration test pipeline:

![fullIntTEst.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1596562730724/E8HnUQLa7.png)

### Step 3: Run Pipeline
Now, we are ready to run the integration test pipeline.

![intTEst Success.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1596557331419/yJzQkwcDj.png)

Wonderful! Our project successfully passed both tests in each pipeline, which are fully automated.

As mentioned earlier in the previous article of this series, automated testing reduces the time and effort of having to manually run tests. It also ensures a thoroughly efficient and smooth workflow by allowing early error detection in the project. 

## Next Steps
Congratulations! You now know more testing actions that can be easily implemented into [Buddy CI/CD](https://buddy.works?utm_source=devops_with_buddy&utm_medium=referral&utm_campaign=content_victoria_lo) for automation! Automated testing is a crucial part of the DevOps principle of continuous delivery and integration. I hope this article has been helpful in introducing unit and integration testing, and how they can help increase your projects functionality and reliability for production.

For more information on Buddy CI/CD and its wide variety of [actions](https://buddy.works/actions?utm_source=devops_with_buddy_actions&utm_medium=referral&utm_campaign=content_victoria_lo), visit buddy.works or read its documentation [here](https://buddy.works/docs?utm_source=devops_with_buddy_docs&utm_medium=referral&utm_campaign=content_victoria_lo). In the final article of this series, I will be demonstrating how to implement continuous deployment into your pipeline for your apps. Thank you for reading, cheers!