Simplify Testing with React Testing Library

Book description

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.

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
      1. What is the DOM Testing Library?
      2. Guiding principles
    3. Executing test cases with Jest
      1. Running tests with Jest
    4. Enhancing Jest assertions with jest-dom
      1. Adding jest-dom to a project
      2. Advantages of using jest-dom with Jest
    5. Testing implementation details
      1. Problems with implementation detail-focused tests
      2. Implementation detail-focused test example
      3. How to move away from implementation detail testing
    6. Summary
    7. Questions
  7. Chapter 2: Working with React Testing Library
    1. Technical requirements
    2. Adding React Testing Library to existing projects
      1. Manual installation
      2. Automatic installation with create-react-app
    3. Structuring tests with React Testing Library
      1. Rendering elements
      2. Selecting elements in the component DOM output
      3. Asserting expected behavior
    4. Testing presentational components
      1. Creating snapshot tests
      2. Testing expected properties
    5. Using the debug method
      1. Debugging the entire component DOM
      2. Debugging specific component elements
    6. Summary
    7. Questions
  8. Chapter 3: Testing Complex Components with React Testing Library
    1. Technical requirements
    2. Testing user events
      1. Simulating user actions with fireEvent
      2. Simulating user actions with user-event
      3. Testing components that call event handlers in isolation
    3. Testing components that interact with APIs
      1. Requesting API data with fetch
      2. Creating mock API data with MSW
      3. Testing the DrinkSearch component
      4. Using MSW in development
    4. Implementing test-driven development
      1. Building the Vote component using TDD
      2. Building a registration form using TDD
    5. Summary
    6. Questions
  9. Chapter 4: Integration Testing and Third-Party Libraries in Your Application
    1. Technical requirements
    2. Testing integrated components
      1. Using integration testing with the Vote component
      2. Planning test scenarios better suited for isolated testing
    3. Testing components that use the Context API
      1. Testing a context consuming Retail component
      2. Testing the Cart component in isolation
      3. Testing the Product component in isolation
      4. Testing the ProductDetail component in isolation
      5. Testing context errors using error boundaries
      6. Using integration testing to test view product details
    4. Testing components that use Redux
      1. Creating a custom render method for testing Redux consuming components
      2. Using the test Redux Provider in tests
    5. Testing components that use GraphQL
    6. Testing Components that use Material-UI
      1. Adding an ARIA label to test a Vote component
      2. Adding a test ID to test a CustomerTable component
    7. Summary
    8. Questions
  10. Chapter 5: Refactoring Legacy Applications with React Testing Library
    1. Technical requirements
    2. Using tests to catch regressions when updating dependencies
      1. Creating the regression test suite
      2. Upgrading the Material UI dependencies
    3. Refactoring tests written with Enzyme
    4. Refactoring tests written with ReactTestUtils
    5. Refactoring tests to comply with common testing best practices
    6. Summary
    7. Questions
  11. Chapter 6: Implementing Additional Tools and Plugins for Testing
    1. Technical requirements
    2. Implementing best practices with Testing Library ESLint plugins
      1. Installing and configuring eslint-plugin-testing-library
      2. Installing and configuring eslint-plugin-jest-dom
    3. Testing accessibility with jest-axe
    4. Selecting the best queries with Testing Playground
      1. Selecting queries using the Testing Playground website
      2. Selecting queries using the Testing Playground Chrome extension
    5. Increasing our testing productivity with Wallaby.js
      1. Installing and configuring Wallaby.js
      2. Writing tests with Interactive Test Output
    6. Summary
    7. 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
      1. Creating page objects in Cypress
      2. Creating custom Commands in Cypress
    6. Testing APIs with Cypress
    7. Writing Gherkin-style tests with Cucumber
    8. Using React Developer Tools with Cypress
    9. Summary
    10. 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

Product information

  • Title: Simplify Testing with React Testing Library
  • Author(s): Scottie Crump
  • Release date: May 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781800564459