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 server = new Server({port: 9876}, function(exitCode) {
console.log('Karma has exited with ' + exitCode)
process.exit(exitCode)
})
Equivalent of karma start
.
server.start()
Trigger a file list refresh. Returns a promise.
server.refreshFiles()
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')
})
Arguments:
port
: Port number
Begin accepting connections on the specified port.
Arguments:
browser
: The browser instance
A new browser was opened, but is not ready yet.
Arguments:
browser
: The browser instanceerror
: The error that occurred
There was an error in this browser instance.
Arguments:
browser
: The browser instanceinfo
: Details about the run
A test run is beginning in this browser.
Arguments:
browser
: The browser instanceresult
: Test results
A test run has completed in this browser.
Arguments:
browsers
: A collection of browser instances
The list of browsers has changed.
All browsers are ready for execution
Arguments:
browsers
: A collection of browser instances on which tests are executed
A test run starts.
Arguments:
browsers
: A collection of browser instancesresults
: A list of results
A test run was completed.
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)
})
This function will signal a running server to stop. The equivalent of karma stop
.
var stopper = require('karma').stopper
runner.stop({port: 9876}, function(exitCode) {
if (exitCode === 0) {
console.log('Server stop as initiated')
}
process.exit(exitCode)
})
- If there is an error, the error code will be provided as the second parameter to the error callback.