Karma Test Runner: Setting Up Server-Side Execution for Node.js Applications
Karma runs tests in real browsers through a WebSocket server. Use ChromeHeadless for CI environments by configuring the browsers array in karma.conf.js. This gives real browser behavior without GUI overhead.
21 Oct 2026, 18:21 UTC

Quick Answer: Karma's Server-Side Execution Model
Karma runs tests in real browsers through a WebSocket server, not directly on Node.js. For CI environments, use ChromeHeadless or FirefoxHeadless browsers defined in your karma.conf.js. This gives you real browser behavior without GUI overhead.
How Karma's Architecture Works
Karma starts a Node.js server that opens real browsers (or headless instances) via WebDriver. Tests execute in the browser context, with results sent back through WebSocket adapters. This means your test environment matches real user conditions.
Working Configuration Example
Create karma.conf.js with this minimal setup for Jasmine and ChromeHeadless:
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine', 'jquery-3.6.0'],
files: ['src/**/*.js', 'test/**/*.spec.js'],
browsers: ['ChromeHeadless'],
singleRun: true,
reporters: ['progress', 'coverage'],
preprocessors: {'src/**/*.js': ['coverage']},
coverageReporter: {
type: 'html',
dir: 'coverage/'
},
port: 9876,
browserNoActivityTimeout: 60000
});
};
Installation and Setup
Run these commands from your project root (requires npm permissions):
npm install karma jasmine karma-jasmine karma-chrome-launcher karma-coverage jasmine-core --save-devnpx karma initto generate a baseline config- Modify
browsersarray to includeChromeHeadless
Running Tests
Execute from project directory:
npx karma start karma.conf.js --single-run
For continuous testing during development:
npx karma start karma.conf.js
This watches files and re-runs tests on changes.Common Configuration Issues
Port Conflicts
If port 9876 is in use, specify a custom port:
port: 9877
Timeout Problems
Increase browserNoActivityTimeout if browsers hang:
browserNoActivityTimeout: 60000
Headless Browser Not Found
Ensure Chrome/Chromium is installed. On Linux CI servers, install chromium-browser package or set Chrome binary path:
customLaunchers: {
ChromeHeadlessCI: {
base: 'ChromeHeadless',
flags: ['--no-sandbox', '--disable-gpu']
}
}
Limitations and Practical Constraints
- Browser Installation Required: CI servers need actual Chrome/Firefox binaries, not just Node.js
- Memory Usage: Large test suites can cause memory leaks; monitor with
ps aux | grep karma - Version Compatibility: Headless browser behavior varies across Karma and Chrome versions
- Asynchronous Test Handling: Jasmine's default timeout is 5000ms; increase with
jasmine.DEFAULT_TIMEOUT_INTERVALin your specs
Verification Steps
- Check browser console output for test results after running
- Verify
coverage/directory containsindex.htmlafter coverage runs - Run
npm test(if configured) to confirm integration with package.json scripts
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.