Chapter 9. Unit-testing Angular applications

 

This chapter covers

  • The basics of unit testing with the Jasmine framework
  • The main artifacts from the Angular testing library
  • Testing the main players of an Angular app: services, components, and the router
  • Running unit tests against web browsers with the Karma test runner
  • Implementing unit testing in the online auction example

To ensure that your software has no bugs, you need to test it. Even if your application has no bugs today, it may have them tomorrow, after you modify the existing code or introduce new code. Even if you don’t change the code in a particular module, it may stop working properly as a result of changes in another module. Your application code has to be retested regularly, and this process should be automated. You need to prepare test scripts and start running them as early as possible in your development cycle.

There are two main types of testing for the front end of web applications:

  • Unit testing asserts that a small unit of code (such as a component or function) accepts the expected input data and returns the expected result. Unit testing is about testing isolated pieces of code, especially public interfaces. That’s what we’ll discuss in this chapter.
  • End-to-end testing asserts that the entire application works as end users expect and that all units properly interact with each other. For end-to-end testing of Angular 2 applications, you can use the Protractor library (see https://angular.github.io/protractor).

9.1. Getting to know Jasmine

9.2. What comes with Angular’s testing library

9.3. Testing a sample weather application

9.4. Running tests with Karma

9.5. Hands-on: unit-testing the online auction

9.6. Summary

sitemap