What Is Unit Testing, and Why Is It Important?

trialShutterstock / Viktoria Kazakova

Unit tests are the process of automatically writing and running tests to ensure that the functions you code work as expected. While this may seem like more work, it’s really about taking preventative steps to eliminate bugs before they happen.

What is a unit test?

A “unit” is the smallest possible software component in your application (that is, functions, classes, or components). Individual unit tests help ensure that the core component of your application behaves as expected, and that future engagement in one area of ​​your code will not interrupt code in another. If so, you probably have a bug in your new or old code (or in a poorly written / outdated test).

The purpose of unit testing is obvious: to reduce bugs, especially bugs resulting from integration. A developer may think everything is fine locally and commit their code, only to find that another commit broke the application. Unit testing helps catch some of these flaws before they turn into issues, and when combined with automated continuous integration pipelines, they can ensure that everyday build is still running smoothly.

Unit testing isn’t just about small pieces of code; You can also test larger components that use several other functions, which themselves can be unit tested. This helps you to locate errors more efficiently. Is the error in the methods of the large component object or in one of the other components it uses?

While unit tests are important, they aren’t the only tests you should be doing either. Running end-to-end user interface tests and manual manual examination will find many logic bugs that unit tests can miss when each unit is performing as expected.

Unit tests lead to cleaner code bases

One of the main issues with legacy code bases is dinosaur code – code so old that it’s basically a black box, you might not have a clue how it works, but one way or another it works and you don’t want to refactor it out of fear might break everything.

In a way, when you write unit tests, you write documentation for it. You might not need to write a full manual, but you will still define two things: what to give the function and what to return, in the same way you would define an API schema. . With these two pieces of information, you clearly know what the function does and how to integrate it into your application. Obviously, unit testing doesn’t fix existing code bases, but it prevents you from writing that kind of dinosaur code in the first place.

Often times, you will be able to write your tests before the function you are testing. If you know what your function should do, writing the test first requires you to think about the end result of your code and what it is responsible for.

RELATED: What is TypeScript and should you use it instead of Vanilla JS?

If you like this effect of unit testing, you might also be interested in Manuscript—A compiled superset of JavaScript that makes it strongly typed. You’ll always want to write unit tests, but knowing what types a function gives and takes while you code is a very useful feature.

How to run unit tests

There are many different unit testing frameworks out there, and which one you ultimately use will depend on the language you are testing. To show how they work we will use To joke, a JavaScript testing framework that is the default for new React apps.

A unit test typically consists of three steps:

Organize, where the data is prepared for the unit to be tested. If you need to retrieve data, create a complex object, or just need to configure things, you need to do it first.
Act, where the unit is called and the response is recorded.
Affirm where most of the testing takes place. This is where you write Boolean operations based on the

If any of the assertions fail, the unit failed the test and you will get a detailed log and stack trace of what went wrong, what you expected and what actually went wrong. returned.

Jest has a bunch of different matchers, which let you perform quick and easy assertions. For example, let’s say you have the following function, which just adds two numbers:

function doSomeMath (a, b) {
returns a + b;

You can test this function with the following statement:

test (‘Expect the math to work’, () => {
expect (doSomeMath (1, 1)). toBe (2);

Usually this is saved with the function under functionName.test.js. Jest will automatically search for these files when running the tests.

RELATED: How to get started with Jest for JavaScript unit testing

The .toBe () function is the matcher, in this case verifying base equality. There are many more, like .toBeEqual (), which checks the equality of objects, and .toContain (), which checks the contents of the array. You can read Jest Documents for a full list of their supported correspondents.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.