Unit testing of components and services
In this section, we will introduce Angular testing utilities and show how to test components and services. Testing of directives, pipes, router, and so on, will not be explained due to the limited size of the book.
Angular testing utilities
As you could see in the previous section, Angular comes with a testing library, @angular/core/testing
, which offers the TestBed
helper class and many other utilities. TestBed
helps us to set up dependencies for tests--modules, components, providers, and so on. You can call TestBed.configureTestingModule()
and pass the same metadata configuration object as used with @NgModule
. The configureTestingModule()
function should be called within a beforeEach()
setup function.
Another useful function from the testing library is called inject()
. It allows you to inject the specified objects into the tests. The following code snippet provides an example:
describe('MyService', () => { let service; beforeEach(() => TestBed...