Angular-inspired red component layers with a green test checkmark

Recent versions of the Angular CLI use Vitest as the default unit test runner for new projects through the @angular/build:unit-test builder. The Angular team provides a migration path for existing Karma projects. The AnalogJS community builder predates the CLI integration and remains an option for teams with custom requirements. This guide explains both approaches and how to validate a migration.

How Angular runs Vitest

Angular's build system compiles components, templates and dependency injection metadata in ways that a plain Vite plugin cannot reproduce. Rather than asking developers to configure Vitest by hand, the CLI provides a builder that prepares the application build, wires the TestBed environment, and hands the compiled test files to Vitest. From your point of view, ng test still works; underneath, Vitest is doing the running, the watching and the reporting.

The builder's runner option accepts vitest or karma, and vitest is the default. New projects generated with recent CLI versions include vitest and jsdom as dependencies and already point the test target at the unit-test builder, so there is nothing to migrate.

Migrating an existing Karma project

Install the dependencies

Add vitest and a DOM emulation library such as jsdom as development dependencies. If your tests need a real browser, the builder can run them in Chromium through Vitest's browser mode by setting the browsers option.

Change the builder

In angular.json, locate the test target of your project and replace the builder value with @angular/build:unit-test. The builder uses tsconfig.spec.json and the development build target by default; set tsConfig and buildTarget explicitly only when your project needs different values. One important difference from the Karma builder is that build options like polyfills, assets or styles are no longer configured inside the test target; they come from the referenced build target instead.

Run the migration schematic

The Angular team ships an automated refactoring schematic that converts Jasmine-specific calls to their Vitest equivalents. Run it after the builder change, then review the diff. Most spies, matchers and lifecycle hooks map cleanly; custom matchers and some asynchronous patterns may need manual attention.

Run and fix

Start ng test. Failures at this stage are usually about environment details: a missing DOM library, a test that relied on a Karma-only global, or timing differences between the two runners. Fix them one spec file at a time rather than trying to understand everything at once.

Configuring the builder

The unit-test builder exposes the options most teams need directly in angular.json: include and exclude patterns, reporters such as default, verbose, dots, json, junit, tap and html, coverage settings, a watch flag, a browsers list for real-browser execution and a flag to enable the Vitest UI. For advanced needs, the runnerConfig option can point to a vitest-base.config file or be set to true so the builder searches for one. When no such file is found, the builder uses its own in-memory configuration and deliberately ignores a stray vitest.config.ts to keep runs predictable.

The AnalogJS vitest-angular builder

Before Angular offered official support, the AnalogJS project created @analogjs/vitest-angular, a standalone builder that pairs a Vite plugin for Angular with Vitest. It is still maintained and supports features such as snapshot testing, zoneless change detection and a browser mode flag. Setup involves installing the package, adding a vite.config.mts with the Angular plugin and a test block, creating a test-setup file that initialises the TestBed, and pointing the angular.json test target at the @analogjs/vitest-angular:test builder.

Choose the AnalogJS builder when you already use AnalogJS, need an option the official builder does not expose yet, or are on an Angular version that predates the official unit-test builder. Choose the official builder for new projects and for the smoothest upgrade path with future CLI versions.

Writing Angular tests under Vitest

TestBed works exactly as before. Import describe, it, expect and vi from vitest, or enable globals in the configuration if you want the implicit style. Replace jasmine.createSpy with vi.fn, spyOn with vi.spyOn, and Jasmine clock calls with vi.useFakeTimers. Asynchronous tests can use async and await directly, which many teams find clearer than the older done callback pattern.

Running tests in a real browser

For components that depend on layout, real events or browser APIs that jsdom emulates poorly, set the browsers option to a list such as chromium. The builder uses Vitest's browser mode with a Playwright provider to execute the specs inside an actual browser while keeping the same ng test workflow.

Troubleshooting Angular-specific issues

If a test cannot find a component template, confirm that the test target references a build target that includes the right assets and styles. If zone.js errors appear, check whether the project is zoneless and align the test setup accordingly. If coverage reports look incomplete, set the coverage include patterns to your src folder so generated files are excluded. Where the builder's option set is too narrow, move to a runner config file through runnerConfig instead of patching node_modules.

Comparing Karma and Vitest in practice

Karma launched a real browser for every run and reported results back through a socket, which made it accurate but slow to start and awkward to debug. The Vitest-based builder runs most specs in a Node environment with jsdom, starts in a fraction of the time, reruns only affected files in watch mode and offers the UI dashboard and HTML reporter. When you need an actual browser, the browsers option brings it back on demand rather than for every test.

Coverage and reporters in Angular projects

The builder exposes coverage options directly, so a command with the coverage flag produces reports without extra configuration. Reporter names mirror Vitest's own list, and junit output drops straight into most CI dashboards. For the HTML report or coverage thresholds, use a runner config file so the settings live with the rest of the project configuration.

Conclusion

Angular now treats Vitest as its default unit-test runner through the @angular/build:unit-test builder, with a documented path from Karma that consists of installing vitest and jsdom, switching the builder in angular.json and running the Jasmine-to-Vitest schematic. The AnalogJS vitest-angular builder remains a solid alternative for projects that need it. Either way, you keep ng test, TestBed and your existing specs while gaining Vitest's speed, watch mode and tooling.