Karma Test Runner
Karma, currently at stable version 6.4.4, is a long-standing JavaScript test runner specifically designed to execute tests directly within real browser environments. Its core utility lies in facilitating test-driven development by providing quick feedback cycles, enabling testing across diverse browsers (desktop, mobile, tablets) and integration with continuous integration servers. It supports running tests on file saves and offers features like coverage reporting via Istanbul. However, the project has been officially deprecated. The web testing ecosystem has evolved significantly since Karma's inception over ten years ago, leading to the emergence of more performant and modern alternatives. While critical security issues will continue to be addressed until 12 months after Angular CLI's Web Test Runner support stabilizes, no new features or general bug fixes are being accepted. Users are strongly encouraged to migrate to modern alternatives such as Web Test Runner, `jasmine-browser-runner`, Jest, or Vitest.
Common errors
-
No captured browser, open http://localhost:9876/
cause The Karma server started successfully, but no browser instance connected to it. This can happen if a browser launcher is missing, misconfigured, or if the browser failed to launch.fixVerify that you have the necessary browser launcher installed (e.g., `karma-chrome-launcher`), that it's listed in the `browsers` array of your `karma.conf.js`, and that no firewall or system setting is preventing the browser from launching or connecting to the Karma server. -
Can not load "Chrome" browser! Are you missing a plugin for it?
cause Karma cannot find the necessary plugin to launch the specified browser (e.g., 'Chrome', 'Firefox', 'Safari').fixInstall the corresponding Karma browser launcher package (e.g., `npm install --save-dev karma-chrome-launcher`) and ensure it's listed in your `karma.conf.js` file, typically in the `plugins` array if manually specified, or implicitly used if added to `browsers`. -
Can not load "jasmine" framework! Are you missing a plugin for it?
cause The testing framework specified in your `karma.conf.js` (e.g., 'jasmine', 'mocha', 'qunit') does not have a corresponding Karma plugin installed.fixInstall the correct Karma plugin for your chosen framework (e.g., `npm install --save-dev karma-jasmine`) and ensure it's included in the `frameworks` array within your `karma.conf.js`.
Warnings
- deprecated Karma is officially deprecated. The project will no longer receive new features or general bug fixes. While critical security fixes will continue for a limited period, users are strongly encouraged to migrate to modern alternatives.
- gotcha Karma requires Node.js version >= 10. Using older Node.js versions will lead to installation and runtime failures. Using very new, unsupported Node.js versions might also lead to compatibility issues, particularly with older plugins.
- breaking A malicious update to the `colors` package (a dependency of `karma`) in January 2022 introduced potential supply chain vulnerabilities, leading to corrupted output or system issues for users who pulled specific versions.
- breaking An 'Open Redirect Vulnerability' was present in Karma, which could allow an attacker to redirect users to arbitrary external URLs if certain conditions were met.
- gotcha Karma's configuration file (`karma.conf.js`) is fundamentally designed for CommonJS syntax (`module.exports = function(config) { ... }`). Attempting to use ESM `import`/`export` syntax directly in this file will lead to syntax errors or unexpected behavior, even in Node.js environments configured for ESM.
Install
-
npm install karma -
yarn add karma -
pnpm add karma
Imports
- Server
import { Server } from 'karma';const Server = require('karma').Server; - config
import { config } from 'karma';const config = require('karma').config; - runner
import { runner } from 'karma';const runner = require('karma').runner;
Quickstart
// karma.conf.js
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],
files: [
'src/**/*.js',
'test/**/*.js'
],
exclude: [],
preprocessors: {},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['ChromeHeadless'], // Ensure karma-chrome-launcher is installed
singleRun: false,
concurrency: Infinity
});
};
// test/example.test.js
describe('A simple test', function() {
it('should pass', function() {
expect(true).toBe(true);
});
});