Node.js Unit Testing Automation With Drone CI Using Mocha Framework
Learn how to do continuous integration (CI) using Drone CI, and taking a simple Node.js application and Mocha as the unit testing framework.
There’s no way around it: unit testing is an essential piece of any software development lifecycle. In today’s modern cloud-native space, automated testing is in high demand with many platforms and tools available. This testing automation can be done through a continuous integration (CI) process. CI is an essential method in the overall DevOps process. CI helps developers test and build their code, so they can make sure that nothing is breaking and the code can be confidently integrated into the main branch with a simple approval by an approver. The CI process can be seamlessly automated with Drone CI to test, build, and give confidence to your developers. CI lets developers save time on manual testing and focus on building the rich features that their customers need. Today, we’ll demonstrate how to do CI using Drone CI, and taking a simple Node.js application and Mocha as our unit testing framework.
Step 1: Create a folder for all of your application files:
Step 2: Write a simple Node.js application.
Initialize your project by running the following command:
This creates a package.json file with all of the dependencies that you downloaded for your project.
Step 3: Install express, the Node.js web application framework:
Step 4: Next, we must create a file named app.js with the following code:
Step 5: Run the app:
Mocha Unit Testing Framework
Step 6: Let’s add the test folder to our app directory where all of our tests will reside.
Create a folder named test, and under that, create a file test.js.
You can install Mocha and Chai as part of the Node.js test framework:
Add Mocha as a dependency in the package.json file for the particular project.
Step 7: Add the following basic Mocha string test spec to your test.js file:
Step 8: You can go back and check your package.json file. It should have the Mocha part included, and it should look like this:
Step 9: Now, run the test.
Go to your app directory and run the following command:
My output is as follows:
It’s failing, as there seems to be an error when Mocha was run. You can see 1 failing.
The source code of the example project is shared here.
CI for Node.js application
Until this point, we locally tested the Node.js application, and this can be automated through a CI server. Drone CI is our preferred CI platform that we already know how to run locally. It’s open source and easy to install in just minutes.
If you don’t know how to set up a Drone server on your local machine, then here is my colleague’s article that will help you have your own Drone CI up and running.
Every time a developer pushes the code to the main branch of the application, using a CI server like Drone helps companies find bugs and rectify the errors and mistakes before they reach customers.
Let’s continue with the next steps.
Step 10: Push your application to GitHub.
We should make sure that no file or repository is missing. In our GitHub repository, we still need to add two files:
- .drone.yml as part of the Drone configuration files. Pipelines are configured by placing a .drone.yml file in the root of your Git repository.
- Dockerfile to build and publish a Docker image as a step in our build pipeline. Therefore, add a simple Dockerfile, as it contains instructions to build your container.
ENV NODE_ENV development
# Add a work directory
# Cache and Install dependencies
COPY package.json .
RUN npm install
# Copy app files
COPY . .
# Expose port
# Start the app
CMD [ "node", "app.js" ]
The .drone.yml file looks like this:
- name: test
- npm install
- npm test
- name: docker
# registry: docker.io
Pipelines are configured by placing a .drone.yml file in the root of your Git repository. The YAML syntax is designed to be easy to read and expressive, so that anyone viewing the repository can understand the workflow. See here for a simple node example.
Step 11: Now, let’s open the Drone dashboard and sync our new repo. Then, we’ll activate it with secrets.
After syncing the repos, you should see all of your repos as follows, and pick the one that we’re building.
The next step is to activate the repository by adding secrets.
After all of the above steps are done, select the NEW BUILD button and it will start building the pipeline.
Here you will see Drone doing its magic by building the code and testing it against the tests that we specified for our application code (remember, we have added a plain Mocha test framework to our application). If the tests pass, then it should go ahead and push a newly built image to our DockerHub as specified by us in the .drone.yml steps.
Moreover, if the test fails, then the application pipeline shouldn’t build and should stop there.
Let’s see what happened to our build.
As expected, the pipeline failed without carrying out the next steps. You can go back, correct the errors, and Drone automatically runs the pipeline. If everything is good, then it pushes the new build to Docker Hub as expected.
When everything is passed, go check your Docker Hub repo that you mentioned in the .drone.yml. It should have pushed the image there. There is the tag that we gave in pavans, and you can see it here:
Congratulations! We’ve successfully built a simple Node.js application, added a simple Mocha test, used Drone to do CI, and pushed the new image to the Docker Hub.
To learn more about how Harness can help you with your CI processes, request a personalized demo.