Preface

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 utilize this modern, user-friendly tool to test React components, explaining its benefits to reduce risk in React applications.

The book demonstrates code snippets that will allow you to implement RTL with ease, helping you understand the guiding principles of the DOM Testing Library to write tests from the user's perspective. You'll explore the advantages of testing components from the perspective of individuals who will actually use your components, and you'll 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 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 with ease.

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..

What this book covers?

Chapter 1, Exploring React Testing Library, will help you understand the guiding principles of the DOM Testing Library to write tests from the perspective of the user. You will learn the disadvantages of implementing detail-focused testing. Finally, you will learn the advantages of using jest-dom to enhance our tests using RTL.

Chapter 2, Working with React Testing Library, will teach you how to add RTL to React projects. You will learn how to properly structure tests using the API. You will look at presentational components and write some initial tests. Finally, we will learn how to use the debug method to assist us in the process of writing tests.

Chapter 3, Testing Complex Components with React Testing Library, will help you understand how to test more complex React components. You will learn how to simulate user interactions with Fire Event and the user event module. You will learn how to use TDD to drive the process of writing tests. Finally, you will unit test components that interact with APIs.

Chapter 4, Integration Testing and Third-Party Libraries in Your Application, teaches you how to test components in various React applications. You will learn how to test integrated components and how to test components using the Context API. Finally, you will test components using popular third-party libraries such as GraphQL and Redux.

Chapter 5, Refactoring Legacy Applications with React Testing Library, will teach you strategies for dealing with breaking changes while refactoring legacy React applications. You will learn how to update production packages while using RTL tests to guide you in resolving breaking changes. You will also learn how to convert tests written in Enzyme or ReactTestUtils to RTL.

Chapter 6, Implementing Additional Tools and Plugins for Testing, will help you learn additional tools to enhance your ability to test React applications using RTL.

Chapter 7, End-To-End UI Testing with Cypress, will teach you how to write end-to-end UI tests using the popular Cypress library. You will learn design patterns to structure your tests. You will learn how to test APIs with Cypress. Finally, you will learn how to integrate Cucumber and React Developer Tools into your test suite.

To get the most out of this book

You will need NodeJS and npm installed on your computer—the latest versions, if possible. All code examples have been tested using RTL version 11.2.5 on macOS Big Sur version 11.2.3. However, they should work with future version releases too.

You are free to use any code editor for chapter examples. However, suggested extensions to enhance your experience when running examples are provided for the VSCode editor.

If you are using the digital version of this book, we advise you to type the code yourself or access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related to copy/pasting of code.

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packt.com.
  2. Select the Support tab.
  3. Click on Code Downloads.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Simplify-Testing-with-React-Testing-Library. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781800564459_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system."

A block of code is set as follows:

html, body, #map {

height: 100%;

margin: 0;

padding: 0

}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

[default]

exten => s,1,Dial(Zap/1|30)

exten => s,2,Voicemail(u100)

exten => s,102,Voicemail(b100)

exten => i,1,Voicemail(s0)

Any command-line input or output is written as follows:

$ mkdir css

$ cd css

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select System info from the Administration panel."

Tips or important notes

Appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
54.152.77.92