Unit test in programming
Tags: javascript, beginners, webdev, programming
Published: 2023-04-21 16:20:17

## What is that 🙋?

> In computer programming, unit testing is a software testing method by which individual units of source code-sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures—are tested to determine whether they are fit for use.

> Unit tests are typically automated tests written and run by software developers to ensure that a section of an application (known as the "unit") meets its design and behaves as intended. In procedural programming, a unit could be an entire module, but it is more commonly an individual function or procedure. - Wikipedia.


## Why do we use it? I think it is making us spend more effort to do something which end-users never use ...

![Unit test is useless for end-users](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19nj8vvnfckjelun5ll4.gif)

Yes, it is true. Unit testing may not be directly used by end-users 🦹‍♂️. **BUT** it is still a critical part of the software development process. Here are some reasons why we use unit testing.

### To ensure correctness

Unit testing helps to ensure that each component of a software system behaves as expected. By testing each unit of code in isolation. We can verify that is works correctly before integrating it with other parts of the system.

![correctness in unit test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g6ayosz0l4pida2a8x11.png)


### To catch bugs early

Unit testing help to catch bugs early in the development process, when they are less expensive to fix. By detecting issues as soon as they are introduced, we can avoid downstream problems that might be more difficult and time-consuming to solve.


![To catch bugs early](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3610w9tka4e6tm4squ50.png)



### To facilitate change

Unit testing helps to make it easier to change software over time. By have a suite of tests in place, developers can be confident that their changes do not break existing functionality.


![To facilitate change](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/un7vh5ilix5lw9myxlhi.png)


### To improve code quality

Unit testing helps to improve the quality of software by encouraging good programming practices. By writing tests for each unit of code, developers are forced to think about the expected behavior of their code, which can lead to better-designed and more maintainable software.


![To improve code quality](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8npth701h2lf6n5g2sj.png)

### Fetching image function by Javascript

Here's an example of a simple function in JavaScript that fetches an image from `https://picsum.photos/1080`:

```js
async function fetchImage() {
  const response = await fetch('https://picsum.photos/1080');
  const blob = await response.blob();
  const url = URL.createObjectURL(blob);
  return url;
}
```
And here's an example of a unit test for this function using the Jest testing framework:

```js
test('fetchImage returns a URL', async () => {
  const url = await fetchImage();
  expect(typeof url).toBe('string');
  expect(url).toMatch(/^blob:/);
});
```

This test ensures that the fetchImage function returns a URL string that starts with blob: (since the response from the fetch API is a Blob object). If the test passes, it means that the function is working as expected and can be used in other parts of the code with confidence.

### Summary
1. Unit testing is a software testing technique.
2. Unit tests are typically automated and use specialized testing frameworks or libraries, such as JUnit for Java or Jest for JavaScript.
3. The goal of unit testing is to ensure that each unit of code is correct, reliable, and meets its design requirements.
4. Unit testing is just one part of the software testing process, and it should be used in conjunction with other testing techniques, such as integration testing and system testing, to ensure that the software is of high quality and meets the needs of its users.