Book Image

AngularJS Test-driven Development

By : Tim Chaplin
Book Image

AngularJS Test-driven Development

By: Tim Chaplin

Overview of this book

<p>Starting with reviewing the test-driven development (TDD) life cycle, you will learn how Karma and Protractor make your life easier while running JavaScript unit tests. You will learn how Protractor is different from Selenium and how to test it entirely. This book is a walk-through to using TDD to build an AngularJS application containing a controller, model, and scope.</p> <p>Building on the initial foundational aspects, you will expand to include testing for multiple controllers, partial views, location references, CSS, and the HTML element. In addition, you will explore using a headless browser with Karma. You will also configure Karma file watching to automate testing and tackle components of AngularJS (controller, service, model, and broadcasting) using TDD. At the end of this book, you will extend explore how to pull data using an external API, setting up and configuring Protractor to use a standalone Selenium server, and setting up Travis CI and Karma to test your application.</p> <p>This book is a complete guide to testing techniques using Karma for unit testing and performing end-to-end testing with Protractor.</p>
Table of Contents (17 chapters)
AngularJS Test-driven Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

The book will provide the reader with a complete guide to the test-driven development (TDD) approach for AngularJS. It will provide step-by-step, clear examples to continually reinforce TDD best practices. The book will look at both unit testing with Karma and end-to-end testing with Protractor. It will not only focus on how to use the tools, but also on understanding the reason they were built, and why they should be used. Throughout, there will be focus on when, where, and how to use these tools, constantly reinforcing the principles of the TDD life cycle (test, execute, refactor).

What this book covers

This book is basically split into two parts. The initial chapters focus on the TDD life cycle, and how Karma and Protractor fit into the life cycle and development of an AngularJS application. As we proceed, you'll get a step-by-step approach to AngularJS TDD using Karma and Protractor. Each of the chapters builds up on the previous one and introduces how to test several different AngularJS components.

Chapter 1, Introduction to Test-driven Development, is an introduction to the concepts of TDD and testing techniques.

Chapter 2, The Karma Way, explores the origins of Karma and why it is an essential tool for any AngularJS project.

Chapter 3, End-to-end Testing with Protractor, introduces the simplicity of Protractor, an end-to-end testing tool built specifically for AngularJS.

Chapter 4, The First Steps, covers the TDD journey and shows the fundamentals and tools in action.

Chapter 5, Flip Flop, expands to include testing for multiple controllers, partial views, location references, CSS, and HTML element building on the initial foundational aspects learned in the previous chapter.

Chapter 6, Telling the World, dives into communicating across controllers, and testing services and broadcasting.

Chapter 7, Give Me Some Data, dives into how to apply several of the concepts shown previously, and extend them to pull data using an external API.

Appendix A, Integrating Selenium Server with Protractor, walks through setting up and configuring Protractor to use a standalone Selenium server.

Appendix B, Automating Karma Unit Testing on Commit, covers how to set up Travis CI, a platform for continuous integration, and setting up Karma to test your application.

Who this book is for

This book is for the developer who wants to go beyond the basic tutorials, and wants to take the plunge into AngularJS development. This book is for the developer who has experience with AngularJS and has walked through the basic tutorials but wants to understand the wider context of when, why, and how to apply testing techniques and best practices to create quality-clean code. To get the most out of this book, it is preferred that the reader has basic understanding of HTML, JavaScript, and AngularJS.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Create a web page and import calculator.js for testing."

A block of code is set as follows:

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>

<script src="calculator.js"></script>
</body>
</html>

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

$ node calculator.js

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Traditionally, tests were run by having to manually launch a browser and check for results by continually hitting the Refresh button."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.