Component TestBed
Custom test bed for testing Component.
info
Works for standalone and non-standalone component.
Quick example
describe('AppComponent', () => {
const tb = componentTestBed(AppComponent) // π οΈ Create the test bed which is re-compiled for each test
.inject('prefs', Preferences); // ποΈ Link a key to an injection for all tests, see below π
it('should render title', tb(({ component, query }) => { // π Access enhanced tools for testing components
expect(component.title).toEqual('app-v17');
const span = query.findElement('.content span');
expect(span.textContent).toContain('app-v17 app is running!');
}));
it('should update preferences on click', tb(({ action, injected: { prefs } }) => { // π€― Retrieve injections by autocompletion
expect(prefs.approved).toBeFalse();
action.click('#my-button');
expect(prefs.approved).toBeTrue();
}));
});
componentTestBed(..)
β
Creates a specific test bed for component.
It returns a function to be used to wrap it
's callback and from which you access tools (check ComponentTools).
describe('AppComponent', () => {
const tb = componentTestBed(AppComponent);
it('should ', tb((tools) => { // π tb function used here
// ... expectations
}));
});
tb
function supports the jasmine DoneFn
and async/await notation.
describe('AppComponent', () => {
const tb = componentTestBed(AppComponent);
it('should ', tb(async (tools) => {
// ... async expectations
}));
it('should ', tb((tools, done) => {
// ... expectations
done();
}));
});
TestBed Optionsβ
describe('AppComponent', () => {
const tb = componentTestBed(AppComponent, {} /* π here */);
it('should ', tb(() => {
// ... expectations
}));
});