-
Book Overview & Buying
-
Table Of Contents
Full-Stack React, TypeScript, and Node - Second Edition
By :
In this chapter, we covered the essential tools and techniques for testing React applications. We started with Vitest, a modern test runner that shares most of its API with Jest while offering faster execution and seamless integration with Vite. We learned how to structure tests into suites, set up shared state with lifecycle functions, and write assertions using a variety of matchers.
We then introduced React Testing Library, which encourages testing from the user's perspective. We verified that our components produce the expected output in a simulated browser environment powered by jsdom, using queries that search by text content, test IDs, and roles.
The largest section of the chapter focused on mocking. We explored vi.fn for standalone mock functions, vi.mock for replacing entire modules, and vi.spyOn for intercepting internal calls. These three strategies allow us to isolate the code under test and verify behavior without relying on real network calls or external services...
Change the font size
Change margin width
Change background colour