Test-Driven React, Second Edition
Find Problems Early, Fix Them Quickly, Code with Confidence
by Trevor Burnham
Turn your React project requirements into tests and get the feedback you
need faster than ever before. Combine the power of testing, linting, and
typechecking directly in your coding environment to iterate on React
components quickly and fearlessly!
You work in a loop: write code, get feedback, adjust. The faster you get
feedback, the faster your code improves and the more effective you
become as a developer. And that feedback comes from testing.
The conceptual elegance of React has opened the door to a new generation
of web testing: clear, expressive, and lightning fast. That makes React
a perfect fit for test-driven development (TDD), a methodology in which
tests are a blueprint instead of an afterthought.
Each chapter presents new projects to challenge you and new tools to
help you:
- Write fast, easy-to-understand tests with Vitest and React Testing
Library.
- Keep your code tidy with ESLint and Prettier.
- Perform every task you need from a single window within VS Code.
- See your test output directly in your code with Wallaby.
- Style components in a test-friendly way with styled-components.
- Stay focused on what’s important by running your tests continuously
in the background.
- Generate beautiful documentation with Storybook, and update it on
every commit with GitHub Actions.
The second edition of Test-Driven React has been extensively revised
to reflect the latest tools and techniques for React development,
including TypeScript. Each chapter presents new projects to challenge
you and new tools to help you detect and fix bugs in your code.
What You Need
You’ll need a working knowledge of JavaScript or TypeScript, and a
computer running macOS, Windows, or Linux.
Resources
Releases:
2024/09/25
P1.0
First printing.
2024/09/04
B4.0
*Copy edit and indexing are complete. Now it’s on to layout and the printer.
2024/07/10
B3.0
• Fixed the “npm create vite” command. Thanks to reader Pete Latham for reporting this bug in the Devtalk forum!
• Removed an outdated instruction to add file extensions to the lint/format scripts. Thanks to Pete Latham again for this report!
• Fixed test code that used the var name nextButton to refer to the “Prev” button. Thanks to Henrik Horneber for this report!
• Fixed incorrect eslint-plugin-react-hooks version number in install command. Thanks to Henrik Horneber again!
• Fixed some leftover references from the first edition of the book. Thanks to Henrik Horneber again!
• Fixed multiple book markup issues in example code. Thanks to Henrik Horneber again!
• The book is content-complete and headed to copy edit and indexing.
2024/04/18
B2.0
*Added new chapter, “Continuous Integration and Collaboration.”
*Bug fixes! Thanks to reader Jim Speakman for reporting these issues in the Devtalk forum.
- Introduction
- What’s in This Book
- What’s Not in This Book
- What’s New in the Second Edition
- How to Read the Code Examples
- Online Resources
- Mantra: Code with Joy
- Test-Driven Development with Jest
- Integrated Tooling with VS Code
- Editing with VS Code
- Checking Code Quality with ESLint
- Beautifying Code with Prettier
- Real-Time Testing with Wallaby
- Mantra: Live in the Code
- Example Palindrome Finder Solution
- Testing React with Testing Library
- Starting a React Project
- Testing Simple Components with React Testing Library
- Testing Nested Markup
excerpt

- Testing Stateful Components
- Mantra: Test One Piece at a Time
- Styling in JavaScript with Styled-Components
- Adding an Example Page
- Getting Started with Styled-Components
- Testing Styled Components
excerpt

- Taking Snapshots of Styles
- Mantra: Actively Seek Feedback
- Refactoring with Hooks
- Making Custom Hooks
- The Controllable Pattern
- Testing with Timers
- Setting Timers with the useEffect Hook
- Preventing Unnecessary Updates with useCallback and useRef
- Inspecting Components with React Devtools
- Mantra: Keep the Unit Small
- Continuous Integration and Collaboration
- Setting up Travis CI
- Git Hooks with Husky
- Adding Docs with Storybook
- Mantra: Actively Automate
Author
Trevor Burnham is a front-end engineer at AWS. His previous books
include CoffeeScript and Async
JavaScript, both
published by The Pragmatic Bookshelf.