API
Angular Testing Library
re-exports everything from DOM Testing Library
as
well as these methods:
Some of the DOM Testing Library
re-exports are patched to work easier with
Angular:
- The events on
fireEvent
automatically invoke a change detection cycle after the event has been fired - The
findBy
queries automatically invoke a change detection cycle before the query is invoked function - The
waitFor
functions automatically invoke a change detection cycle before invoking the callback function
render
Component RenderOptions
componentProperties
An object to set @Input
and @Output
properties of the component.
default : {}
example:
componentProviders
A collection of providers to inject dependencies of the component.
For more info see the Angular docs.
default : []
example:
declarations
A collection of providers needed to render the component via Dependency Injection, for example, injectable services or tokens.
For more info see the Angular docs.
default : []
example:
detectChanges
Will call detectChanges
when the component is compiled
default : true
example:
excludeComponentDeclaration
Exclude the component to be automatically be added as a declaration. This is needed when the component is declared in an imported module.
default : false
example:
imports
A collection of imports needed to render the component, for example, shared
modules. Adds NoopAnimationsModule
by default if BrowserAnimationsModule
isn't added to the collection
For more info see the Angular docs.
default : [NoopAnimationsModule]
example:
queries
Queries to bind. Overrides the default set from DOM Testing Library unless merged.
default : undefined
example:
routes
The route configuration to set up the router service via
RouterTestingModule.withRoutes
. For more info see the
Angular Routes docs.
default : []
example:
schemas
A collection of schemas needed to render the component. Allowed values are
NO_ERRORS_SCHEMA
and CUSTOM_ELEMENTS_SCHEMA
.
For more info see the Angular docs.
default : []
example:
removeAngularAttributes
Removes the Angular attributes (ng-version, and root-id) from the fixture.
default : false
example:
Directive RenderOptions
To test a directive, the render API is a bit different. The API has the same options as the Component RenderOptions, but has more options:
template
The template to render the directive.
example:
wrapper
An Angular component to wrap the directive in.
default: WrapperComponent
, an empty component that strips the
ng-version
attribute.
example:
RenderResult
container
The containing DOM node of your rendered Angular Component. This is a regular
DOM node, so you can call container.querySelector
etc. to inspect the
children.
debug
Prints out the component's DOM with syntax highlighting. Accepts an optional parameter, to print out a specific DOM node.
rerender
Re-render the same component with different props. Will call detectChanges
after props has been updated.
detectChanges
Trigger a change detection cycle for the component.
For more info see the Angular docs.
debugElement
The Angular DebugElement
of the component.
For more info see the Angular docs.
fixture
The Angular ComponentFixture
of the component.
For more info see the Angular docs.
๐จ If you find yourself using
fixture
to access the component's internal values you should reconsider! This probable means, you're testing implementation details.
navigate
Accepts a DOM element or a path as parameter. If an element is passed,
navigate
will navigate to the href
value of the element. If a path is
passed, navigate
will navigate to the path.
...queries
The most important feature of render
is that the queries from
DOM Testing Library are
automatically returned with their first argument bound to the component under
test.
See Queries for a complete list.
example: