liveBook · Manning

Test

As explained in Pro Angular 16

A unit test is an individual test that checks the behavior of an isolated software unit.

A unit test checks a small software unit in isolation. In Angular, components and directives depend on framework features and application infrastructure, so testing them requires recreating enough of that environment to exercise their behavior. section 29.0 section 29.4

Tests can check component logic, bindings, events, input and output properties, and generated HTML. Testing can also help clarify the behavior a feature needs, especially when it is difficult to implement or likely to cause bugs; it remains a tool to use selectively. section 29.0 section 29.6

A basic Jasmine test

A minimal assertion checks that the test setup works: `it("is working", () => expect(true).toBe(true));`. Changing the expectation to `false` makes the test fail and reports the failed assertion. section 29.2

Table 29.1. Putting Angular unit testing in context
Question
Answer
What is it?
Angular components and directives require special support for testing so that their interactions with other parts of the application infrastructure can be isolated and inspected.
Why is it useful?
Isolated unit tests can assess the basic logic provided by the class that implements a component or directive but do not capture the interactions with host elements, services, templates, and other important Angular features.
How is it used?
Angular provides a test bed that allows a realistic application environment to be created and then used to perform unit tests.
Are there any pitfalls or limitations?
Like much of Angular, the unit testing tools are complex. It can take some time and effort to get to the point where unit tests are easily written and run and you are sure that you have isolated the correct part of the application for testing.
Are there any alternatives?
As noted, you don’t have to unit test your projects. But if you do want to unit testing, then you will need to use the Angular features described in this chapter.

Use this overview to compare what Angular unit tests can isolate with the additional environment components and directives need. section 29.0

How Pro Angular 16 covers it

How deep it goes
Works from a basic Jasmine assertion through Angular component and directive tests covering bindings, events, properties, and rendered HTML.
Examples
Uses passing and failing Jasmine assertions, component tests, and an attribute directive applied through a test component.
What it assumes
Assumes an Angular project and does not introduce unit testing from first principles.
What you can do after
Apply unit tests via Jasmine, Karma, and Angular testing utilities to check components, directives, bindings, events, and properties.
How it connects
Connects unit testing with Angular components, directives, services, pipes, dependency injection, templates, and Jasmine.

Also explained in

Compare how 53 books explain test →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.