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




















































