TestBed and Component Fixtures
Configure and create components in tests.
Why TestBed Exists
Angular components depend on the framework: dependency injection, change detection, and the template compiler. To test them in isolation you need a tiny Angular environment. TestBed is Angular's primary testing utility that builds this environment for each test.
It configures a temporary NgModule, compiles your component, and gives you a handle to interact with it.
configureTestingModule
TestBed.configureTestingModule() declares what your test needs: the component under test, its providers, and any imports. For standalone components you put the component itself in imports.
import { TestBed } from '@angular/core/testing';
import { CounterComponent } from './counter.component';
beforeEach(() => {
TestBed.configureTestingModule({
imports: [CounterComponent],
});
});All lessons in this course
- TestBed and Component Fixtures
- Testing Inputs, Outputs, and DOM
- Mocking Services and Dependencies
- Testing Signals and Async Code