Running Karma Unit Tests in Headless Chrome with Custom Launch Flags
Learn how to configure Karma to run unit tests in a headless Chrome instance using karma‑chrome‑launcher, including installation, configuration, flags, and trade‑offs for CI.
22 Jan 2026, 05:25 UTC

Problem: Tests stall in CI because Chrome opens a UI window
\nWhen Karma launches Chrome for unit tests, the default launcher tries to open a visible browser window. In headless CI environments this either fails or wastes resources, slowing down the pipeline.
\n\nThesis: Use the HeadlessChrome launcher from karma‑chrome‑launcher with custom flags
\nThe karma-chrome-launcher plugin provides a ChromeHeadless launcher that runs Chromium without a UI. You can further tune it with Chrome flags such as --no-sandbox or --disable-gpu to match the constraints of your runner.
Installing the plugin
\n- \n
- Open a terminal in the project root (you need write permission to create
node_modulesand to install packages). \n - Run: \n
npm i -D karma karma-chrome-launcher jasmine jasmine-core\nThis installs Karma, the Chrome launcher, and a test framework as dev dependencies.
\n\nConfiguring karma.conf.js
\nCreate or edit karma.conf.js in the project root:
module.exports = function(config) {\n config.set({\n frameworks: ['jasmine'],\n files: ['test/**/*.spec.js'],\n browsers: ['ChromeHeadless'],\n customLaunchers: {\n ChromeHeadless: {\n base: 'ChromeHeadless',\n flags: ['--no-sandbox', '--disable-gpu']\n }\n },\n singleRun: true,\n autoWatch: false\n });\n};\n\nThe browsers array tells Karma to use the launcher named ChromeHeadless. The customLaunchers block defines that launcher, inheriting from the plugin’s base and adding the flags you need.
Running the tests
\nExecute the test suite from the same terminal:
\nnpx karma start --single-run\nKarma will launch Chromium in headless mode, run the specs, and print results to the console. You should see a line similar to:
\nChrome Headless 112.0.5615.49 (Linux 0.0.0) ...\nNo browser window appears, confirming the headless launch.
\n\nTrade‑off and limitation
\n- \n
- Security: The
--no-sandboxflag is often required in container‑based CI (e.g., Docker) but reduces the isolation guarantees of Chrome. Evaluate whether your runner already provides sufficient isolation (e.g., user namespaces, VM) before adding it. \n - Binary dependency: The launcher expects a
chromeorchromiumexecutable in the PATH. On minimal CI images you may need to install it first, e.g.,apt-get install -y chromium-browseron Debian‑based systems. \n - Plugin version: Versions of
karma-chrome-launcherolder than 2.2.0 may not recognise newer Chrome flags. Keep the plugin updated (npm outdated karma-chrome-launcher) to match your Chrome version. \n
Practical verification
\nAfter the run, check two things:
\n- \n
- The output contains the string
Chrome Headless(orHeadlessChrome) indicating the correct launcher. \n - No graphical browser window popped up on the machine running the command (you can verify by observing the desktop or, in a CI log, by the absence of any GUI‑related error). \n
If either check fails, revisit the customLaunchers flags or ensure a Chrome binary is installed.
Actionable closing
\nAdd the karma-chrome-launcher dependency and the configuration snippet above to your repository. Commit the updated package.json and karma.conf.js. In your CI pipeline, run npx karma start --single-run as part of the test stage. Monitor the launcher line in the logs to confirm headless execution, and periodically update the plugin and Chrome binary to stay compatible.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.