Software development processes have changed drastically over the years, from agile development to the DevOps practices we see today. Additionally, software testing has gained tremendous momentum through new tools and automation. Testing software relies heavily upon continuous integration (CI) best practices and tools in the modern cloud-native world. Organizations need to focus on automated testing and make testing easy for developers so they can focus on building rich features. Hence, software testing is an essential process in the software development lifecycle (SDLC). So what’s the best way to ensure that your code continues to work as you make future changes? Write unit tests. Unit testing is a crucial part of software development that has grown in popularity over the past few years. Why? Because it helps you identify potential bugs or issues before they occur and it prevents them from ever making their way into production.
In this blog post, we’ll go over the basics of unit testing with Jest as an example, see why unit testing is important, and show you how you can implement it in your project.
Unit tests are small chunks of code that can validate individual functions, features, or methods. These tests focus on testing the logic behind your code, rather than its behavior. They don’t test user interfaces or performance, but rather test to see if the code you wrote meets the expectations. Unit tests are also known as “test-driven development” (TDD). You might wonder why every code/feature gets tested with unit tests because it has many benefits: it can catch bugs before they go into production, prevent regression, and improve code quality over time. The main objective of unit testing is to ensure that individual units of source code operate as intended and do not have any hidden bugs or defects. Unit testing isn’t something you can turn on and off like a switch; it’s a process that needs to be part of your development workflow from the beginning.
Image credit: openbase
You should see the package.json file getting created after you run the above command on your terminal.
Now, you can go back to your package.json file and confirm that Jest has been added as a dependency.
Finally, your package.json file should look like this:
In the above example, we are basically using mathOperations - addition, subtraction and multiplication - as our logic.
You should see that the tests passed successfully for the inputs we provided in the calculator.test.js file. You can alter this file and add your own values.
The source code of the example project is shared here: https://github.com/pavanbelagatti/jest-unit-test-drone
In our example project, the codebase is small and we can easily manage it, but what if the project gets bigger and more developers start working on it?
Developers usually collaborate and work with DevOps tools for more productivity and efficiency. One prominent tool is GitHub, which helps developers push and merge their code to the main branch. Whenever developers push their code, there has to be a system to automatically test the code and alert for possible bugs or errors. Manual testing takes a lot of time and it is not trustworthy in most cases. This is where continuous integration comes into the picture. Drone CI by Harness, a continuous integration testing tool, has emerged as one of the best open-source tools in the market. It tests and builds against the tests specified. It’s also very simple to set up and easy to understand.
Let’s set up a Drone CI server on our local machine to automate our testing.
My colleague’s article will help you get your own Drone CI up and running.
Using a CI server like Drone helps companies find bugs and rectify the errors before they reach customers.
While working with Drone CI, you need a configuration file .drone.yml. Below is the sample file for node projects:
Our code repo already has this configuration file.
When you open the Drone UI, you will see a welcome message and you need to enter some basic information as part of the sign up process.
Once you log in, you will see all your repositories listed. Select the one we are working on. Activate the repository by going to the settings and adding secrets. Secrets are basically your Docker hub username and password.
Click on the new build after adding the secrets. You should see the screen as shown below:
You should see the execution steps when you hover over the build pipeline:
How cool is that. You can see the test executed successfully. Now, whenever someone pushes any code to the main branch, the drone starts its magic and runs the test specified, in this case, the Jest unit test framework.
Would you like to try other Node.js testing frameworks? Check out my other article, which shows how to use Mocha as the unit testing framework for a simple Node.js application and how to automate it with Drone CI.
Some of you might wonder if there is a Drone extension you can use instantly without setting up Drone on your computer. Well, we have it for you. You just need to have Docker Desktop installed on your computer, and using the below command, you can easily set up the Drone extension in a minute.
See, it is very straightforward. Import the pipelines.
Then click on the start button, which is below the actions tab.
As soon as you click the start button, you should see your pipeline running.
Finally, you should see the pipeline completed successfully.
Congratulations on successfully running your CI pipeline through Drone.
To request a personalized demo of Harness, visit us at https://harness.io/demo
Enjoyed reading this blog post or have questions or feedback?
Share your thoughts by creating a new topic in the Harness community forum.