Test runners: from Karma to Vitest
Angular's shift away from Karma, through a Jest-era waypoint, to Vitest as the current default application test builder.
Test runners: from Karma to Vitest
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Karma ran tests inside a real, visible (or headless) browser — accurate, but slow to start and heavier than most unit tests actually need.
- Jest became a popular community-driven replacement for a stretch of Angular's history, running tests in Node with a simulated DOM instead of a real browser, trading some fidelity for speed.
- Angular's current default application builder uses Vitest, which — like Jest — runs outside a real browser by default, with the option of a genuine browser-mode run for tests that specifically need one.
A test runner is infrastructure, separate from what TestBed does
Everything the last two lessons covered — TestBed, fixtures, assertions — is
about what a test does. Something else entirely has to actually execute that
test file, report pass/fail, and provide the JavaScript environment the test
runs inside. That’s the test runner’s job, and it’s changed more than once
across Angular’s history — worth understanding both because you’ll meet
different ones in different codebases, and because the current default is
genuinely different from what long-standing tutorials still describe.
Karma: real browser fidelity, at a real cost
For most of Angular’s history, the default was Karma — a runner that launches an actual browser (headless Chrome, typically) and runs your tests inside it. The appeal is obvious: a test running in a genuine browser environment is about as faithful a stand-in for production behavior as a test can get. The cost is just as real: spinning up a browser instance has meaningful startup overhead, and running a large test suite that way, repeatedly, during active development, adds up to a noticeably slower feedback loop than running the same tests in a lighter environment.
Jest: a popular, community-driven waypoint
For a significant stretch, Jest became a widely adopted alternative in the Angular community — not an official default, but popular enough that a large share of real Angular codebases from that era use it instead of Karma. Jest runs in Node directly, simulating a DOM rather than using a real browser’s rendering engine, which is considerably faster to start and run. The tradeoff is fidelity: a simulated DOM isn’t a perfect stand-in for genuine browser behavior in every case, though for the overwhelming majority of component and service tests, the difference never actually matters in practice.
Why a test runner migration is rarely a rewrite
It’s worth being reassuring about the actual migration cost here, since
“the test runner changed” can sound more disruptive than it is. Everything
from the previous two lessons — TestBed, fixtures, HttpTestingController —
is Angular’s own testing API, independent of which runner executes the test
file. Switching from Karma to Jest, or from either to Vitest, mostly changes
configuration and how the test command is invoked, not the test code itself;
describe/it/expect read nearly identically across all three, and
Angular’s own TestBed-based assertions don’t change at all. The real
migration work tends to live in a handful of runner-specific details — mock
timers, module-resolution quirks, a handful of APIs each runner names
slightly differently — not in rewriting the substance of what each test
actually checks.
Vitest: the current default, with real-browser mode when you need it
Current Angular’s official application builder now defaults new projects to
Vitest, with Karma deprecated. Vitest largely continues the Jest-era
tradeoff — fast, Node-based execution by default, no real browser spun up for
most test runs — while adding a genuine browser-mode option for the specific
tests that do need real-browser fidelity, rather than forcing an all-or-nothing
choice the way Karma-versus-Jest effectively did. If you’re setting up a new
Angular project today, Vitest is what ng new wires up; if you’re working in
an existing codebase, don’t be surprised to find Karma or Jest configured
instead — both remain common in real, actively maintained projects, and the
testing concepts from the previous two lessons apply identically regardless of
which runner is executing them.
This lesson’s video covers the shift to Vitest directly, from a channel that’s tracked Angular’s testing story closely; the linked “deeper” video, while dated to 2023, is worth watching for the Karma-to-Jest half of this history told by someone who lived through it, even though it predates Vitest becoming the default.
Check yourself
Why did Karma, running tests inside an actual browser, start to feel heavy for large Angular test suites?
Spinning up a real (even headless) browser instance has real startup and execution overhead compared to running tests directly in a Node process — for a large suite run constantly during development, that overhead adds up into noticeably slower feedback loops, which is the core complaint that motivated looking for alternatives.
What's the actual tradeoff of running Angular tests in Vitest's default mode instead of a real browser, and how does Vitest's browser mode address it?
The default mode simulates a DOM in Node rather than using a genuine browser's rendering engine — faster, but not a perfect stand-in for real browser behavior in every edge case. Vitest's browser mode runs tests in an actual browser instead, for the cases where that fidelity specifically matters, while keeping the faster default available for the large majority of tests that don't need it.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.