simplebuild-karma
v2.0.0
Published
A simple library for automating Karma
Downloads
25
Readme
Simplebuild-Karma
A simple library for automating Karma.
Karma is a test runner for JavaScript. It’s useful for automated cross-browser testing. This library provides a simple interface to Karma that's convenient to use with task automation tools such as Grunt or Jake.
Installation
This is a Node.js library. Install Node, then:
npm install simplebuild-karma
(add --save
or --save-dev
if you want)
Note that this library uses your existing Karma installation. Karma 4.3 or higher must be installed. (To use previous versions of Karma, install v1 of simplebuild-karma.)
Usage
This library provides these functions:
start
: Start the Karma server.run
: Run Karma.
There are two main ways to use Karma: launch Karma and test browsers once and leave them running ("development" workflow), or start Karma and the browsers every time you test ("CI server" workflow).
To use the development workflow, call start
, manually capture your test browsers, then call run
every time you want to run your tests. (To make Karma capture your test browsers automatically, use the browsers
option in the Karma config file.)
To use the CI server workflow, call run
with the capture
option set. Karma will launch, capture your browsers, run your tests, then automatically shut down.
start(options, success, failure)
Start the Karma server and leave it running.
options
: an object containing the following properties:configFile
(string): path to the Karma config file.
success()
: a function to call if the server exits successfully.failure(message)
: a function to call if the server does not exit successfully. A simple error message is provided in themessage
parameter, but detailed error messages are provided by Karma to stdout (or stderr).
run(options, success, failure)
Run Karma tests. You need to call start
first unless you use the capture
option.
options
: an object containing the following properties:configFile
(string): path to the Karma config file.expectedBrowsers
(optional array of strings, default[]
): a list of test browsers. A warning is printed to stdout for each browser in the list that isn't tested. The string must exactly match the string displayed by Karma (e.g., "Chrome 42.0.2311 (Mac OS X 10.10.3)").strict
(optional boolean, defaulttrue
): causes the tests to fail ifexpectedBrowsers
results in any warnings.capture
(optional array of strings, default[]
): if present, automatically starts Karma, captures the provided browsers, and shuts them down when done. (Equivalent to usingkarma start configFile --browsers XX,YY --single-run
.)clientArgs
(optional array of strings, default[]
): options to pass through to your test adapter. For example, when using Mocha, providing[ "--grep=SOMETHING" ]
will cause Mocha to only run tests containing the string "SOMETHING." The behavior of this parameter depends on your test adapter. It's equivalent to theclient.args
option in the Karma configuration file.
success()
: a function to call if the tests succeed.failure(message)
: a function to call if the tests fail. A simple error message is provided in themessage
parameter, but detailed error messages are provided by Karma to stdout (or stderr).
Examples
This library is designed to be easy to integrate with any task automation tool:
Grunt
var karma = require("simplebuild-karma");
module.exports = function(grunt) {
grunt.initConfig({
karma: {
configFile: "karma.conf.js",
expectedBrowsers: [
"Chrome 42.0.2311 (Mac OS X 10.10.3)",
"Firefox 37.0.0 (Mac OS X 10.10)"
]
}
});
grunt.registerTask("karma", "Start Karma server", function() {
karma.start(grunt.config("karma"), this.async(), grunt.warn);
});
grunt.registerTask("test", "Run tests", function() {
karma.run(grunt.config("karma"), this.async(), grunt.warn);
});
grunt.registerTask("default", [ "test" ]);
};
Jake
var karma = require("simplebuild-karma");
task("default", [ "test" ]);
desc("Start Karma server");
task("karma", function() {
karma.start({
configFile: "karma.conf.js"
}, complete, fail);
}, { async: true });
desc("Run tests");
task("test", function() {
karma.run({
configFile: "karma.conf.js",
expectedBrowsers: [
"Chrome 42.0.2311 (Mac OS X 10.10.3)",
"Firefox 37.0.0 (Mac OS X 10.10)"
]
}, complete, fail);
}, { async: true });
Plain JavaScript
var karma = require("simplebuild-karma");
karma.run({
configFile: "karma.conf.js",
expectedBrowsers: [
"Chrome 42.0.2311 (Mac OS X 10.10.3)",
"Firefox 37.0.0 (Mac OS X 10.10)"
],
capture: [ "Chrome", "Firefox" ]
}, function() {
console.log("OK");
}, function(message) {
console.log(message);
});
About Simplebuild
This library is a simplebuild module. In addition to being used as a standalone library (as described above), it can also be used with simplebuild extensions and mappers. For more information about simplebuild, see the Simplebuild GitHub page.
Version History
- 1.0.0: Added
clientArgs
pass-through - 0.8.1: Fix: Corrected API naming error (
start
was misnamed) - 0.8.0: Initial release.
Contributors
Created by James Shore.
Release Process
- Update version history in readme and check in
- Ensure clean build:
./jake.sh karma
- Capture firefox:
http://localhost:9876
./jake.sh
- Update npm version:
npm version [major|minor|patch]
- Release to npm:
npm publish
- Release to github:
git push && git push --tags
License
The MIT License (MIT)
Copyright (c) 2012-2016 James Shore
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.