8 Unit testing in modern Angular

 

This chapter covers

  • What unit tests are and how they work in Angular
  • Setting up a unit testing environment
  • Writing unit tests for Angular building blocks
  • Unit testing classes that use the inject function
  • Unit testing signals
  • Third-party tools that facilitate unit testing of Angular applications
  • AI tools to assist with unit testing

In previous chapters, we learned about all the modern code-level tools that Angular now provides out of the box to make developing large frontend applications as seamless as possible. All of those tools were confined to the code that we write itself, helping to solve business requirement-related problems. But what about building our applications, developer experience, deployment, search engine optimizations, and so on? Now it is time to explore all of these topics. However, before we can actually deploy our beautiful HRMS application, we need to ensure it works properly and is reasonably maintainable (as in “will not break easily when developers add new changes”). And this is precisely what this chapter is about: unit testing.

8.1 Unit testing: The what and the why

Before we begin, let’s briefly discuss unit testing in more general terms, the reasons we need it, and how to achieve it.

8.1.1 Prerequisites

8.1.2 What is a unit test?

8.1.3 Why do we want unit tests?

8.2 Configuring a testing environment

8.2.1 Choosing a test runner

8.2.2 Setting up the test runner

8.2.3 Installing third-party tools

8.3 Running Angular unit tests

8.3.1 What do unit tests look like?

8.3.2 Providing mock dependencies

8.3.3 Testing components

8.3.4 Testing services

8.3.5 Testing signals

8.4 Powers of AI with Angular unit tests

8.4.1 Unit testing Angular applications with ChatGPT

8.4.2 Unit testing Angular applications with GitHub Copilot

8.5 The future of unit testing in Angular