Karma can be extended through plugins. A plugin is essentially an NPM module. Typically, there are four kinds of plugins: frameworks, reporters, launchers and preprocessors. The best way to understand how this works is to take a look at some of the existing plugins. Following sections list some of the plugins that you might use as a reference.
karma-*
karma-plugin
, karma-framework
.karma-*-reporter
karma-plugin
, karma-reporter
karma-*-launcher
karma-plugin
, karma-launcher
A preprocessor is a function that accepts three arguments (content
, file
, and next
), mutates the content in some way, and passes it on to the next preprocessor.
content
of the file being processedfile
object describing the file being processedsome/file.coffee
-> some/file.coffee.js
This path is mutable and may not actually exist.next(null, encodedContent)
next
function to be called when preprocessing is complete, should be called as next(null, processedContent)
or next(error)
karma-*-preprocessor
karma-plugin
, karma-preprocessor
Karma is assembled by Dependency Injection and a plugin is just an additional DI module (see node-di for more), that can be loaded by Karma. Therefore, it can ask for pretty much any Karma component and interact with it. There are a couple of plugins that do more interesting stuff like this, check out karma-closure, karma-intellij.
Karma Framework connects existing testing libraries to Karma's API, so that their results can be displayed in a browser and sent back to the server.
Karma frameworks must implement a window.__karma__.start
method that Karma will
call to start test execution. This function is called with an object that has methods
to send results back to karma:
.result
a single test has finished.complete
the client completed execution of all the tests.error
an error happened in the client.info
other data (e.g. number of tests or debugging messages)Most commonly you'll use the result
method to send individual test success or failure
statuses. The method takes an object of the form:
{
// test id
id: String,
// test description
description: String,
// the suite to which this test belongs. potentially nested.
suite: Array[String],
// an array of string error messages that might explain a failure.
// this is required if success is false.
log: Array[String],
success: Boolean, // pass / fail
skipped: Boolean // skipped / ran
}