0%

A fast-paced, practical guide to helping you leverage React Testing Library to test the DOM output of components

Key Features

  • Get to grips with React Testing Library and create tests that don't break with changes in implementation
  • Learn how to put RTL into practice by implementing it in real-world scenarios
  • Test apps to be more accessible and ensure your tests will work with actual DOM nodes

Book Description

React Testing Library (RTL) is a lightweight and easy-to-use tool for testing the document object model (DOM) output of components. This book will show you how to use this modern, user-friendly tool to test React components, reducing the risk that your application will not work as expected in production.

The book demonstrates code snippets that will allow you to implement RTL easily, helping you to understand the guiding principles of the DOM Testing Library to write tests from the perspective of the user. You'll explore the advantages of testing components from the perspective of individuals who will actually use your components, and use test-driven development (TDD) to drive the process of writing tests. As you advance, you'll discover how to add RTL to React projects, test components using the Context API, and also learn how to write user interface (UI) end-to-end tests using the popular Cypress library. Throughout this book, you'll work with practical examples and useful explanations to be able to confidently create tests that don't break when changes are made.

By the end of this React book, you will have learned all you need to be able to test React components confidently.

What you will learn

  • Explore React Testing Library and its use cases
  • Get to grips with the RTL ecosystem
  • Apply jest-dom to enhance your tests using RTL
  • Gain the confidence you need to create tests that don't break with changes using RTL
  • Integrate Cucumber and Cypress into your test suite
  • Use TDD to drive the process of writing tests
  • Apply your existing React knowledge for using RTL

Who this book is for

This book is for software engineers, quality engineers and React developers who want to learn about modern practices used for testing React components using the latest testing tool, RTL. Basic knowledge of React development is required to get the most out of this book.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Simplify Testing with React Testing Library
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for?
    2. What this book covers?
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Reviews
  6. Chapter 1: Exploring React Testing Library
    1. Technical requirements
    2. Introducing the DOM Testing Library
    3. What is the DOM Testing Library?
    4. Guiding principles
    5. Executing test cases with Jest
    6. Running tests with Jest
    7. Enhancing Jest assertions with jest-dom
    8. Adding jest-dom to a project
    9. Advantages of using jest-dom with Jest
    10. Testing implementation details
    11. Problems with implementation detail-focused tests
    12. Implementation detail-focused test example
    13. How to move away from implementation detail testing
    14. Summary
    15. Questions
  7. Chapter 2: Working with React Testing Library
    1. Technical requirements
    2. Adding React Testing Library to existing projects
    3. Manual installation
    4. Automatic installation with create-react-app
    5. Structuring tests with React Testing Library
    6. Rendering elements
    7. Selecting elements in the component DOM output
    8. Asserting expected behavior
    9. Testing presentational components
    10. Creating snapshot tests
    11. Testing expected properties
    12. Using the debug method
    13. Debugging the entire component DOM
    14. Debugging specific component elements
    15. Summary
    16. Questions
  8. Chapter 3: Testing Complex Components with React Testing Library
    1. Technical requirements
    2. Testing user events
    3. Simulating user actions with fireEvent
    4. Simulating user actions with user-event
    5. Testing components that call event handlers in isolation
    6. Testing components that interact with APIs
    7. Requesting API data with fetch
    8. Creating mock API data with MSW
    9. Testing the DrinkSearch component
    10. Using MSW in development
    11. Implementing test-driven development
    12. Building the Vote component using TDD
    13. Building a registration form using TDD
    14. Summary
    15. Questions
  9. Chapter 4: Integration Testing and Third-Party Libraries in Your Application
    1. Technical requirements
    2. Testing integrated components
    3. Using integration testing with the Vote component
    4. Planning test scenarios better suited for isolated testing
    5. Testing components that use the Context API
    6. Testing a context consuming Retail component
    7. Testing the Cart component in isolation
    8. Testing the Product component in isolation
    9. Testing the ProductDetail component in isolation
    10. Testing context errors using error boundaries
    11. Using integration testing to test view product details
    12. Testing components that use Redux
    13. Creating a custom render method for testing Redux consuming components
    14. Using the test Redux Provider in tests
    15. Testing components that use GraphQL
    16. Testing Components that use Material-UI
    17. Adding an ARIA label to test a Vote component
    18. Adding a test ID to test a CustomerTable component
    19. Summary
    20. Questions
  10. Chapter 5: Refactoring Legacy Applications with React Testing Library
    1. Technical requirements
    2. Using tests to catch regressions when updating dependencies
    3. Creating the regression test suite
    4. Upgrading the Material UI dependencies
    5. Refactoring tests written with Enzyme
    6. Refactoring tests written with ReactTestUtils
    7. Refactoring tests to comply with common testing best practices
    8. Summary
    9. Questions
  11. Chapter 6: Implementing Additional Tools and Plugins for Testing
    1. Technical requirements
    2. Implementing best practices with Testing Library ESLint plugins
    3. Installing and configuring eslint-plugin-testing-library
    4. Installing and configuring eslint-plugin-jest-dom
    5. Testing accessibility with jest-axe
    6. Selecting the best queries with Testing Playground
    7. Selecting queries using the Testing Playground website
    8. Selecting queries using the Testing Playground Chrome extension
    9. Increasing our testing productivity with Wallaby.js
    10. Installing and configuring Wallaby.js
    11. Writing tests with Interactive Test Output
    12. Summary
    13. Questions
  12. Chapter 7: End-to-End UI Testing with Cypress
    1. Technical requirements
    2. Getting started with Cypress
    3. Enhancing Cypress commands with the Cypress Testing Library
    4. Cypress-driven development
    5. Writing Tests using Cypress design patterns
    6. Creating page objects in Cypress
    7. Creating custom Commands in Cypress
    8. Testing APIs with Cypress
    9. Writing Gherkin-style tests with Cucumber
    10. Using React Developer Tools with Cypress
    11. Summary
    12. Questions
  13. Answers
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Why subscribe?
  14. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Leave a review - let other readers know what you think
3.95.2.54