liveBook · Manning

TestBed

As explained in Pro Angular 16

TestBed is an Angular testing utility that configures a simulated application environment and creates objects for component tests.

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

How Pro Angular 16 covers it

How deep it goes
Explains module setup, component instantiation, fixtures, dependencies, and external-template compilation without covering broader testing internals.
Examples
Uses SimpleComponent tests, a mock repository, ComponentFixture inspection, and an external HTML template.
What it assumes
Assumes Angular component testing and Jasmine basics while teaching TestBed configuration from module declarations onward.
What you can do after
Build component tests from scratch with TestBed, fixtures, providers, and asynchronous template compilation.
How it connects
Connects Angular components, dependency injection, ComponentFixture, Jasmine, DOM testing, and asynchronous testing.

Also explained in

Compare how 2 books explain TestBed →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.