Component TestBed
Custom test bed for testing Component.
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
}));
});
imports
β
Default : []
Imports template's dependencies for the described component and its children.
It is often used for non-standalone component, because standalone component embed its own importations.
Example :
const tb = componentTestBed(AppComponent, {
imports: [SharedModule, MaterialModule],
});
providers
β
Default : []
List of providers to be available during tests for the described component and its children.
Example :
const tb = componentTestBed(AppComponent, {
providers: [AppService, { provide: StoreService, useClass: MockStoreService }],
});
declarations
β
Default : []
List of components, directives and pipes to be used in the described non-standalone component template.
Example :
const tb = componentTestBed(NonStandaloneComponent, {
declarations: [ChildComponent, AppDirective, AppPipe],
});
schemas
β
Default : []
Allows specific elements and properties to be used in the template.
See Angular CUSTOM_ELEMENTS_SCHEMA and NO_ERRORS_SCHEMA.
Example :
const tb = componentTestBed(AppComponent, {
schemas: [CUSTOM_ELEMENTS_SCHEMA],
});
noopAnimations
β
Default : true
Disable Angular animation.
It provides provideNoopAnimation()
under the hood.
startDetectChanges
β
Default : true
Runs component fixture.detectChanges()
before each assertion.
Has no effect if noTemplate is true
.
noTemplate
β
Default : false
Useful when you only want to test the logic of the described component.
If enabled, no template will be rendered and no change detections will be performed.
httpTesting
β
Default : false
Enables HttpTestingTools.
verifyHttp
β
Default : true
When enabled, each assertion will end by HttpTestingController.verify()
.
Works only if httpTesting is true
, otherwise has no effect.
autoCompile
β
Default : true
Automatically compiles the custom test bed for each test.
checkCreate
β
Default : true
Automatically invokes the "should create" Angular test.
It checks if the provided described instance is truthy.
Assertion toolsβ
The tb function provides ComponentTools
.
describe('AppComponent', () => {
const tb = componentTestBed(AppComponent);
it('should ', tb((tools /* π here */) => {
// ... expectations
}));
});
π ComponentTools
extends BaseTools, RendererTools and HttpTestingTools.
component
β
The described component instance.
The instance is typed according to the passed component Type<T>
in componentTestBed(..)
.
Example :
it('should ', tb(({ component }) => {
expect(component.prop).toEqual('foo');
}));
Assertion optionsβ
For specific test, you enable/disable options that override the test bed options.
describe('AppComponent', () => {
const tb = componentTestBed(AppComponent);
it('should ', tb((tools) => {
// ... expectations
}, {} /* π here */));
});
startDetectChanges
β
Same as options startDetectChanges but only for the current assertion.
verifyHttp
β
Same as options verifyHttp but only for the current assertion.
ComponentTestBed
β
import(..)
β
Same as options imports but with chaining methods.
Example :
describe('AppComponent', () => {
const tb = componentTestBed(AppComponent)
.import(SharedModule)
.import([ThirdPartyModule, MaterialModule]);
});
provide(..)
β
Same as options providers but with chaining methods.
Example :
describe('AppComponent', () => {
const tb = componentTestBed(AppComponent)
.provide(AppService)
.provide([StoreService, { provide: MY_TOKEN, useValue: mockValue }]);
});
declare(..)
β
Same as options declarations but with chaining methods.
Example :
describe('AppComponent', () => {
const tb = componentTestBed(AppComponent)
.declare(ChildComponent)
.declare([HeaderComponent, AppPipe]);
});
inject(..)
β
Links an injected instance to a key and retrieve it into the enhanced tools by autocompletion.
describe('AppComponent', () => {
const tb = componentTestBed(AppComponent)
.inject('auth', AuthService);
it('should ', tb(({ injected: { auth } }) => {
// ... expectations
}));
});
setup(..)
β
Setups extra action using the enhanced tools.
Works only for beforeEach
and afterEach
.
describe('AppComponent', () => {
const tb = componentTestBed(AppComponent);
beforeEach(tb.setup(({ component }) => {
component.myInput = true;
}));
});
compile()
β
To be used when you need to do third party setups before compiling the custom test bed.
It has to be used into beforeEach(..)
and autoCompile must be set to false
.
describe('AppComponent', () => {
const tb = componentTestBed(AppComponent, { autoCompile: false });
beforeEach(async () => {
// ... third party setup
await tb.compile();
});
});
itShouldCreateComponent(..)
β
Create the "should create" for the described component.
To be used when there are no apparent or relevant tests to be performed.
describe('AppComponent', () => {
itShouldCreateComponent(AppComponent);
});