What is unit test
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the Pain Free Mocking with Jest course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Pain Free Mocking with Jest, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
[00:00 - 00:19] In this lesson, we will learn about unit testing a crucial aspect of software development. We will explore what unit testing is, the different approaches to unit testing, and how we can use test doubles to run tests in isolation. So let's dive in.
[00:20 - 00:58] Unit testing is the process of verifying small independent pieces of code known as units. This unit can be functions, methods, or even classes. The purpose of unit testing is to ensure that each unit performs as expected and functions correctly. Unit testing possesses several key attributes. Firstly, it verifies the small pieces of code a line developers to focus on specific functionality. Secondly, it executes tests quickly, a line for rapid feedback on the code's behavior.
[00:59 - 01:15] Thirdly, it does so in an isolated manner. The first two points here are pretty non-controversial. The third point is where people have vastly different opinions. We review what this all means.
[01:16 - 01:46] One approach emphasizes testing units in isolation from their dependencies. This approach involves using test doubles such as mocks or stops to simulate the behavior of those dependencies and isolates the unit on the test. Imagine we're testing a function with several dependencies it relies on. On the left, the test case needs to set up and run the entire system, including all the help has or dependencies just to test the function.
[01:47 - 02:00] On the right, we use test doubles to replace the dependencies. You may ask what are test doubles. A simple analogy to understand what test doubles are just as one double ensures the safety of our actor while filming a dangerous scene.
[02:01 - 02:26] Test doubles are replacements for dependencies or collaborators. Test doubles are simplified versions that mimic the essential behavior of the real helpers or dependencies, but much faster and easier to set up. By isolating the unit's form its dependencies, we can focus solely on testing the unit's behavior without worrying about the correctness of other components it relies on.
[02:27 - 02:36] In the other approach to unit testing, it's not the code that needs to be tested in an isolated manner. Rather, unit tests themselves should be run in isolation from each other.
[02:37 - 02:59] To do this, we need to make sure that different tests do not have a shared state. Typical example of such a shared state will be out of process dependencies like the database or the file system. I've been talked about the attributes of a unit test. You might ask what exactly is a unit. A unit can be defined as the smallest testable part of an application.
[03:00 - 03:35] A unit can be a single function, a method, or even a small portion of code that performs a specific task. In object-oriented programming, a unit will be a class. While in a programming language like JavaScript, a unit can be a single function. A good unit test will point out poor code quality. Basically, if you find a piece of code hard to test, it means it needs improvement. In the next lesson, we'll further discuss unit testing specifically, entry and exit points, and how they can inform how you test the unit.