You want to write maintainable tests for your Angular components. As a part of
this goal, you want your tests to avoid including implementation details of your
components and rather focus on making your tests give you the confidence for
which they are intended. As part of this, you want your testbase to be
maintainable in the long run so refactors of your components (changes to
implementation but not functionality) don't break your tests and slow you and
your team down.
This solution
The @testing-library/angular is a very lightweight solution for
testing Angular components. It provides light utility functions on top of Angular
and @testing-library/dom, in a way that encourages better testing practices. Its
primary guiding principle is:
This module is distributed via npm which is bundled with node and
should be installed as one of your project's devDependencies.
Starting from ATL version 17, you also need to install @testing-library/dom:
Or, you can use the ng add command.
This sets up your project to use Angular Testing Library, which also includes the installation of @testing-library/dom.
We try to only expose methods and utilities that encourage you to write tests
that closely resemble how your Angular components are used.
Utilities are included in this project based on the following guiding
principles:
If it relates to rendering components, it deals with DOM nodes rather than
component instances, nor should it encourage dealing with component
instances.
It should be generally useful for testing individual Angular components or
full Angular applications.
Utility implementations and APIs should be simple and flexible.
At the end of the day, what we want is for this library to be pretty
light-weight, simple, and understandable.