-
Book Overview & Buying
-
Table Of Contents
React Application Architecture for Production - Second Edition
By :
Unit testing is a testing method where individual pieces of code (functions, hooks, components) are tested in isolation. External dependencies are replaced with mocks or stubs, ensuring tests are fast, deterministic, and focused solely on the unit's own logic.
The following diagram shows how unit tests work. We test a single piece of code in isolation, providing inputs and verifying the outputs without involving any external systems:

Figure 11.2 – Unit testing
For writing unit tests, we can use Vitest as our test runner and React Testing Library for testing React components and hooks. Vitest is fast, has a great developer experience, and integrates seamlessly with our Vite-based application.
Let's look at how to unit test different parts of our application. Utility functions are perfect candidates for unit testing. They have clear inputs and outputs, no side effects, and don't depend on external state. For example, our authorization policies have more...