A beginner’s guide to unit testing with Jest | Part 1

Syed Safayet Karim
REWRITE TECH by diconium
4 min readOct 25, 2022

The why, what and how of unit testing

This first part of a three part series serves as an introduction to unit testing and explains why it is important. In the second part we will get to know about Jest and how to write unit tests with it. In the final part we will follow up with some hands-on experience in writing some advanced tests using Jest. The aim of this series would be to serve as a beginner’s guide for those looking to get started in writing their own Jest tests. So without further ado, let’s dive in.

Testing software is as important as developing it, since it can be used to find out whether the software meets the actual requirements or not. Moreover, any error in code can be identified at the beginning itself thus making it cost-effective as it saves time to rectify the issues at a later stage.

The topic of software testing can often feel overwhelming because of the numerous possible testing situations which is why it’s important to understand what part of the code should actually be tested so that the tests are meaningful.

Unit test written with Jest (source: Unsplash)

There are different types of testing but mainly all of the tests fall into these main categories:

* Unit testing

* Integration testing

* End to end testing

* UI testing

So what actually are unit tests?

Unit tests verify the smallest parts of your application in complete isolation. The purpose of unit testing is to validate that each unit of the software performs as designed. As suggested by the name, they test individual units such as methods, functions, and objects.

Let’s check out some of the benefits that unit testing brings along. Unit testing:

* can be executed faster

* setting up the environment is relatively easy

* helps to understand in case of failing tests due to the elaborate error messages.

* increases code quality as we are able to validate that our application works as expected for both us and our users.

* is cost-effective because the earlier you are able to catch bugs, the earlier it can be fixed.

* can act as kind of living documentation of the product. Developers can refer to unit tests to get an understanding of the logic behind the code and the system as a whole.

* helps to reduce code complexity. Writing tests should be simple and easy and once the creation becomes complex, it actually gives us a hint that the code that we are trying to test might be overly complicated too. This allows us to make the code more readable and less complex.

One of the standard approaches for writing tests is actually writing them before any actual code is implemented. This approach is more commonly known as test-driven development (TDD). In TDD, specific test cases reflect the requirements of the software, then the software is developed and updated by validating it to pass the new tests. The best part of this approach is that not a single line of code is added that hasn’t been proven to meet defined requirements. Writing tests first might be challenging at the beginning but with time and practice this approach will become easier to follow. Anyways, if tests are written first or after the code is complete, the significance of unit tests in my opinion will be the same — as long as we cover the code we write.

One of the most common question regarding testing is “ How do I know what to test ” ?

Well, talking from my personal experience when I started writing my first tests, I was kind of not always sure what needs to be tested. But with time it became somewhat easier to get the hang of it and to realise which test cases added value. So not to get too overwhelmed in the initial phase, I would suggest to start testing with something simple and one of the easiest and smallest units to test would be a simple function — a pure one.

Meaning that for the same input the function output would always be the same.

Some more advanced test scenarios would be to test every page of the web application to validate whether the expected data exists or not and the expected behaviour from the possible user interactions. Checking if the data being properly fetched from an endpoint would be also a good test case. The usual test flow would be to import the component or function that we want to test and then check if we receive the expected output based on the given input.

Conclusion

Unit testing is the simplest way to improve the quality of our applications since it helps finding bugs and defects in the code early on and it makes it easier to validate if the application is working as expected. All of the factors listed above are interconnected and imply that unit testing undeniably contributes to improving your software. So let’s make it a habit to write tests whenever we implement code. For more information on how to write unit tests with Jest, check out the next part of this series. Till then, happy coding!

--

--