Angular 22 Vitest Testing
Use Vitest for fast feedback loops, deterministic assertions, and lightweight component and service tests. Keep test setup minimal and focus on observable behavior.
Core Rules
- Prefer Vitest for unit tests that do not need a full browser runtime.
- Keep test files colocated with the code they verify when that improves discoverability.
- Mock only external boundaries such as HTTP, storage, timers, and browser globals.
- Use
describe, it, expect, vi, and lifecycle hooks consistently.
- Favor stable assertions over snapshot-heavy workflows unless snapshots are truly valuable.
- Use
vitest run for one-shot CI execution and the watch runner for local iteration.
Example
import { afterEach, describe, expect, it, vi } from 'vitest';
describe('formatPrice', () => {
afterEach(() => {
vi.restoreAllMocks();
});
it('formats currency deterministically', () => {
const formatter = vi.fn((value: number) => `USD ${value.toFixed(2)}`);
expect(formatter(12.5)).toBe('USD 12.50');
expect(formatter).toHaveBeenCalledWith(12.5);
});
});
Async and Timers
import { describe, expect, it, vi } from 'vitest';
describe('delayed work', () => {
it('uses fake timers predictably', async () => {
vi.useFakeTimers();
const result = new Promise<string>((resolve) => {
setTimeout(() => resolve('ready'), 1000);
});
await vi.advanceTimersByTimeAsync(1000);
await expect(result).resolves.toBe('ready');
vi.useRealTimers();
});
});
Configuration Guidance
- Vitest reads
vite.config.* by default, so reuse existing Vite configuration when possible.
- Use a dedicated
vitest.config.* when test-specific settings need to stay separate.
- Keep setup files small and predictable.
- Use browser mode only when a test depends on real browser APIs or visual behavior.
Testing Style
- Prefer explicit arrange, act, assert structure.
- Keep assertions close to the behavior that matters.
- Use
vi.mock() sparingly and reset mocks between tests.
- Use snapshots only when the output is stable and structural change matters.
- Use
test.each or describe.each when coverage varies only by input data.
Best Practices
- Reset spies and mocks between tests to avoid shared state.
- Keep async tests explicit by awaiting the observable result.
- Prefer fake timers for predictable time-based logic.
- Use component tests only for rendered behavior that matters to users.
Review Checklist
- The test name says what changes if the assertion fails.
- The setup is minimal and local.
- The test runner choice matches the test's risk and scope.
- Mocks do not hide important integration behavior.
1---2name: ng22-vitest3description: Guides Angular 22 teams to use Vitest for fast, deterministic unit and component testing.4---5
6# Angular 22 Vitest Testing
7
8Use Vitest for fast feedback loops, deterministic assertions, and lightweight component and service tests. Keep test setup minimal and focus on observable behavior.
9
10## Core Rules
11
121. Prefer Vitest for unit tests that do not need a full browser runtime.
132. Keep test files colocated with the code they verify when that improves discoverability.
143. Mock only external boundaries such as HTTP, storage, timers, and browser globals.
154. Use `describe`, `it`, `expect`, `vi`, and lifecycle hooks consistently.
165. Favor stable assertions over snapshot-heavy workflows unless snapshots are truly valuable.
176. Use `vitest run` for one-shot CI execution and the watch runner for local iteration.
18
19## Example
20
21```typescript
22import { afterEach, describe, expect, it, vi } from 'vitest';
23
24describe('formatPrice', () => {
25 afterEach(() => {
26 vi.restoreAllMocks();
27 });
28
29 it('formats currency deterministically', () => {
30 const formatter = vi.fn((value: number) => `USD ${value.toFixed(2)}`);
31
32 expect(formatter(12.5)).toBe('USD 12.50');
33 expect(formatter).toHaveBeenCalledWith(12.5);
34 });
35});
36```
37
38## Async and Timers
39
40```typescript
41import { describe, expect, it, vi } from 'vitest';
42
43describe('delayed work', () => {
44 it('uses fake timers predictably', async () => {
45 vi.useFakeTimers();
46
47 const result = new Promise<string>((resolve) => {
48 setTimeout(() => resolve('ready'), 1000);
49 });
50
51 await vi.advanceTimersByTimeAsync(1000);
52 await expect(result).resolves.toBe('ready');
53
54 vi.useRealTimers();
55 });
56});
57```
58
59## Configuration Guidance
60
61- Vitest reads `vite.config.*` by default, so reuse existing Vite configuration when possible.
62- Use a dedicated `vitest.config.*` when test-specific settings need to stay separate.
63- Keep setup files small and predictable.
64- Use browser mode only when a test depends on real browser APIs or visual behavior.
65
66## Testing Style
67
68- Prefer explicit arrange, act, assert structure.
69- Keep assertions close to the behavior that matters.
70- Use `vi.mock()` sparingly and reset mocks between tests.
71- Use snapshots only when the output is stable and structural change matters.
72- Use `test.each` or `describe.each` when coverage varies only by input data.
73
74## Best Practices
75
76- Reset spies and mocks between tests to avoid shared state.
77- Keep async tests explicit by awaiting the observable result.
78- Prefer fake timers for predictable time-based logic.
79- Use component tests only for rendered behavior that matters to users.
80
81## Review Checklist
82
83- The test name says what changes if the assertion fails.
84- The setup is minimal and local.
85- The test runner choice matches the test's risk and scope.
86- Mocks do not hide important integration behavior.