front matter

preface

Testing JavaScript Applications is the testing book I wish I had read six years ago. At that time, I was a quality assurance (QA) intern. It was my first experience in the software industry. Unfortunately, it didn’t require me to do what I liked the most: casting spells on a keyboard. Instead, I had to manually go through screens, click buttons, fill forms, and make sure that the software we built was working as it should.

“There must be a better way,” I thought. So I started crafting incantations of my own for machines to do the drudgery, liberating me to be the creative wizard I wanted to become.

After 18 months, I thought I had figured most of it out. By then, I had automated myself out of my QA role and become a software engineer.

Once I started writing applications, even more questions popped up in my mind. Having been into QA for a significant amount of time, I didn’t want to depend on others to build software that works. I also didn’t want to spend my precious spell-crafting time clicking buttons and filling forms, as I used to do.

Once again, I thought that “there must be a better way.” That’s when I started reading more about software testing. Now that I had access to the source code, I discovered that I could build software more confidently, in less time. Furthermore, I could liberate my QA friends to perform more creative and proactive work instead of just throwing software over the wall for them to test manually.

The tricky part was finding material that would teach me how to do it. Even though I could sometimes find helpful articles online, most of them were out of date or focused on a small part of the testing puzzle.

Putting those pieces together was the most challenging part of learning about software testing. Should software engineers always write tests? If so, which types of tests, for what, and how many? How do software development and QA fit together?

Years ago, there wasn’t a single piece of content that could answer all these questions. The book I wanted to read didn’t exist; therefore, I decided to write it myself.

While good content is scattered all over the internet, much of it remains unwritten. A big part of the testing puzzle remains unstructured in the brains of those who maintain the testing libraries that others use.

In Testing JavaScript Applications, I put those pieces together in a comprehensible manner. I combined what I’ve learned from many years of reading and practical work experience with what I’ve discovered while maintaining the testing libraries that millions of people use, like Chai and Sinon.

I firmly believe that excellent testing practices are at the core of every successful piece of software out there. These practices help people write better software, in less time, for fewer dollars. Most importantly, they extricate us humans from drudgery and give us time to do what we do best: create software, which, to me, is still pretty much like magic.

acknowledgments

My mom’s alarm clock has always gone off before 6:00 a.m., just like mine. If it wasn’t for her, I don’t know how I could’ve written the 500 pages you’re about to read, most of which I’ve written while the world was asleep. My mom taught me the value of discipline and hard work, and I’m very thankful for that.

Like her, many other people deserve my gratitude for the lessons they taught me and the help they gave me.

Among those people, the first ones I’d like to thank are my family, who cheer for me from the other side of the Atlantic. My father, Hercílio, who said he’d always support me in whatever I’d like to do; my sister, Luiza, the kindest person I know; and my mom, Patrícia, whose hard work I’ve praised in the very first paragraph.

In addition to them, I must also thank my grandparents, the ones who took care of me as my parents were working, and, especially my grandmother, Marli Teixeira da Costa, to whom I dedicate this book.

No matter how hard things were back in Brazil, she always did her best for me to have everything I needed for work, from books to computers. During the week, she used to prepare lunch for me and offered me a room in her house next to the university where I could rest so I could pay attention in class.

Besides my family, there are also a few people without whom I wouldn’t have been able to complete this work: Douglas Melo and Lorenzo Garibotti, who taught me what friendship really means; Ana Zardo, who showed me the world was bigger than I thought; Alex Monk, my therapist, who helped me navigate change and deal with my frequent existential crises; and Gideon Farrell, who brought me to London and continues to trust me and help me do my best work.

I also can’t forget to thank everyone in the JavaScript open source community for everything they’ve taught me over the years: Lucas Vieira, whom I met in college and is one of the most talented engineers I know; Carl-Erik Kopseng, who brought me into Sinon.js and with whom I had the pleasure of working in 2017; and Keith Cirkel, who invited me to be a core maintainer of Chai.js, and has always been a supportive friend. He helped me stay afloat when I moved to England three years ago. I’m glad the internet has connected me with such amazing people!

To all the reviewers: Sergio Arbeo, Jeremy Chen, Giuseppe De Marco, Lucian Enache, Foster Haines, Giampiero Granatella, Lee Harding, Joanna Kupis, Charles Lam, Bonnie Malec, Bryan Miller, Barnaby Norman, Prabhuti Prakash, Dennis Reil, Satej Sahu, Liza Sessler, Raul Siles, Ezra Simeloff, Deniz Vehbi, Richard B. Ward, and Rodney Weis, your suggestions helped make this a better book.

Finally, I’d like to thank my editors and the team at Manning, Helen Stergius, Dennis Sellinger, and Srihari Sridharan, for having reviewed each of these pages and patiently answering the numerous questions I’ve had throughout the process.

Obrigado.

about this book

Testing JavaScript Applications uses practical examples to teach you how to test your JavaScript code and explains what factors you should consider when deciding which tests to write and when.

In addition to covering the most popular JavaScript testing tools and testing best practices, the book explains how different types of tests complement each other and how they could fit into your development process so that you can build better software, in less time, with fewer bugs and more confidence.

Who should read this book

I’ve written Testing JavaScript Applications mostly for junior developers and for the software engineers who think that “there must be a better way” to build software that works but haven’t yet figured out how.

This book assumes readers can already write code but doesn’t require any previous knowledge about software testing.

In addition to covering the practical aspects of writing tests, it explains why they’re important and how they impact your projects and empowers you to make the best possible decisions for your context.

How this book is organized: A roadmap

This book contains 12 chapters divided into three parts.

The first part of Testing JavaScript Applications covers what automated tests are, why they are important, the different types of automated tests, and how each type of test impacts your projects.

  • Chapter 1 explains what automated tests are and the advantages of writing them.

  • Chapter 2 covers the different types of automated tests and teaches you the pros and cons of each one, so you know what to consider when deciding which tests to write. Additionally, it teaches you fundamental patterns you can apply to all kinds of tests.

Part 2 uses practical examples to teach you how to write the different types of tests that you learned about in the first part.

  • Chapter 3 covers testing techniques that help you make the most out of your tests. It teaches you how to organize multiple tests within test suites so that you receive precise feedback, how to write thorough assertions so that you catch more bugs, and which parts of your code you should isolate during tests. Additionally, it explains what code coverage is and how to measure it and shows how misleading it can sometimes be.

  • Chapter 4 teaches how to write tests for a backend application. It covers essential aspects you should consider to make your application testable, demonstrates how to test your server’s routes and its middleware, and how to deal with external dependencies such as databases or third-party APIs.

  • Chapter 5 presents techniques to help you reduce your backend tests’ costs and make them quicker and more reliable. It does so by teaching you how to eliminate unpredictable tests, how to run tests concurrently, and how to reduce the overlap between them.

  • Chapter 6 describes how to test a vanilla JavaScript frontend application. This chapter explains how you can simulate a browser’s environment within your test framework and demonstrates how to write tests that interact with your application’s interface, interface with browser APIs, and handle HTTP requests and WebSockets.

  • Chapter 7 covers the React testing ecosystem. It builds upon what you’ve learned in the previous chapter to explain how tests for a React application work. Additionally, it gives you an overview of the different tools you can use to test React applications and demonstrates how to write your first React tests. Furthermore, it gives you tips on how to apply similar techniques to other JavaScript libraries and frameworks.

  • Chapter 8 digs deeper into the practicalities of testing a React application. In this chapter, I’ll explain how to test components that interact with each other, how to test a component’s styles, and what snapshot testing is and what to consider when deciding whether you should use it. Furthermore, you’ll learn about the importance of component-level acceptance testing and how this practice can help you build better React applications more quickly.

  • Chapter 9 is about test-driven development (TDD). It explains how to apply this software development technique, why it’s helpful to adopt it, and when to do so. Besides covering the practical aspects of TDD, it explains how this technique impacts teams and how to create an environment in which TDD can succeed. It also covers TDD’s relationship to a practice called behavior-driven development, which can help improve communication among different stakeholders and improve your software’s quality.

  • Chapter 10 describes what UI-based end-to-end tests are and how they impact your business. It also explains how these tests differ from other types of tests and teaches you how to decide when to write them.

  • Chapter 11 covers the practical aspect of UI-based end-to-end tests. This chapter will teach you how to write your first UI-based end-to-end tests, how to make them robust and reliable, and how to run them on multiple browsers. Additionally, it describes how to incorporate visual regression testing into your tests and explains how this new type of test could be helpful.

Part 3 covers complementary techniques to amplify the positive impact that writing tests can have on your business.

  • Chapter 12 describes what continuous integration and continuous delivery are and explains why they’re helpful techniques and the essentials you need to know to apply them in your projects.

  • Chapter 13 covers technologies, tools, and techniques complementary to tests. It talks about how types can help you catch bugs and make your tests more efficient, explains how code reviews improve your code quality, and covers the impact that documentation and monitoring have in building software that works. Additionally, it describes how to debug your code more quickly and confidently.

I recommend readers to read the first three chapters sequentially before reading any others. These first few chapters teach fundamental testing concepts and how they relate to each other. It’s essential to read these chapters first because you’ll need the information in them to make the most out of any further chapters.

Then, readers can jump straight to the chapter that interests them the most, depending on the type of software they want to test.

Ideally, readers should go through chapters 12 and 13 only when they have already put tests in place and want to understand how to supplement their testing techniques and infrastructure.

About the code

Testing JavaScript Applications contains numerous practical examples. All of them are available online at this book’s GitHub repository, which readers can find at https://github.com/lucasfcosta/testing-javascript-applications. In that repository, I’ve separated examples into a folder for each chapter. Within each of those chapter’s folders, I’ve grouped examples by section.

Both inline code and separate code listings are formatted using a fixed-width font similar to this one so that you can differentiate it from ordinary text. Sometimes code is also in bold to highlight code that has changed from previous steps in the chapter, such as when a new feature adds to an existing line of code.

In many cases, the original source code has been reformatted; we’ve added line breaks and reworked indentation to accommodate the available page space in the book. In rare cases, even this was not enough, and listings include line-continuation markers (➥). Additionally, comments in the source code have often been removed from the listings when the code is described in the text. Code annotations accompany many of the listings, highlighting important concepts.

I’ve annotated every significant example in this book to highlight important concepts and explain to readers what each piece of code does.

The code for the examples in this book is also available for download from the Manning website at www.manning.com/books/testing-javascript-applications.

System requirements

All of this book’s code samples have been written and tested on macOS Catalina. However, they should work on all platforms, including Linux and Windows.

The only changes you may have to do to get this book’s examples running is adapting how you set environment variables, depending on the shell and operating system you use. If you’re using PowerShell on a Windows machine, for example, you can’t merely prepend VAR_NAME=value to your commands to set an environment variable’s value.

To run this book’s examples, you must install Node.js and NPM on your machine. These two usually come bundled together. When you install Node.js, you’ll usually get NPM, too. To download and install these two pieces of software, you can follow the instructions at https://nodejs.org/en/download/. The versions of Node.js and NPM I’ve used when building this book’s examples were, respectively, 12.18 and 6.14.

liveBook discussion forum

Purchase of Testing JavaScript Applications includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the author and from other users. To access the forum, go to https://livebook.manning.com/#!/book/testing-javascript-applications/ discussion. You can also learn more about Manning’s forums and the rules of conduct at https://livebook.manning.com/#!/discussion.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to the forum remains voluntary (and unpaid). We suggest you try asking the author some challenging questions lest his interest stray! The forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

about the author

Lucas da Costa is a software engineer, published author, international speaker, and professional problem solver. As an active member of the open source community, he is the core maintainer of some of the most popular JavaScript testing libraries, Chai and Sinon. Furthermore, he has contributed to numerous other projects, including Jest and NodeSchool.

In the past few years, Lucas has presented at numerous software engineering conferences in more than 10 countries.

His content has been voluntarily translated into many languages, including Russian, Mandarin, French, Portuguese, and Spanish, and is used as reference material in multiple software engineering courses around the world.

Lucas loves opinionated books, beautiful code, well-engineered prose, command-line interfaces, and Vim. In fact, he loves Vim so much that he has a :w tattooed on his ankle.

about the cover illustration

The figure on the cover of Testing JavaScript Applications is captioned “Bourgeois de Paris.” The illustration is taken from a collection of dress costumes from various countries by Jacques Grasset de Saint-Sauveur (1757–1810), titled Costumes civils de actuals de toue les peuples connus, published in France in 1788. Each illustration is finely drawn and colored by hand. The rich variety of Grasset de Saint-Sauveur’s collection reminds us vividly of how culturally apart the world’s towns and regions were just 200 years ago. Isolated from each other, people spoke different dialects and languages. In the streets or in the countryside, it was easy to identify where they lived and what their trade or station in life was just by their dress.

The way we dress has changed since then and the diversity by region, so rich at the time, has faded away. It is now hard to tell apart the inhabitants of different continents, let alone different towns, regions, or countries. Perhaps we have traded cultural diversity for a more varied personal life—certainly for a more varied and fast-paced technological life.

At a time when it is hard to tell one computer book from another, Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional life of two centuries ago, brought back to life by Grasset de Saint-Sauveur’s pictures.

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

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