grunt-contrib-qunit
Run QUnit unit tests in a headless PhantomJS instance.
Note that this plugin has not yet been released, and only works with the latest bleeding-edge, in-development version of grunt. See the When will I be able to use in-development feature 'X'? FAQ entry for more information.
Getting Started
If you haven't used grunt before, be sure to check out the Getting Started guide, as it explains how to create a gruntfile as well as install and use grunt plugins. Once you're familiar with that process, install this plugin with this command:
npm install grunt-contrib-qunit --save-dev
Qunit task
Run this task with the grunt qunit
command.
This task is a [multi task][] so any targets, files and options should be specified according to the [multi task][] documentation.
[multi task]: https://github.com/gruntjs/grunt/wiki/Configuring-tasks
When installed by npm, this plugin will automatically download and install PhantomJS locally via the grunt-lib-phantomjs library.
Also note that running grunt with the --debug
flag will output a lot of PhantomJS-specific debugging information. This can be very helpful in seeing what actual URIs are being requested and received by PhantomJS.
Options
timeout
Type: Number
Default: 5000
The amount of time (in milliseconds) that grunt will wait for a QUnit start()
call before failing the task with an error.
inject
Type: String
Default: (built-in)
Path to an alternate QUnit-PhantomJS bridge file to be injected. See the built-in bridge for more information.
urls
Type: Array
Default: []
Absolute http://
or https://
urls to be passed to PhantomJS. Specified URLs will be merged with any specified src
files first. Note that urls must be served by a web server, and since this task doesn't contain a web server, one will need to be configured separately. The grunt-contrib-connect plugin provides a basic web server.
(-- PhantomJS arguments)
Type: String
Default: (none)
Additional --
style arguments that need to be passed in to PhantomJS may be specified as options, like {'--option': 'value'}
. This may be useful for specifying a cookies file, local storage file, or a proxy. See the PhantomJS API Reference for a list of --
options that PhantomJS supports.
Usage examples
Wildcards
In this example, grunt qunit:all
(or grunt qunit
because qunit
is a [multi task][]) will test all .html
files in the test directory and all subdirectories. First, the wildcard is expanded to match each individual file. Then, each matched filename is passed to PhantomJS (one at a time).
grunt.initConfig({
qunit: {
all: ['test/**/*.html']
}
});
Testing via http:// or https://
In circumstances where running unit tests from local files is inadequate, you can specify http://
or https://
URLs via the urls
option. Each URL is passed to PhantomJS (one at a time).
In this example, grunt qunit
will test two files, served from the server running at localhost:8000
.
grunt.initConfig({
qunit: {
all: {
options: {
urls: [
'http://localhost:8000/test/foo.html',
'http://localhost:8000/test/bar.html'
]
}
}
}
});
Wildcards and URLs may be combined by specifying both.
Using the grunt-contrib-connect plugin
It's important to note that grunt does not automatically start a localhost
web server. That being said, the grunt-contrib-connect plugin connect
task can be run before the qunit
task to serve files via a simple connect web server.
In the following example, if a web server isn't running at localhost:8000
, running grunt qunit
with the following configuration will fail because the qunit
task won't be able to load the specified URLs. However, running grunt connect qunit
will first start a static connect web server at localhost:8000
with its base path set to the Gruntfile's directory. Then, the qunit
task will be run, requesting the specified URLs.
grunt.initConfig({
qunit: {
all: ['http://localhost:8000/test/foo.html', 'http://localhost:8000/test/bar.html']
},
connect: {
server: {
options: {
port: 8000,
base: '.'
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.registerTask('test', ['connect', 'qunit']);
Custom timeouts and PhantomJS options
In the following example, the default timeout value of 5000
is overridden with the value 10000
(timeout values are in milliseconds). Additionally, PhantomJS will read stored cookies from the specified file. See the PhantomJS API Reference for a list of --
options that PhantomJS supports.
grunt.initConfig({
qunit: {
options: {
timeout: 10000,
'--cookies-file': 'misc/cookies.txt'
},
all: ['test/**/*.html']
}
});
Events and reporting
QUnit callback methods and arguments are also emitted through grunt's event system so that you may build custom reporting tools. Please refer to to the QUnit documentation for more information.
The events (with arguments) are as follows:
qunit.begin
qunit.moduleStart
: namequnit.testStart
: namequnit.log
: result, actual, expected, message, sourcequnit.testDone
: name, failed, passed, totalqunit.moduleDone
: name, failed, passed, totalqunit.done
: failed, passed, total, runtime
In addition to QUnit callback-named events, the following event is emitted when PhantomJS is spawned for a test:
You may listen for these events like so:
grunt.event.on('qunit.spawn', function (url) {
grunt.log.ok("Running test: " + url);
});
Release History
- 2013-01-08 v0.1.1rc5 Updating to work with grunt v0.4.0rc5. Switching to this.filesSrc api. Adding "urls" option for specifying absolute test URLs.
- 2012-10-04 v0.1.0 Work in progress, not yet officially released.
Task submitted by "Cowboy" Ben Alman
This file was generated on Wed Jan 09 2013 14:12:29.