Skip to main content

Vitest

v1.0 (2023)

Jasmine-style syntax (via Angular's TestBed) is fully compatible with Vitest in Angular 21.

Vitest emulates Jasmine/Jest APIs by default, so describe, it, expect, beforeEach, and TestBed work unchanged—no native Vitest syntax (like vi.fn or test) required unless you opt in.

Installation

Step 1

yarn add -D nx@latest @nx/devkit@latest @nx/vite@latest

Step 2

yarn ng g @analogjs/platform:setup-vitest

In a modern Angular 21 zoneless architecture, the distinction between mocking and faking is defined by how they interact with the new Signal-based reactivity and the removal of fakeAsync. Mocking with Vitest (e.g., vi.spyOn) is used to intercept and verify the execution flow of your Signals and Actions, ensuring that when a state changes, the correct side effects---like an API call---are triggered. Faking, however, is increasingly preferred for zoneless testing because creating a simplified, synchronous implementation of a service (like an InMemoryStorage using a native Signal) avoids the pitfalls of manual change detection. By using a fake, you bypass the need for complex await fixture.whenStable() calls or vi.useFakeTimers(), allowing your tests to run as pure, synchronous logic that mirrors how Angular 21 naturally handles updates without the overhead of Zone.js.