29,99 €
Test-driven development (TDD) is a programming workflow that helps you build your apps by specifying behavior as automated tests. The TDD workflow future-proofs apps so that they can be modified without fear of breaking existing functionality. Another benefit of TDD is that it helps software development teams communicate their intentions more clearly, by way of test specifications.
This book teaches you how to apply TDD when building React apps. You’ll create a sample app using the same React libraries and tools that professional React developers use, such as Jest, React Router, Redux, Relay (GraphQL), Cucumber, and Puppeteer. The TDD workflow is supported by various testing techniques and patterns, which are useful even if you’re not following the TDD process. This book covers these techniques by walking you through the creation of a component test framework. You’ll learn automated testing theory which will help you work with any of the test libraries that are in standard usage today, such as React Testing Library. This second edition has been revised with a stronger focus on concise code examples and has been fully updated for React 18.
By the end of this TDD book, you’ll be able to use React, Redux, and GraphQL to develop robust web apps.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 561
Veröffentlichungsjahr: 2022
Build simple and maintainable web apps with React, Redux, and GraphQL
Daniel Irvine
BIRMINGHAM—MUMBAI
Copyright © 2022 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, 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.
Group Product Manager: Pavan Ramchandani
Publishing Product Manager: Bhavya Rao
Senior Editor: Aamir Ahmed
Senior Content Development Editor: Feza Shaikh
Technical Editor: Saurabh Kadave
Copy Editor: Safis Editing
Project Coordinator: Manthan Patel
Proofreader: Safis Editing
Indexer: Manju Arasan
Production Designer: Aparna Bhagat
Marketing Coordinator: Anamika Singh and Marylou De Mello
First published: May 2019
Second edition: September 2022
Production reference: 2130922
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-80324-712-0
www.packt.com
To Nige, my first mentor, who taught me the importance of slowing down.
– Daniel Irvine
For as long as test-driven development(TDD) has existed, there has been debate over whether TDD is feasible for user interface work—even among its staunchest proponents. When people built tools to prove that it’s possible, the debate shifted to whether TDD is as valuable for UIs, considering that programmers can get rapid feedback by seeing and interacting with an interface. Even when TDD’s value is evidenced by smaller, more consistent, single-responsibility units of UI code, some would question whether the same could have been accomplished with less effort by simply following established design patterns.
Is TDD worth your time? Will it result in fewer bugs in your code? Will it improve the design of your system? Will it make future maintenance easier? Maybe.
In the fifteen-or-so years that I’ve been learning, practicing, and teaching test-driven development I have oscillated back-and-forth. Sometimes I relentlessly pursue 100% code coverage, and other times I’ll build worrisomely large applications with no automated tests at all. What I’ve found—and what might surprise some readers—is that my code basically turns out the same whether I practice TDD or not: a similar frequency of bugs, the same idiosyncratic design, and no more or less a burden to maintain.
(At this point, you’d be right to start wondering what this ambivalent foreword is doing in a book designed to teach you TDD.)
The reason that TDD itself doesn’t impact my code very much is because my years of practice have utterly and irrevocably changed me.
I write small single-purpose units, because I’ve felt the sheer exhaustion of writing tests of sprawling unfocused objects.
I avoid mixing levels of abstraction, because I’ve been hopelessly lost in mazes of mock objects that combine testing logic with specifying interactions.
I segregate code coupled to frameworks from feature logic, because I’ve contorted too many tests to fit dependencies that weren’t meant to be tested.
Rigorously practicing TDD transformed my career. Not because it’s the One True Way to program, but because it forces you to ceaselessly ask “how would we test that?” TDD is incredibly challenging at first, but patterns gradually emerge that result in easy-to-test code. And code that’s easy to test, is easy to write. And use. And maintain.
Wherever you are in your journey, I hope this book brings you closer to a similar destination.
Justin Searls VP of Engineering at Test Double
testdouble.com
Daniel Irvine is a software consultant based in London. He works with a variety of languages including C#, Clojure, JavaScript, and Ruby. He’s a mentor and coach for junior developers and runs TDD and XP workshops and courses. When he’s not working, he spends time cooking and practicing yoga. He co-founded the Queer Code London meetup and is an active member of the European software craft community.
I would like to thank the technical reviewer of this edition of the book, Emmanuel Demey, for spotting the weakest points of the text and helping me improve them. I continue to be grateful for my friend and technical reviewer from the first edition, Raimo Radczewski, who is also the author of the expect-redux package that is used in this book.
The team at Packt has been supportive and professional throughout, giving me exactly what I needed to finish the book. For this second edition, I am thankful for the detailed and thoughtful editing that Feza Shaikh has undertaken, helping to deliver the book ahead of schedule. My technical editor, Saurabh Kadave, spotted plenty of mistakes that would have otherwise gone unnoticed. Aamir Ahmed, Bhavya Rao, and Manthan Patel were also instrumental in the production of the second edition.
From the first edition, I am thankful for the hard work of Keagan Carneiro, Sachin Sunilkumar from Packt, and also my friends and colleagues Charlotte Payne, Dan Pelensky, Isidro López, Makis Otman, Sam Szreter, Zach Shaw, Brendan Murphy, and Lucy Monie Hall.
Finally, thank you to all the readers of the first edition who took the time to send me comments and feedback, all of which were invaluable. This second edition is better because of your help.
Emmanuel Demey works with the JavaScript ecosystem every day, and he spends his time sharing his knowledge with anybody. His first goal at work is to help the people that he works with. He speaks at French conferences (Devfest Nantes, Devfest Toulouse, Sunny Tech, Devoxx France, and others) about anything related to the web platform: JavaScript frameworks (Angular, React.js, Vue.js), accessibility, Nest.js, and so on. He has been a trainer for 10 years at Worldline and Zenika (two French consulting companies). He is also a co-leader of the Google Developer of Lille group and a co-organizer of the Devfest Lille conference.
Part 1 introduces all of the basic techniques you’ll need to test-drive React applications. As you build more of the application, you will create a set of library functions that help to simplify and accelerate your testing. The goal is to give you both theoretical and practical advice that will help you apply the test-driven development workflow to your daily work.
This part includes the following chapters:
Chapter 1, First Steps with Test-Driven DevelopmentChapter 2, Rendering Lists and Detail ViewsChapter 3, Refactoring the Test SuiteChapter 4, Test-Driving Data InputChapter 5, Adding Complex Form InteractionsChapter 6, Exploring Test DoublesChapter 7, Testing useEffect and Mocking ComponentsChapter 8, Building an Application Component