Run Karma Tests Once in CI with Headless Chrome
Configure Karma to run tests once in CI with ChromeHeadless, verify exit codes, and fix common launcher timeouts.
04 Oct 2025, 15:54 UTC

Desired outcome
Run a Karma test suite exactly once in a continuous integration (CI) job, capture the results in a reporter, and exit with a non-zero status code when any test or browser launch fails. The CI job should not hang waiting for a browser that never starts, and it should not mark a build successful when tests fail.
Useful takeaway: configure singleRun: true, launch ChromeHeadless through karma-chrome-launcher, and make the CI step depend on Karma’s exit code rather than on console output alone.
Prerequisites
- Node.js and npm installed on the CI runner or local machine.
- Karma installed as a project dev dependency.
- An existing test suite using a Karma framework such as Jasmine or Mocha.
- Google Chrome or Chromium installed where Karma runs.
- The
karma-chrome-launcherpackage installed.
Install the Chrome launcher
Run this in the project root. It requires write access to package.json and node_modules.
npm install --save-dev karma karma-chrome-launcher
Configure karma.conf.js for CI
Open karma.conf.js in the project root. The files array is served in listed order, so place library files before the test files that import them.
module.exports = function (config) {
config.set({
frameworks: ['jasmine'],
// Served in this order; dependencies first.
files: [
'src/vendor/**/*.js',
'src/**/*.js',
'test/**/*.spec.js'
],
preprocessors: {},
reporters: ['progress'],
// Headless Chrome has no graphical display.
browsers: ['ChromeHeadlessCI'],
// Run once and exit. Do not use autoWatch: true in CI.
singleRun: true,
autoWatch: false,
// Abort if Chrome takes too long to connect.
captureTimeout: 60000,
customLaunchers: {
ChromeHeadlessCI: {
base: 'ChromeHeadless',
flags: ['--no-sandbox', '--disable-gpu', '--disable-dev-shm-usage']
}
}
});
};
ChromeHeadless is a launcher that starts Chrome without a user interface. singleRun tells Karma to stop after the suite finishes. captureTimeout is the maximum milliseconds Karma waits for a browser to connect before reporting an error.
Point Karma at the Chrome binary
Karma needs the CHROME_BIN environment variable or a Chrome binary on PATH. Set it before starting Karma.
export CHROME_BIN="/usr/bin/google-chrome-stable"
npx karma start karma.conf.js --single-run
Run npx karma start karma.conf.js --single-run from the project root. No elevated permissions are required, but the user must be able to execute the Chrome binary and read the project files.
Expected checks
| Check | What to confirm |
|---|---|
| Reporter output | Output similar to Chrome Headless ...: Executed X of Y SUCCESS appears before the process ends. |
| Exit code | The shell returns to the prompt and echo $? prints 0 on success. |
| Failure behavior | Temporarily add a failing assertion, run the same command, and confirm a non-zero exit code with the failure named in the reporter output. |
Troubleshooting launcher failures
ERROR [launcher]: Cannot start Chrome: Chrome or Chromium is missing, orCHROME_BINpoints to the wrong path. Verify withls -l "$CHROME_BIN".Capture timeout: Chrome started but never connected to the Karma server. IncreasecaptureTimeoutor check that the CI container allows loopback network access.- Shared memory errors in containers: add
--disable-dev-shm-usagetocustomLaunchersflags, or mount/dev/shmwith more space. --no-sandbox: Required in some root-run containers, but it reduces Chrome’s process isolation. Use it only when the container security model allows it.
Recovery options
If the configuration breaks the local test run, revert karma.conf.js to the previous version from version control and remove the temporary CHROME_BIN export. Changing singleRun back to false with autoWatch: true keeps Karma running and is useful for local debugging, but it must not be left unattended in CI.
Limitations
- Headless Chrome requires graphics and shared-memory libraries even though it has no display. Minimal container images often lack them.
singleRun: truedoes not re-run tests when source files change; pair it with a CI trigger, not a watch workflow.- Reporter output alone is not a reliable pass/fail signal; always check the process exit code.
Practical verification
- Run
npx karma start karma.conf.js --single-runon a clean checkout. - Confirm
echo $?returns0and the reporter lists all suites as successful. - Introduce one deliberate failing assertion, run the command again, and confirm the exit code is non-zero.
- Remove the failing assertion and confirm the exit code returns to
0.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.