ngx-testing-library
Lightweight utility functions to test Angular components.
Table of Contents
Installation
Install ngx-testing-library
from npm and add it your devDependencies
:
npm install ngx-testing-library --save-dev
Why
- test your UI components the way your users are using it
- making your tests resilient to implementation changes
What
ngx-testing-library is an Angular adapter around dom-testing-library, which provides lightweight utility functions to test UI components. Your tests will work with actual DOM nodes.
How
createComponent
This library only consists of one function, createComponent
which is used to setup the Angular TestBed
and creates the component fixture.
This method can be used in two ways:
Based on a template:
; createComponent'<my-component [prop]="1"></my-component>', options;
Based on a component type:
; createComponent , options,;
The second parameter in createComponent
is the options
parameter, which looks like this:
detectChanges
: runs detectChanges
on the fixture
declarations
: passed to the TestBed
providers
: passed to the TestBed
imports
: passed to the TestBed
schemas
: passed to the TestBed
The createComponent
function returns an object consisting all of the query functions from dom-testing-library, all the event functions exposed from fireEvent
, and adds the following properties:
Every event runs
detectChanges
on the fixture.
container: HTMLElement
The DOM node containing the Angular component.
All of the dom-testing-library query functions are binded to this container.
debug() => void
Prints out the container.
fixture: any
The Angular fixture.
getFromTestBed(token: any, notFoundValue?: any) => any
Calls the the Angular TestBed.get
function.
Usage
You can find some examples in the tests folder.
Here is how the "default" specifications can be written with ngx-testing-library
.
Before:
;; describe'AppComponent',;
After:
;; it`should have as title 'my-awesome-app'`, ; it`should render title in a h1 tag`, ;
LICENSE
MIT