← Back to Angular, page 9

angular-0176 · Medium mid

What are the options for unit testing an Angular app (Karma, Jest, Vitest), and what is the direction the Angular team has taken?

Karma with Jasmine ran tests in a real browser and was the long-time default, but the Karma project is deprecated. Jest is popular through community presets, and runs in Node with jsdom. Vitest is the direction the Angular team chose: ng test uses Vitest by default for new projects in Angular 21, and Karma stays supported. Experimental Jest and Web Test Runner support is deprecated.

Full answer

Unit tests need a runner (finds and runs tests, reports results), an assertion library, and an environment (a browser or a simulated DOM).

Karma and Jasmine: the classic Angular setup. Karma launches real Chrome, and Jasmine provides describe, it, expect and spies. Benefits: real browser behaviour (layout, real events). Drawbacks: slower start, heavier setup, and the Karma project was deprecated in 2023, so the Angular team looked for a replacement.

Jest: a very popular runner from the React world, with fast start, snapshots, and good mocking. It runs in Node with jsdom, so there is no real browser. In Angular it is used through community packages (jest-preset-angular, the Nx Jest plugin). The CLI also had an experimental Jest builder, which is deprecated.

Vitest: a modern runner built on Vite, with a Jest-like API, watch mode, and an option to run in a real browser (Playwright or WebdriverIO providers). The Angular team announced that ng test runs Vitest by default for new projects in Angular 21, and that Vitest support is stable. New projects include vitest and jsdom. Existing Karma projects keep working, and there is an experimental schematic (refactor-jasmine-vitest) that converts Jasmine style tests.

Key differences in daily work:

  • Environment: jsdom (Node, fast, not a real browser) versus a real browser (Karma, or Vitest browser mode).
  • API: jasmine.createSpy and spyOn versus vi.fn() and vi.spyOn. Timers: fakeAsync works with zone.js, and vi.useFakeTimers() works with Vitest, so tests for zoneless apps are different.
  • Speed: Node based runners start faster.
  • Migration cost: mostly mock syntax, timers and configuration.

How to answer in an interview: say what your project uses and why, name the direction (Vitest, Karma still supported), mention that real browser tests (Playwright component tests, Vitest browser mode, or e2e) cover what jsdom cannot, and show that you would migrate gradually. Keep tests written against behaviour (rendered output, harnesses), because those survive a runner change.

typescript
// Same behaviour test: only the spy and the test API differ between the two runners.
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-counter',
  template: `<button (click)="inc()">Count {{ count() }}</button>`,
})
export class CounterComponent {
  readonly count = signal(0);
  inc(): void {
    this.count.update((c) => c + 1);
  }
}

// Karma and Jasmine:
//   const log = jasmine.createSpy('log');
//   expect(log).toHaveBeenCalledWith('ok');
//
// Vitest:
//   const log = vi.fn();
//   expect(log).toHaveBeenCalledWith('ok');
//
// The component test body is the same in both:
//   const fixture = TestBed.createComponent(CounterComponent);
//   fixture.nativeElement.querySelector('button').click();
//   fixture.detectChanges();
//   expect(fixture.nativeElement.textContent).toContain('Count 1');

This is based on the Angular 21 announcement and the Angular testing guides (checked on angular.dev in December 2026). The code was only type-checked. The Vitest form was not run here, because the test project uses Karma.

Key points

  • Karma (real browser) is the long-time default, but the Karma project is deprecated.
  • Jest runs in Node with jsdom and is used through community presets. The CLI's experimental Jest support is deprecated.
  • Vitest is the Angular team's default for new projects in Angular 21. Karma remains supported.
  • Behaviour based tests and harnesses survive a runner change. Use real browser tests where jsdom is not enough.

Common mistakes

  • Saying Karma is removed. It is still supported.
  • Assuming jsdom behaves like a real browser for layout.
  • Migrating all tests at once without a plan.

Follow-ups

  • What does a migration from Jasmine spies to Vitest involve?
  • Which tests need a real browser?
  • How do fake timers differ without zone.js?

Tips

  • Be exact: Vitest is the default for new projects, and existing Karma projects still work.
  • Mention the jsdom limit.
  • Suggest gradual migration.

Evaluation rubric

  • Compares the three options accurately.
  • States the Angular team direction correctly.
  • Talks about migration and test design.