Karma Browser Auto-Capture and singleRun Diagnostic Guide
Fix Karma browser capture failures and singleRun hangs with ordered checks, config fixes, and escalation criteria.
03 Sept 2025, 04:06 UTC

Problem and takeaway
Karma starts, but the browser never shows a "Captured" badge, or the run ends with "Executed 0 of 0" despite test files existing. The useful takeaway is that most auto-capture and singleRun failures come from a small set of configuration issues: an occupied default port, a missing browser launcher, mismatched file globs, or singleRun closing the browser before tests execute. This guide matches each symptom to a check and a fix.
Recognizable conditions
- Terminal output lacks a line containing
Capturedafterkarma start. - A browser window opens but remains blank or shows a connection error.
- CI logs show
Capturedbut noExecutedlines. - The process exits immediately with an empty suite, even though spec files exist.
Cause and diagnostic table
| Symptom | Likely cause | First place to check |
|---|---|---|
| No "Captured" line in logs | Port 9876 occupied or browser launcher missing | karma.conf.js port and plugins |
| Browser opens but stays blank | CSP or same-origin policy blocking Karma adapter | Browser DevTools Console tab |
singleRun ends before tests execute | singleRun: true with no active watch or activity timeout | karma.conf.js singleRun and browserNoActivityTimeout |
| "Executed 0 of 0" despite spec files | files or exclude globs match nothing | karma.conf.js files patterns |
Ordered checks
1. Check whether the default port is in use
Run this in your project root before starting Karma:
lsof -i :9876
On Windows PowerShell, use:
netstat -ano | findstr :9876
Where: terminal at the project root. Permissions: none normally required. Expected check: no output, or only the Karma process. Risk: do not kill an unrelated process just to free the port; change Karma's port instead.
If another service holds port 9876, edit karma.conf.js:
module.exports = function (config) {
config.set({
port: 9877,
// ...
});
};
Update every CI script and README that references port 9876 to match.
2. Confirm the browser launcher is installed
Karma does not bundle browser launchers. For Chrome, check:
ls node_modules | grep karma-chrome-launcher
Expected check: a directory named karma-chrome-launcher appears. If it is missing, install it:
npm install --save-dev karma-chrome-launcher
Then add it to plugins and browsers in karma.conf.js:
plugins: ['karma-chrome-launcher'],
browsers: ['ChromeHeadless'],
ChromeHeadless runs Chrome without a visible UI, which is usually preferable in CI.
3. Inspect singleRun behavior
singleRun: true tells Karma to run once and exit. In local development this can close the browser before you see results. In CI it is usually desired, but it must be paired with a timeout so a hung browser does not block the pipeline indefinitely.
Check the config:
singleRun: true,
browserNoActivityTimeout: 100000,
For interactive local runs, use:
karma start --no-single-run
or set singleRun: false in the config. Do not leave singleRun: false in CI without a job-level timeout, or the pipeline can hang.
4. Validate test file globs
Karma runs only files matched by the files array, minus anything in exclude. A common mistake is a glob that misses the spec directory:
files: [
'src/**/*.js',
'test/**/*.spec.js'
],
exclude: [],
If your specs live in __tests__ instead of test, the suite executes zero tests. Run karma start and confirm the terminal lists the files being included before execution starts.
5. Check the browser console for adapter errors
If the browser captures but the page stays blank, open DevTools and look at the Console. Enterprise browsers sometimes block Karma's debug adapter with a Content Security Policy (CSP) or same-origin error. The fix is to whitelist the Karma server origin, typically http://localhost:9876, and any script sources your tests require. This is a browser policy setting, not a Karma config change.
Fixes tied to findings
| Finding | Fix |
|---|---|
| Port 9876 occupied | Set port: 9877 in karma.conf.js and update CI references. |
| Missing launcher | Install karma-chrome-launcher or equivalent, then list it in plugins and browsers. |
singleRun closes browser too soon | Use singleRun: false locally; in CI keep true with browserNoActivityTimeout and a pipeline timeout. |
| Glob matches no files | Correct files patterns to match the actual spec directory and extension. |
| CSP blocks adapter | Allow http://localhost:9876 in the browser's CSP policy or use a less restricted browser for local debugging. |
Escalation criteria
Escalate to the browser or infrastructure team when:
- The browser captures successfully but no
Executedlines appear after two minutes, suggesting a runner or adapter crash rather than a config issue. - CSP errors persist in a managed enterprise browser and you cannot change the policy.
- Changing the port breaks downstream CI jobs and the port cannot be standardized across environments.
Limitations and practical verification
This guide assumes Karma 6.x in a Node.js project. Confirm your version with:
karma --version
To verify a fix, run:
karma start
A successful local run shows a Captured line with the browser name and version, followed by Executed X of Y SUCCESS. In CI, parse the log for both Captured and Executed; missing Captured means no browser connected, while missing Executed means tests did not run.
State change and rollback
Editing karma.conf.js changes repository state. Before editing, back up the file:
cp karma.conf.js karma.conf.js.bak
If the new configuration fails, restore it with:
cp karma.conf.js.bak karma.conf.js
Remove the backup after the fix is confirmed.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.