1 | [Grunt homepage](https://github.com/cowboy/grunt) | [Documentation table of contents](toc.md)
|
2 |
|
3 | # qunit (built-in task)
|
4 | Run [QUnit][qunit] unit tests in a headless [PhantomJS][phantom] instance.
|
5 |
|
6 | [qunit]: http://docs.jquery.com/QUnit
|
7 | [phantom]: http://www.phantomjs.org/
|
8 |
|
9 | ## About
|
10 |
|
11 | This task is a [multi task](types_of_tasks.md), meaning that grunt will automatically iterate over all `qunit` targets if a target is not specified.
|
12 |
|
13 | _Need some help getting started with grunt? Visit the [getting started](getting_started.md) page. And if you're creating your own tasks or helpers, be sure to check out the [types of tasks](types_of_tasks.md) page as well as the [API documentation](api.md)._
|
14 |
|
15 | ### QUnit
|
16 |
|
17 | [QUnit][qunit] is a powerful, easy-to-use, JavaScript test suite. It's used by the jQuery project to test its code and plugins but is capable of testing any generic JavaScript code.
|
18 |
|
19 | ### PhantomJS
|
20 |
|
21 | [PhantomJS](http://www.phantomjs.org/) is a headless WebKit with JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG. PhantomJS is required for the `qunit` task to work.
|
22 |
|
23 | See the [FAQ](faq.md) for instructions on installing PhantomJS.
|
24 |
|
25 |
|
26 | ## A Very Important Note
|
27 | Your `grunt.js` gruntfile **must** contain this code, once and **only** once. If it doesn't, grunt won't work. For the sake of brevity, this "wrapper" code has been omitted from all examples on this page, but it needs to be there.
|
28 |
|
29 | ```javascript
|
30 | module.exports = function(grunt) {
|
31 | // Your grunt code goes in here.
|
32 | };
|
33 | ```
|
34 |
|
35 | ## Project configuration
|
36 |
|
37 | This example shows a brief overview of the [config](api_config.md) properties used by the `qunit` task. For a more in-depth explanation, see the usage examples.
|
38 |
|
39 | ```javascript
|
40 | // Project configuration.
|
41 | grunt.initConfig({
|
42 | // Lists of files or URLs to be unit tested with QUnit.
|
43 | qunit: {}
|
44 | });
|
45 | ```
|
46 |
|
47 | ## Usage examples
|
48 |
|
49 | ### Wildcards
|
50 |
|
51 | In this example, `grunt qunit` will test all `.html` files in the test directory. First, the wildcard is expanded to match each individual file. Then, each matched filename is converted to the appropriate `file://` URI. Finally, [QUnit][qunit] is run for each URI.
|
52 |
|
53 | ```javascript
|
54 | // Project configuration.
|
55 | grunt.initConfig({
|
56 | qunit: {
|
57 | all: ['test/*.html']
|
58 | }
|
59 | });
|
60 | ```
|
61 |
|
62 | With a slight modification, `grunt qunit` will test all `.html` files in the test directory _and all subdirectories_. See the [minimatch](https://github.com/isaacs/minimatch) module's documentation for more details on wildcard patterns.
|
63 |
|
64 | ```javascript
|
65 | // Project configuration.
|
66 | grunt.initConfig({
|
67 | qunit: {
|
68 | all: ['test/**/*.html']
|
69 | }
|
70 | });
|
71 | ```
|
72 |
|
73 | ### Testing via http:// or https://
|
74 |
|
75 | In circumstances where running unit tests from `file://` URIs is inadequate, you can specify `http://` or `https://` URIs instead. If `http://` or `https://` URIs have been specified, those URIs will be passed directly into [QUnit][qunit] as-specified.
|
76 |
|
77 | In this example, `grunt qunit` will test two files, served from the server running at `localhost:8000`.
|
78 |
|
79 | ```javascript
|
80 | // Project configuration.
|
81 | grunt.initConfig({
|
82 | qunit: {
|
83 | all: ['http://localhost:8000/test/foo.html', 'http://localhost:8000/test/bar.html']
|
84 | }
|
85 | });
|
86 | ```
|
87 |
|
88 | _Note: grunt does NOT start a server at `localhost:8000` automatically. While grunt DOES have a [server task](task_server.md) that can be run before the qunit task to serve files statically, it must be started manually..._
|
89 |
|
90 | ### Using the built-in static webserver
|
91 |
|
92 | If a web server isn't running at `localhost:8000`, running `grunt qunit` with `http://localhost:8000/` URIs will fail because grunt won't be able to load those URIs. This can be easily rectified by starting the built-in static web server via the [server task](task_server.md).
|
93 |
|
94 | In this example, running `grunt server qunit` will first start a static web server on `localhost:8000`, with its base path set to the gruntfile's directory. Then, the `qunit` task will be run, requesting the specified URIs from that server.
|
95 |
|
96 | ```javascript
|
97 | // Project configuration.
|
98 | grunt.initConfig({
|
99 | qunit: {
|
100 | all: ['http://localhost:8000/test/foo.html', 'http://localhost:8000/test/bar.html']
|
101 | },
|
102 | server: {
|
103 | port: 8000,
|
104 | base: '.'
|
105 | }
|
106 | });
|
107 |
|
108 | // A convenient task alias.
|
109 | grunt.registerTask('test', 'server qunit');
|
110 | ```
|
111 |
|
112 | _Note: in the above example, an [alias task](types_of_tasks.md) called `test` was created that runs both the `server` and `qunit` tasks._
|
113 |
|
114 | ## Debugging
|
115 |
|
116 | 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.
|
117 |
|
118 | See the [qunit task source](../tasks/qunit.js) for more information.
|