TestBed simulates the Angular application environment used by unit tests. Configure its testing module before creating components, then use the resulting ComponentFixture to inspect and exercise the component. section 29.4.1
A typical component test setup proceeds as follows:
- Import TestBed from @angular/core/testing and configure the testing module inside beforeEach. section 29.4.1
- Add the component under test to declarations, just as a component must be declared in a real Angular module. section 29.4.1
- Call createComponent with the component type and retain the returned ComponentFixture. section 29.4.1
- Provide injected collaborators through the testing module's providers, including replacement values such as a mock repository. section 29.4.1
Create and verify a component
import { TestBed, ComponentFixture } from "@angular/core/testing"; import { SimpleComponent } from "../simple.component"; describe("SimpleComponent", () => { let fixture: ComponentFixture<SimpleComponent>; let component: SimpleComponent; beforeEach(() => { TestBed.configureTestingModule({ declarations: [SimpleComponent] }); fixture = TestBed.createComponent(SimpleComponent); component = fixture.componentInstance; }); it("is defined", () => { expect(component).toBeDefined(); }); }); The test configures the module, creates the component, retrieves its class instance, and verifies that the instance exists.
Components with external templates require explicit asynchronous compilation through compileComponents; pair it with waitForAsync during setup. section 29.4.3

