Skip to content

Introduction to Karma

Karma is a test runner, originally created by the AngularJS team, that launches real (or headless) web browsers to execute your JavaScript tests. This lesson introduces what Karma is, why it exists, and where it still fits in modern frontend testing.

What Is Karma?

Karma is an open-source test runner created by Vojta Jína while working on the AngularJS team at Google. Its job is narrow but important: launch one or more browsers, load your application code and test files into each one, run the tests inside the real browser engine, and report the pass/fail results back to your terminal or CI pipeline.

Karma itself does not know how to write or execute assertions. It relies on adapter plugins like karma-jasmine or karma-mocha to bring in an actual testing framework, and on launcher plugins like karma-chrome-launcher to control specific browsers. This plugin-based design is what let Karma support many browsers and frameworks through one consistent runner.

// karma.conf.js
module.exports = function(config) {
  config.set({
    frameworks: ['jasmine'],
    files: ['src/**/*.spec.js'],
    browsers: ['ChromeHeadless'],
    singleRun: true
  });
};

A minimal Karma configuration: use Jasmine for assertions, run every spec file, launch headless Chrome, and exit after one run.

How a Karma Run Works

1. karma start reads karma.conf.js
2. Karma starts a local web server (default port 9876)
3. Karma launches the configured browser(s)
4. Each browser loads a generated HTML page with your files
5. The framework adapter (e.g. karma-jasmine) runs your tests
6. Results stream back to Karma over a socket connection
7. Karma prints results and exits (or watches for changes)
  • Karma is a coordinator, not a test framework — Jasmine or Mocha still defines describe/it.
  • Every configured browser runs the full test suite independently.
  • singleRun: true exits after one pass, which is what CI pipelines want.
  • Without singleRun, Karma stays open in watch mode and reruns tests on file changes.

Karma at a Glance

The essential vocabulary you need before touching a karma.conf.js file.

Concept Example Purpose
Config file karma.conf.js Describes how to run your tests
Start Karma karma start Launches the configured browsers and runs tests
Framework adapter karma-jasmine Provides describe/it/expect
Launcher karma-chrome-launcher Controls launching a specific browser
Headless browser ChromeHeadless Runs Chrome without a visible window
Reporter progress Formats test output in the terminal
Watch mode autoWatch: true Reruns tests automatically on file changes
CI mode singleRun: true Runs once and exits with a pass/fail code

Why Karma Exists

Before Karma, testing AngularJS applications against real DOM and browser behavior meant either manually opening HTML test pages in a browser, or trusting a simulated DOM that might not match real browser quirks exactly. The AngularJS team needed a way to run the same test suite automatically, across multiple real browsers, from the command line and from continuous integration.

Karma solved that by automating the entire loop: it serves your files, opens the browsers you configure, injects the test files, and collects results — all without you manually clicking 'run' in a browser tab.

  • Runs tests inside real browser engines, not a simulated environment.
  • Supports multiple browsers at once (e.g. Chrome and Firefox in the same run).
  • Framework-agnostic through adapters (Jasmine, Mocha, QUnit, and others).
  • Was the default test runner scaffolded by the Angular CLI for years.

Where Karma Fits Today

It's worth being direct about Karma's current status: many new JavaScript and even Angular projects now default to Jest, Vitest, or the Angular team's own Web Test Runner-based builder, largely because those tools start faster and don't require spinning up a real browser for every run. Karma has also been formally deprecated by the Karma maintainers and by the Angular team as the default unit-test runner in newer Angular CLI versions.

That said, Karma remains highly relevant in practice. A huge number of existing Angular applications — and other frontend codebases built over the last decade — still ship with a working karma.conf.js, and maintaining, debugging, and gradually migrating those suites is a real, common task for frontend developers today.

Scenario Typical Choice
Maintaining an existing Angular app with Karma Keep and understand Karma
Starting a brand-new Angular app (recent CLI) Jest or Web Test Runner (CLI default)
Need guaranteed real-browser DOM behavior Karma, Web Test Runner, or Playwright
Fast unit tests with simulated DOM Jest or Vitest with jsdom

Common Mistakes

  • Assuming Karma is a testing framework itself, rather than a runner that delegates to Jasmine/Mocha.
  • Starting a brand-new project with Karma today without first checking whether Jest/Vitest better fits the team's needs.
  • Not realizing Karma requires a real (or headless) browser binary installed and reachable on the machine running tests.
  • Confusing Karma (the runner) with karma-jasmine or karma-mocha (the framework adapters it depends on).

Key Takeaways

  • Karma is a browser-based test runner, originally built by the AngularJS team.
  • It delegates assertions to a framework adapter like karma-jasmine or karma-mocha.
  • It launches real or headless browsers and streams results back over a local server.
  • Karma is legacy for many new projects but still essential for maintaining existing Angular/Karma test suites.

Pro Tip

If you inherit a project with a karma.conf.js you don't understand yet, run karma start --single-run first with no other flags. Watching one full pass end-to-end is the fastest way to build a mental model before you start editing configuration.