Most of the time, you will be using Karma directly from the command line. You can, however, call Karma programmatically from your node module. Here is the public API.
var Server = require('karma').Server
var karmaConfig = {port: 9876}
var server = new Server(karmaConfig, function(exitCode) {
console.log('Karma has exited with ' + exitCode)
process.exit(exitCode)
})
Notice the capital 'S' on require('karma').Server
.
Equivalent of karma start
.
server.start()
Trigger a file list refresh. Returns a promise.
server.refreshFiles()
Trigger a file refresh. Returns a promise.
server.refreshFile('src/js/module-dep.js')
The server
object is an EventEmitter
. You can simply listen to events like this:
server.on('browser_register', function (browser) {
console.log('A new browser was registered')
})
listening
#Arguments:
port
: Port numberBegin accepting connections on the specified port.
browser_register
#Arguments:
browser
: The browser instanceA new browser was opened, but is not ready yet.
browser_error
#Arguments:
browser
: The browser instanceerror
: The error that occurredThere was an error in this browser instance.
browser_start
#Arguments:
browser
: The browser instanceinfo
: Details about the runA test run is beginning in this browser.
browser_complete
#Arguments:
browser
: The browser instanceresult
: Test resultsA test run has completed in this browser.
browsers_change
#Arguments:
browsers
: A collection of browser instancesThe list of browsers has changed.
browsers_ready
#All browsers are ready for execution
run_start
#Arguments:
browsers
: A collection of browser instances on which tests are executedA test run starts.
run_complete
#Arguments:
browsers
: A collection of browser instancesresults
: A list of resultsThis event gets triggered whenever all the browsers, which belong to a test run, finish. For example, on a run that has 3 browsers, one would expect 3 browser_complete
events before the run_complete
one.
The equivalent of karma run
.
var runner = require('karma').runner
runner.run({port: 9876}, function(exitCode) {
console.log('Karma has exited with ' + exitCode)
process.exit(exitCode)
})
runner.run()
returns an EventEmitter
which emits a progress
event passing
the reporter output as a Buffer
object.
You may listen for that event to print the reporter output to the console:
runner.run({port: 9876}).on('progress', function(data) {
process.stdout.write(data)
})
This function will signal a running server to stop. The equivalent of karma stop
.
var stopper = require('karma').stopper
stopper.stop({port: 9876}, function(exitCode) {
if (exitCode === 0) {
console.log('Server stop as initiated')
}
process.exit(exitCode)
})
This function will load given config file and returns a filled config object. This can be useful if you want to integrate karma into another tool and want to load the karma config while honoring the karma defaults. For example, the stryker-karma-runner uses this to load your karma configuration and use that in the stryker configuration.
const cfg = require('karma').config;
const path = require('path');
// Read karma.conf.js, but override port with 1337
const karmaConfig = cfg.parseConfig(path.resolve('./karma.conf.js'), { port: 1337 } );
The current version of karma
The default port used for the karma server
The default hostname used for the karma server
The default address use for the karma server to listen on
The value for disabling logs
The value for the log error
level
The value for the log warn
level
The value for the log info
level
The value for the log debug
level
An array of log levels in descending order, i.e. LOG_DISABLE
, LOG_ERROR
, LOG_WARN
, LOG_INFO
, and LOG_DEBUG
The default color pattern for log output
The default pattern for log output without color
The default console appender
The exit code