Sub topic 1
How Vitest fits into an Angular project
Before you write any component test, it helps to know which tool does which job. Angular tests involve three layers: a test runner, Angular's own testing utilities, and a simulated or real browser. This chapter explains how Vitest fits with the other two. It stops before the setup details, which the next chapter covers.
Prerequisites
This chapter assumes you already have:
- Basic TypeScript, including modules, generics, and async functions.
- Angular standalone components, templates, and dependency injection.
- Familiarity with signals and the
input()andoutput()functions. - Comfort with npm and the Angular CLI (
ngcommands).
You don't need any prior testing experience with Vitest. If you have used Jasmine and Karma, the comparison section below shows what carries over and what changes.
Three layers, three jobs
A component test touches three separate things.
- The test runner finds your test files, executes them, reports failures, and provides the vocabulary of tests:
describe,it,expect, and mock functions. Vitest is the runner. - Angular's testing utilities build and drive components.
TestBedcreates a small Angular environment for one test, andComponentFixturewraps the component you created. These come from@angular/core/testingand are the same whichever runner you choose. - The environment supplies a DOM (
document,HTMLElement, events) so templates can render. By default this is an emulated DOM running inside Node.js. A real browser is an option that a later chapter covers.
Vitest knows nothing about components. Angular's utilities know nothing about test runners. The Angular CLI connects them: it compiles your code and specs, starts Vitest with the right settings, and initializes the Angular testing environment before your tests run.
Read the diagram top to bottom. Everything above "Start Vitest runner" is Angular CLI work. Everything below it is your test code interacting with Angular and the DOM.
The test target in angular.json
The link between the CLI and Vitest is one entry in the test target of your project in angular.json:
{
"test": {
"builder": "@angular/build:unit-test",
"options": {
"runner": "vitest"
}
}
}Two fields matter here.
buildernames the Angular CLI builder that handlesng test. The@angular/build:unit-testbuilder is a general unit-test builder. It is not specific to Vitest.runnerselects which test runner the builder starts. With"vitest", your specs run through Vitest. Other values exist for other runners, such as Karma for projects that still use it.
This is why you rarely write a vitest.config.ts for an Angular project. The builder generates the configuration it needs from the target's options. Changing behavior, such as which files are included or whether coverage is collected, happens through builder options, not a separate Vitest config file. Later chapters introduce those options as you need them.
What running ng test does
When you run ng test, the following happens in order:
- The builder reads the
testtarget and findsrunner: "vitest". - It compiles your code and the spec files (by default, files ending in
.spec.ts) through the Angular build. - It starts Vitest with an emulated DOM environment, unless you configured a browser.
- It sets up the Angular testing environment once, so
TestBedis ready when your first test runs. - Vitest runs each spec file and reports passes and failures in the terminal.
- In watch mode, Vitest re-runs the affected specs when you save a file.
The important consequence of step 3 is that your tests run in a Node.js process, not in Chrome. Rendering is fast, and you can run tests in CI without a browser installed. The cost is that an emulated DOM is an approximation. It does not do real layout, so values such as element sizes are typically zero, and some browser features may be missing or behave differently. For tests that depend on real browser behavior, the browser option exists, and a later chapter covers it.
How Vitest compares with Karma and Jasmine
Many existing Angular projects use Jasmine as the assertion library and Karma as the runner that launches a browser. Vitest replaces both. The table shows where the concepts map.
| Concern | Karma and Jasmine | Vitest |
|---|---|---|
| Test structure | `describe`, `it` | `describe`, `it` (same names) |
| Assertions | `expect(x).toBe(y)` | `expect(x).toBe(y)` (similar matchers) |
| Mock function | `jasmine.createSpy()` | `vi.fn()` |
| Spy on a method | `spyOn(obj, 'method')` | `vi.spyOn(obj, 'method')` |
| Fake time | `jasmine.clock()` | `vi.useFakeTimers()` |
| Where tests run | A real browser launched by Karma | Node.js with an emulated DOM, or a real browser if configured |
| Angular utilities | `TestBed`, `ComponentFixture` | `TestBed`, `ComponentFixture` (unchanged) |
The names describe, it, and expect look the same, so most simple specs port with small edits. The differences show up in mocking and timing. Each Jasmine-specific call needs a Vitest equivalent: jasmine.createSpy becomes vi.fn, and jasmine.clock becomes vi.useFakeTimers.
A first look at a Vitest-style test
You will learn the structure of a test in the next chapters. For now, note where each import comes from:
import { TestBed } from '@angular/core/testing';
import { describe, it, expect, vi } from 'vitest';TestBed comes from Angular. describe, it, expect, and vi come from Vitest. In this tutorial you import the Vitest names explicitly, so each spec file shows exactly where its tools come from. Keeping that split in mind makes errors easier to diagnose: a problem with creating or rendering a component is an Angular question, and a problem with matchers, mocks, or timers is a Vitest question.
Common errors at this stage
These problems appear most often when a project first adopts Vitest.
ng teststill launches Karma. Therunneroption is missing or set to another value, or the target uses an older builder. Check thetesttarget inangular.json.jasmine is not definedorspyOn is not defined. A spec uses Jasmine globals. Replace them withvi.fn()andvi.spyOn(), and importvifromvitest.describe is not defined. A spec uses Vitest names without importing them. Add the import fromvitest.- A test depends on layout or sizes. An element's measured width or height is zero in an emulated DOM. Test the logic instead, or run that test in a real browser.
- Zone-based helpers behave unexpectedly. Helpers such as
fakeAsyncdepend on Zone.js, and newer Angular projects may not use Zone.js at all. Prefer Vitest's fake timers, which a later chapter covers.
What comes next
The next chapter walks through setting up and running tests with ng test, including the options you will use day to day. After that, you will learn the anatomy of a Vitest test, then move to TestBed, fixtures, and the DOM. Each later chapter adds one layer to the model you saw here: the CLI starts Vitest, Vitest runs your specs, and your specs use Angular's utilities to render components.
Exercise
A teammate says, "I'll import describe and expect from @angular/core/testing, since Angular owns testing." Which package supplies those names in this tutorial's setup, which package supplies TestBed, and which angular.json field decides that Vitest is the runner?
Show answer
describe, it, expect, and vi come from vitest. TestBed comes from @angular/core/testing. The runner option, set to "vitest" in the options of the test target that uses the @angular/build:unit-test builder, selects Vitest.
Exercise
A spec fails with "spyOn is not defined" after you move a project from Karma to Vitest. What is the likely cause, and what is the fix?
Show answer
The spec uses Jasmine's global spyOn, which Vitest does not provide. Import vi from vitest and use vi.spyOn(obj, 'method') instead. For standalone mock functions, replace jasmine.createSpy() with vi.fn().