Sub topic 1
How Vitest fits into an Angular project
Vitest is a test runner. It finds your test files, runs them, and reports which ones passed or failed. Angular does not ship its own runner. This chapter explains which part of your project does what, so the later chapters on writing tests make sense.
Prerequisites
This chapter assumes you already know:
- Basic TypeScript.
- Angular standalone components, templates, and dependency injection.
- Angular signals and the
input()andoutput()functions. - How to use npm and the Angular CLI.
The three layers
Testing an Angular component involves three separate tools. Each answers a different question.
| Layer | Tool | Question it answers |
|---|---|---|
| Command and build | Angular CLI, `@angular/build:unit-test` builder | How are the spec files compiled and started? |
| Runner and assertions | Vitest | How are tests found, run, and checked? |
| Angular testing API | `TestBed` and `ComponentFixture` | How is a component created and rendered in a test? |
Vitest knows nothing about Angular. It runs functions and checks values. TestBed knows about Angular but has no idea which runner is calling it. The CLI builder connects the two. It compiles your TypeScript and templates the way Angular needs, then hands the compiled specs to Vitest.
Where the runner is chosen
The runner is a setting on the test target in angular.json:
{
"test": {
"builder": "@angular/build:unit-test",
"options": {
"runner": "vitest"
}
}
}The builder line says the CLI's unit-test builder drives the run. The runner option says Vitest does the running. When you type ng test, the CLI reads this target. The next chapter walks through creating and running it.
By default the specs run in a simulated browser environment that provides document and other DOM objects. This is why TestBed can render a component without a browser window. The last chapters cover running in a real browser instead.
What a spec file imports
Each spec file imports from two places. The runner API comes from vitest, and the Angular API comes from @angular/core/testing:
import { TestBed } from '@angular/core/testing';
import { describe, it, expect } from 'vitest';This split is the main thing to remember. describe, it, expect, and vi belong to Vitest. TestBed and ComponentFixture belong to Angular. Mixing up where something comes from is the source of many early errors.
Common errors
- Using Jasmine helpers. Older Angular projects used Jasmine, which has
jasmine.createSpyObjand a globalspyOn. These do not exist under Vitest. Usevi.fn()andvi.spyOn()instead. - Forgetting the import. If your editor or the run reports that
describeorexpectis not defined, add the import fromvitest. - Running Vitest directly. Calling
npx vitestskips the Angular builder. Component templates are then not compiled the way Angular expects, and tests fail in confusing ways. Always start the run withng test. - Expecting a real browser. The default environment simulates the DOM. Layout values such as element sizes will not be meaningful there.
What comes next
The following chapters build on this split. You will set up and run ng test, learn the shape of a Vitest test, and then use TestBed to render the counter and user-list components from the examples.
Exercise
A teammate's spec calls jasmine.createSpyObj('UserService', ['list']) and fails under ng test with the Vitest runner. Which part of the stack owns the replacement, and what should they use?
Show answer
Vitest is the runner and owns the spy and mock helpers, so Jasmine's createSpyObj is not available. They should import vi from vitest and build the mock with vi.fn(), for example { list: vi.fn() }. They can then supply it through a TestBed provider.