Simplify Testing with React Testing Library - Scottie Crump - E-Book

Simplify Testing with React Testing Library E-Book

Scottie Crump

0,0
27,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 247

Veröffentlichungsjahr: 2021

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Simplify Testing with React Testing Library

Create maintainable tests using RTL that do not break with changes

Scottie Crump

BIRMINGHAM—MUMBAI

Simplify Testing with React Testing Library

Copyright © 2021 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Publishing Product Manager: Aaron Tanna

Senior Editor: Hayden Edwards

Content Development Editor: Abhishek Jadhav

Technical Editor: Deepesh Patel

Copy Editor: Safis Editing

Project Coordinator: Manthan Patel

Proofreader: Safis Editing

Indexer: Rekha Nair

Production Designer: Prashant Ghare

First published: April 2021

Production reference: 1160421

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-80056-445-9

www.packt.com

To my mother, Judy, and father, Randall, for always being there and encouraging me to be the best at whatever my heart desires. To my wife, Victoria, for being my loving partner and being patient enough to allow me to work countless hours on this project while holding back the urge to throw my laptop out of the window.

To my son, Liam, my greatest motivator, who gave me the extra energy to stay up late at night to write another sentence and line of code.

– Scottie Crump

Contributors

About the author

Scottie Crump is a senior test engineer with over four years' experience as a consultant working with clients from a variety of sectors, including automotive, retail, telecommunications, and healthcare, among others. Scottie has also worked with numerous students looking to break into the software industry at Coding Bootcamp settings. His background in quality and software engineering and education enables him to be a crucial cog in any client's project. Scottie has helped bring industry best practices and data-driven insights to support key business objectives.

About the reviewer

Alexander Krolick is an open source maintainer of React Testing Library and related projects.

Table of Contents

Preface

Chapter 1: Exploring React Testing Library

Technical requirements

Introducing the DOM Testing Library

What is the DOM Testing Library?

Guiding principles

Executing test cases with Jest

Running tests with Jest

Enhancing Jest assertions with jest-dom

Adding jest-dom to a project

Advantages of using jest-dom with Jest

Testing implementation details

Problems with implementation detail-focused tests

Implementation detail-focused test example

How to move away from implementation detail testing

Summary

Questions

Chapter 2: Working with React Testing Library

Technical requirements

Adding React Testing Library to existing projects

Manual installation

Automatic installation with create-react-app

Structuring tests with React Testing Library

Rendering elements

Selecting elements in the component DOM output

Asserting expected behavior

Testing presentational components

Creating snapshot tests

Testing expected properties

Using the debug method

Debugging the entire component DOM

Debugging specific component elements

Summary

Questions

Chapter 3: Testing Complex Components with React Testing Library

Technical requirements

Testing user events

Simulating user actions with fireEvent

Simulating user actions with user-event

Testing components that call event handlers in isolation

Testing components that interact with APIs

Requesting API data with fetch

Creating mock API data with MSW

Testing the DrinkSearch component

Using MSW in development

Implementing test-driven development

Building the Vote component using TDD

Building a registration form using TDD

Summary

Questions

Chapter 4: Integration Testing and Third-Party Libraries in Your Application

Technical requirements

Testing integrated components

Using integration testing with the Vote component

Planning test scenarios better suited for isolated testing

Testing components that use the Context API

Testing a context consuming Retail component

Testing the Cart component in isolation

Testing the Product component in isolation

Testing the ProductDetail component in isolation

Testing context errors using error boundaries

Using integration testing to test view product details

Testing components that use Redux

Creating a custom render method for testing Redux consuming components

Using the test Redux Provider in tests

Testing components that use GraphQL

Testing Components that use Material-UI

Adding an ARIA label to test a Vote component

Adding a test ID to test a CustomerTable component

Summary

Questions

Chapter 5: Refactoring Legacy Applications with React Testing Library

Technical requirements

Using tests to catch regressions when updating dependencies

Creating the regression test suite

Upgrading the Material UI dependencies

Refactoring tests written with Enzyme

Refactoring tests written with ReactTestUtils

Refactoring tests to comply with common testing best practices

Summary

Questions

Chapter 6: Implementing Additional Tools and Plugins for Testing

Technical requirements

Implementing best practices with Testing Library ESLint plugins

Installing and configuring eslint-plugin-testing-library

Installing and configuring eslint-plugin-jest-dom

Testing accessibility with jest-axe

Selecting the best queries with Testing Playground

Selecting queries using the Testing Playground website

Selecting queries using the Testing Playground Chrome extension

Increasing our testing productivity with Wallaby.js

Installing and configuring Wallaby.js

Writing tests with Interactive Test Output

Summary

Questions

Chapter 7: End-to-End UI Testing with Cypress

Technical requirements

Getting started with Cypress

Enhancing Cypress commands with the Cypress Testing Library

Cypress-driven development

Writing Tests using Cypress design patterns

Creating page objects in Cypress

Creating custom Commands in Cypress

Testing APIs with Cypress

Writing Gherkin-style tests with Cucumber

Using React Developer Tools with Cypress

Summary

Questions

Answers

Chapter 1

Chapter 2

Chapter 3

Chapter 4

Chapter 5

Chapter 6

Chapter 7

Why subscribe?

Other Books You May Enjoy

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:

Log in or register at www.packt.com.Select the Support tab.Click on Code Downloads.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 WindowsZipeg/iZip/UnRarX for Mac7-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.