Book Image

Angular Test-Driven Development - Second Edition

By : Md. Ziaul Haq
Book Image

Angular Test-Driven Development - Second Edition

By: Md. Ziaul Haq

Overview of this book

<p>This is a complete guide that shows you testing techniques with Karma that will help you perform unit testing and end-to-end testing with Protractor. It will show you how to optimize your Angular development process using TDD techniques and ensure your final project is free of bugs. All examples in this book are based on Angular v2 and are compatible with Angular v4.</p> <p>We start by reviewing the TDD life cycle, TDD in the context of JavaScript, and various JavaScript test tools and frameworks. You will see how Karma and Protractor can make your life easier while running JavaScript unit tests. We will enable you to build a test suite for an Angular application and build a testable medium-to-large scale Angular application by handling REST API data.</p> <p>Building on the initial foundational aspects, we move on to testing for multiple classes, partial views, location references, CSS, and the HTML element. In addition, we will explore how to use a headless browser with Karma. We will also configure a Karma file to automate the testing and tackle elements of Angular (components, services, classes, and broadcasting) using TDD.</p> <p>Finally, you will find out how to pull data using an external API, set up and configure Protractor to use a standalone Selenium server, and set up Travis CI and Karma to test your application.</p>
Table of Contents (15 chapters)
Angular Test-Driven Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface

Preface

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

All examples in this book are based on Angular v2 and are compatible with Angular v4.

What this book covers

Chapter 1, Introduction to Test-Driven Development, introduces us to the fundamentals of test-driven development by explaining why and how TDD can help in the development process.

Chapter 2, Details of JavaScript Testing, covers TDD in a JavaScript context. This chapter explores the types of tests that need in JavaScript applications, such as unit testing, behavior testing, integration testing, and E2E testing. This also explains different types of JavaScript testing tools, frameworks, and their uses in the Angular app.

Chapter 3, The Karma Way, explores the origins of Karma and why it has to be used in any Angular project. By the end of this chapter, readers will not only understand the problem that Karma solves but also will walk through a complete example using Karma.

Chapter 4, End-to-End Testing with Protractor, takes a look at end-to-end testing applications, through all the layers of an application. This chapter introduced readers to Protractor, the end-to-end testing tool from the Angular team. Then it explained why Protractor was created and how it solve the problems. Finally, it guide the readers step by step how to install, configure, and use Protractor with TDD on an existing Angular project.

Chapter 5, Protractor, a Step Ahead, goes a bit deeper with Protractor and explores some advanced configurations. And then it explains the test suite debugging process with examples. This also explores some commonly used Protractor APIs with the relevant example.

Chapter 6, The First Step, provides an introductory walk-through of how to use TDD to build an Angular application with classes, components, and services. This chapter helps readers to begin the TDD journey and see the fundamentals in action. Up to this point, this book stays focused on a foundation of TDD and the tools. And then, by going a step forward it dives into TDD with Angular.

Chapter 7, Flip Flop, continues to expand our knowledge of applying TDD with Angular features such as routes and navigation to get the result set through our sample Angular application. Besides the Angular features, this also guides the readers how to apply end-to-end tests for those specific features with the help of Protractor. 

Chapter 8, Telling the World, covers more unit testing for the sample Angular application, including routes and navigation. Besides that, this chapter also refactor the existing code to make it more testable. And then it introduces to Angular services and event broadcasting while refactoring the code and introduce MockBackend to test HTTP requests in the service.

What you need for this book

In this book, we have used Node Package Manager (npm) as our command tool to run the application and various testing tools. So it's a prerequisite to have npm globally installed on your operating system. To install it, you must have Node.js installed on your operating system. 

We will not look at how to install Node.js and npm. There are a lot of resources already available for installing them on any operating system. 

Who this book is for

This book is for developers who have basic experience with Angular but want to understand the wider context of when, why, and how to apply testing techniques and the best practices to create quality clean code. To get the most out of this book, you should have a good understanding of HTML, CSS, and JavaScript, and a basic understanding of Angular with TypeScript.

Conventions

In this book, you will find a number of text styles 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: "The next lines of code read the link and assign it to the to the Calculator function."

A block of code is set as follows:

var calculator = {
   multiply : function(amount1, amount2) {
       return amount1 * amount2;
   }
};

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

<!DOCTYPE html>
<html>
  <head>
    <title>Test Runner</title>
  </head>
  <body>
    // ...
    <script src="calculator.js"></script>
  </body>
</html>

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

$ npm install protractor
$ npm protractor --version

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "In order to download new modules, we will go to FilesSettings | Project Name | Project Interpreter".

Tip

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 [email protected], 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 for this book from your account at http://www.packtpub.com. 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.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

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/Angular-Test-Driven-Development-Second-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/AngularTestDrivenDevelopment_ColorImages.pdf.

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 copyrighted 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 [email protected] 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

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.