Sub topic 15
Running Unit Tests with ng test
This chapter assumes you have already built the Counter component from the signals chapter. You should also be able to read basic TypeScript and run CLI commands from the workspace root. A unit test checks one small piece of code, such as a component or a service, separately from the rest of the app. The Angular CLI generates a test file for each component you create and gives you one command to run all of them.
Where tests live
When you run ng generate component home, the CLI creates a home.spec.ts file next to the component. Any file ending in .spec.ts counts as a test. ng test finds every spec file, compiles it with your app's TypeScript settings, and runs it.
The test runner depends on your Angular version. Projects created with Angular 21 or later use Vitest by default. Older projects use Karma with Jasmine, which runs the tests in a real browser. Both runners support the describe, it, beforeEach and expect style used below, so the test code is the same either way.
ng generate component home
ng test
ng test --watch=falseBy default, ng test starts in watch mode and runs the tests again whenever you save a file. Use --watch=false for a single run, which is what you want in CI.
How a test run works
Testing the Counter component
TestBed is Angular's testing utility. It sets up a small Angular environment just for the test. Because Counter is a standalone component, you pass it in imports.
// src/app/counter.spec.ts
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Counter } from './counter';
describe('Counter', () => {
let fixture: ComponentFixture<Counter>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [Counter],
}).compileComponents();
fixture = TestBed.createComponent(Counter);
fixture.detectChanges();
});
it('starts at zero', () => {
expect(fixture.componentInstance.count()).toBe(0);
});
it('increments when the button is clicked', () => {
const button: HTMLButtonElement =
fixture.nativeElement.querySelector('button');
button.click();
fixture.detectChanges();
expect(button.textContent).toContain('Count: 1');
expect(fixture.componentInstance.double()).toBe(2);
});
});The fixture wraps the component instance (componentInstance) and its rendered DOM (nativeElement). detectChanges() tells Angular to update the DOM so it shows the current state.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| "is not a known element" or a component won't compile | The component or one of its children is missing from `imports` | Add the standalone component to `imports` in `configureTestingModule` |
| The DOM still shows old values | Change detection did not run after the state changed | Call `fixture.detectChanges()` or `await fixture.whenStable()` |
| `NullInjectorError: No provider for X` | The service has no provider in the test environment | Use `providedIn: 'root'` or add the provider to `providers` |
| Tests hang in CI | Watch mode is still on | Run `ng test --watch=false` |
Running the full check
Before you ship, run the tests once and then build:
ng test --watch=false
ng buildExercise
You add a test that sets fixture.componentInstance.count.set(5) and then checks that the paragraph contains "Double: 10", but the test fails and the paragraph still shows "Double: 0". Why, and what one line fixes it?
Show answer
Setting the signal changes the component's state, but the DOM is not updated until change detection runs. Call fixture.detectChanges(); after count.set(5) and before reading the paragraph text.
Exercise
Which command runs the test suite once and then exits, so it works in a CI pipeline?
Show answer
ng test --watch=false