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

Testing with Angular and Karma


The purpose of this first test using Karma is to create a dynamic to-do list. This walk-through will follow the TDD steps we discussed in Chapter 1, Introduction to Test-Driven Development: test first, make it run, and make it better. This will allow us to gain more experience in using TDD with an Angular application.

A development to-do list

Before we start the test, let's set our focus on what needs to be developed using a development to-do list. This will allow us to organize our thoughts.

Here is the to-do list:

  • Maintain a list of items: The example list consists of test, execute, and refactor

  • Add an item to the list: The example list after we add the item is test, execute, refactor, and repeat

  • Remove an item from the list: The example list after we add and remove the item is test, execute, and refactor

Testing a list of items

The first development item is to provide us with the ability to have a list of items on a component. The next couple of steps will walk...