• Stars
    star
    214
  • Rank 184,678 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 12 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Run QUnit tests in Headless Chrome.

grunt-contrib-qunit v7.0.1 Build Status

Run QUnit unit tests in a headless Chrome instance

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, you may install this plugin with this command:

npm install grunt-contrib-qunit --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-contrib-qunit');

Qunit task

Run this task with the grunt qunit command.

You have chosen to write your unit tests using QUnit, you have written a html page which reports the summary and individual details of your unit tests, you are happy with this but realize you miss the ability to have your unit test suite run automatically each time you commit changes to your code.

This is where the grunt-contrib-qunit plugin comes in the play: grunt-contrib-qunit lets you run your tests in the invisible Chrome browser, thus converting your unit test suite into something you can run from a script, a script you can have automatically run on travis-ci (or the Continuous Integration service of your choice) which in turn can alert you of any rule-breaking commit to your code.

You can still monitor the status of your unit tests suite visiting your html test page in your browser, but with grunt-contrib-qunit you can also run the same suite from the command line interface.

This plugin defines one single task: qunit. Configure it in your Gruntfile.js, run it with the grunt qunit command.

Please read about specifying task targets, files and options in the grunt Configuring tasks guide.

When installed by npm, this plugin will automatically download and install a local Chrome binary within the node_modules directory of the Puppeteer library, which is used for launching a Chrome process. If your system already provides an installation of Chrome, you can configure this plugin to use the globally installed executable by specifying a custom executablePath in the puppeteer launch options.
This will almost certainly be needed in order to run Chrome in a CI environment

OS Dependencies

This plugin uses Puppeteer to run tests in a Chrome process. Chrome requires a number of dependencies that must be installed, depending on your OS.
Please see Puppeteer's docs to see the latest docs for what dependencies you need for your OS:

https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md

QUnit version

The plugin supports QUnit 2.2.0 and later. To test with QUnit 1.x, use grunt-contrib-qunit 5.

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|Array
Default: chrome/bridge.js

One or multiple (array) JavaScript file names to inject into the html test page. Defaults to the path of the QUnit-Chrome bridge file.

You may want to inject something different than the provided QUnit-Chrome bridge, or to inject more than just the provided bridge. See the built-in bridge for more information.

httpBase

Type: String
Default: ""

Create URLs for the src files, all src files are prefixed with that base.

console

Type: boolean
Default: true

By default, console.[log|warn|error] output from the Chrome browser will be piped into QUnit console. Set to false to disable this behavior.

urls

Type: Array
Default: []

Absolute http:// or https:// urls to be passed to Chrome. 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.

force

Type: boolean
Default: false

When true, the whole task will not fail when there are individual test failures, or when no assertions for a test have run. This can be set to true when you always want other tasks in the queue to be executed.

summaryOnly

Type: boolean
Default: false

When true, this will suppress the default logging for individually failed tests. Customized logging can be performed by listening to and responding to qunit.log events.

puppeteer

Type: Object
Default: { headless: true, args: [] }

Options passed to puppeteer.launch(). This can used to specify a custom Chrome executable path, run in non-headless mode, specify environment variables for the Chrome process, etc. See the Puppeteer API Reference for a list of launch options.

The default value for args is set from the CHROMIUM_FLAGS environment variable, which in turn defaults to --no-sandbox if the CI environment variable is set.

noGlobals

Type: boolean
Default: false

Fail a test when the global namespace is polluted. See the QUnit.config.noglobals for more information.

Command line options

Filtering by module name: --modules

grunt qunit --modules="foo"

Will run the module foo. You can specify one or multiple, comma-separated modules to run.

Running tests in seeded-random order: --seed

grunt qunit --seed="a-string"

Specify the seed to pass to QUnit, to run tests in random, but deterministic order. See QUnit.config.seed docs for more information.

Usage examples

Wildcards

In this example, grunt qunit:all 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 Chrome (one at a time).

// Project configuration.
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 Chrome (one at a time).

In this example, grunt qunit will test two files, served from the server running at localhost:8000.

// Project configuration.
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 local 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.

// Project configuration.
grunt.initConfig({
  qunit: {
    all: {
      options: {
        urls: [
          'http://localhost:8000/test/foo.html',
          'http://localhost:8000/test/bar.html',
        ]
      }
    }
  },
  connect: {
    server: {
      options: {
        port: 8000,
        base: '.'
      }
    }
  }
});

// This plugin provides the "connect" task.
grunt.loadNpmTasks('grunt-contrib-connect');

// A convenient task alias.
grunt.registerTask('test', ['connect', 'qunit']);

Custom timeouts and Puppeteer options

In the following example, the default timeout value of 5000 is overridden with the value 10000 (timeout values are in milliseconds). Custom options to use when launching Puppeteer can be specified using options.puppeteer, with all property names corresponding directly to options supported by puppeteer.launch(). For example, the following configuration sets the TZ environment variable and invokes a custom Chrome executable at "/usr/bin/chromium"

// Project configuration.
grunt.initConfig({
  qunit: {
    options: {
      timeout: 10000,
      puppeteer: {
        env: {
          TZ: "UTC"
        },
        executablePath: "/usr/bin/chromium"
      }
    },
    all: ['test/**/*.html']
  }
});

Loading QUnit with AMD

When using AMD to load QUnit and your tests, make sure to have a path for the qunit module defined.

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.on.testStart (obj)

  • qunit.on.testEnd (obj)

  • qunit.on.runEnd (obj)

  • qunit.begin

  • qunit.moduleStart (name)

  • qunit.testStart (name)

  • qunit.log (result, actual, expected, message, source)

  • qunit.testDone (name, failed, passed, total, duration)

  • qunit.moduleDone (name, failed, passed, total)

  • qunit.done (failed, passed, total, runtime)

In addition to forwarding QUnit's events, the following events are also emitted by the Grunt plugin:

  • qunit.spawn (url): when Chrome is spawned for a test
  • qunit.fail.load (url): when Chrome could not open the given url
  • qunit.fail.timeout: when a QUnit test times out, usually due to a missing QUnit.start() call
  • qunit.error.onError (err): when a JavaScript execution error occurs

You may listen for these events like so:

grunt.event.on('qunit.spawn', function (url) {
  grunt.log.ok('Running test: ' + url);
});

Release History

  • 2023-07-02   v7.0.1   Fix unexpected bridge activation in unrated iframes.
  • 2023-02-14   v7.0.0   Update to Puppeteer 19. Require Node.js 14 or later. Change actual/expected value to JSON when possible.
  • 2022-10-18   v6.2.1   Fix serialization of assertions on circular objects.
  • 2022-06-26   v6.2.0   Enable --no-sandbox by default for CI environments. Add support for CHROMIUM_FLAGS environment variable.
  • 2022-04-29   v6.1.0   Fix reporting of error details when used with QUnit 2.17 and later. Add Grunt events qunit.on.*, as forwarded from QUnit.on().
  • 2022-04-03   v6.0.0   Update to Puppeteer 9. Require Node.js 12 or later. Require QUnit 2.2.0 or later.
  • 2021-04-18   v5.0.0   Update to Puppeteer 5.
  • 2020-06-17   v4.0.0   Update to Puppeteer 4. Require Node.js 10 or later.
  • 2018-12-29   v3.1.0   Update to puppeteer 1.11.
  • 2018-08-12   v3.0.1   Fix regressions.
  • 2018-07-24   v3.0.0   Switch to using Headless Chromium via Puppeteer, instead of PhantomJS
  • 2017-04-04   v2.0.0   Remove use of QUnit.jsDump Upgrade qunitjs to 2.3.0
  • 2017-02-07   v1.3.0   Add --seed flag for running with seeded-random order. Add support for todo tests.
  • 2016-04-14   v1.2.0   Add support for filtering running modules using command line (--modules) Remove 'grunt.warn' output from error.onError handler, onus now on end user binding to event.
  • 2016-03-11   v1.1.0   Add summaryOnly option. Fix options.force. Fix query string for noGlobals.
  • 2016-02-05   v1.0.1   Change QUnit.jsDump to QUnit.dump.
  • 2016-02-05   v1.0.0   Update grunt-lib-phantomjs to 1.0.0, effectively upgrading to phantomjs 2.x. Remove grunt as a peerDependency.
  • 2015-04-03   v0.7.0   Log PhantomJS errors as warnings.
  • 2015-03-31   v0.6.0   Add noGlobals option, forwarded to QUnit. Report proper exit code to grunt based on failures. Add support for AMD.
  • 2014-07-09   v0.5.2   Added support for reporting the duration of testDone. Other minor fixes.
  • 2014-05-31   v0.5.1   Updates grunt-lib-phantomjs.
  • 2014-05-31   v0.5.0   Add ability to hide PhantomJS console output. Add option for binding phantomjs console to grunt output. Default is true (do bind). Add httpBase option. Only call jsDump.parse() if a test failed.
  • 2014-01-17   v0.4.0   Update grunt-lib-phantomjs to v0.5.0. Explicitly set files to publish to npm. gruntjs/gruntjs.com#65.
  • 2013-09-29   v0.3.0   Add force option. Add qunit.fail.load and qunit.fail.timeout events. Add qunit.error.onError event to oropagate onError from phantomjs. Update grunt-lib-phantomjs to v0.4.0. Update QUnit to v1.12.0. Remove confusing error message.
  • 2013-06-06   v0.2.2   Warn if no assertions ran in a single test. Spaces instead of newlines for clickable URLs. Wrap bridge.js in a IIFE.
  • 2013-04-05   v0.2.1   Update to use PhantomJS 1.9.0. Fixes PhantomJS not found errors.
  • 2013-02-28   v0.2.0   Update to use PhantomJS 1.8.1.
  • 2013-02-15   v0.1.1   First official release for Grunt 0.4.0.
  • 2013-01-18   v0.1.1rc6   Updating grunt/gruntplugin dependencies to rc6.
  • 2013-01-09   v0.1.1rc5   Updating to work with grunt v0.4.0rc5. Switching to this.filesSrc API. Add urls option for specifying absolute test URLs.
  • 2012-10-05   v0.1.0   Work in progress, not yet officially released.

Task submitted by "Cowboy" Ben Alman

This is a generated file.

More Repositories

1

grunt

Grunt: The JavaScript Task Runner
JavaScript
12,273
star
2

grunt-contrib-watch

Run tasks whenever watched files change.
JavaScript
1,982
star
3

grunt-contrib-uglify

Minify files with UglifyJS.
JavaScript
1,483
star
4

grunt-contrib-imagemin

Minify PNG, JPG, GIF and SVG images.
JavaScript
1,214
star
5

grunt-contrib-sass

Compile Sass to CSS.
JavaScript
848
star
6

grunt-contrib-concat

Concatenate files.
JavaScript
811
star
7

grunt-contrib-cssmin

Compress CSS files.
JavaScript
793
star
8

grunt-contrib-copy

Copy files and folders.
JavaScript
726
star
9

grunt-contrib-connect

Start a static web server.
JavaScript
714
star
10

grunt-contrib-jshint

Validate files with JSHint.
JavaScript
709
star
11

grunt-cli

Grunt's command line interface.
JavaScript
707
star
12

grunt-contrib-less

Compile LESS files to CSS.
JavaScript
667
star
13

grunt-contrib-compass

Compile Compass to CSS.
JavaScript
625
star
14

grunt-contrib-clean

Clear files and folders.
JavaScript
512
star
15

grunt-contrib-requirejs

Optimize RequireJS projects using r.js.
JavaScript
504
star
16

grunt-contrib

[DEPRECATED] A collection of common grunt tasks.
JavaScript
475
star
17

grunt-contrib-htmlmin

Minify HTML.
JavaScript
425
star
18

grunt-contrib-jasmine

Run jasmine specs headlessly through Headless Chrome
JavaScript
354
star
19

grunt-contrib-compress

Compress files and folders.
JavaScript
345
star
20

grunt-contrib-coffee

Compile CoffeeScript files to JavaScript.
JavaScript
329
star
21

grunt-contrib-pug

Compile Pug templates.
JavaScript
328
star
22

grunt-contrib-handlebars

Precompile Handlebars templates to JST file.
JavaScript
282
star
23

grunt-contrib-csslint

Lint CSS files.
JavaScript
240
star
24

grunt-contrib-livereload

Reload assets live in the browser.
JavaScript
202
star
25

grunt-init

Generate project scaffolding from a template.
JavaScript
193
star
26

grunt-contrib-stylus

Compile Stylus files to CSS.
JavaScript
174
star
27

grunt-init-gruntfile

Create a basic Gruntfile with grunt-init.
JavaScript
156
star
28

gruntjs.com

Grunt website
Less
155
star
29

grunt-contrib-jst

Compile underscore templates to JST file.
JavaScript
113
star
30

grunt-lib-phantomjs

Grunt and PhantomJS, sitting in a tree.
JavaScript
93
star
31

grunt-next

Grunt v1.0 alpha
JavaScript
84
star
32

grunt-docs

Grunt documentation. https://github.com/gruntjs/gruntjs.com
79
star
33

grunt-init-gruntplugin

Create a gruntplugin module with grunt-init, including Nodeunit unit tests.
JavaScript
77
star
34

grunt-contrib-yuidoc

Compile YUIDoc Documentation.
JavaScript
75
star
35

grunt-contrib-nodeunit

Run Nodeunit unit tests.
JavaScript
70
star
36

grunt-init-jquery

Create a jQuery plugin with grunt-init, including QUnit unit tests.
JavaScript
58
star
37

grunt-contrib-symlink

Create symbolic links.
JavaScript
56
star
38

grunt-init-node

Create a Node.js module with grunt-init, including Nodeunit unit tests.
JavaScript
55
star
39

grunt-contrib-bump

A work-in-progress Grunt plugin for bumping a version number in JSON files.
JavaScript
52
star
40

grunt-init-commonjs

Create a commonjs module with grunt-init, including Nodeunit unit tests.
JavaScript
17
star
41

grunt-contrib-internal

Internal tasks for managing the grunt-contrib project.
JavaScript
16
star
42

grunt-init-gruntfile-sample

This is sample output generated by the grunt-init "gruntfile" template.
JavaScript
14
star
43

grunt-init-jquery-sample

This is sample output generated by the grunt-init "jquery" template.
JavaScript
12
star
44

grunt-lib-contrib

Common functionality shared across grunt-contrib tasks.
JavaScript
12
star
45

example-subgrunt

Run a Gruntfile in multiple subdirectories.
JavaScript
11
star
46

grunt-contrib-mincss

Renamed to grunt-contrib-cssmin.
9
star
47

grunt-init-gruntplugin-sample

This is sample output generated by the grunt-init "gruntplugin" template.
JavaScript
9
star
48

grunt-init-node-sample

This is sample output generated by the grunt-init "node" template.
JavaScript
8
star
49

grunt-known-options

The known options used in Grunt
JavaScript
7
star
50

grunt-init-commonjs-sample

This is sample output generated by the grunt-init "commonjs" template.
JavaScript
7
star
51

clone-repos

Quickly clone all gruntjs repos (for grunt development)
Ruby
6
star
52

grunt-lib-legacyhelpers

Some old grunt helpers provided for backwards compatability.
JavaScript
6
star
53

grunt-legacy-util

deprecated utility methods
JavaScript
5
star
54

grunt-legacy-log

The Grunt logger.
JavaScript
5
star
55

rfcs

RFCs for changes to Grunt
4
star
56

grunt-legacy-log-utils

Static methods for the Grunt 0.4.x logger.
JavaScript
4
star
57

grunt-plugin-list

[Deprecated] Generates a list of all grunt plugins as json
JavaScript
4
star
58

grunt-legacy-event-logger

Event logger for Grunt legacy libs.
JavaScript
3
star
59

grunt-legacy-config

Grunt's config methods, as a standalone library.
JavaScript
2
star
60

grunt-legacy-option

Grunt's option methods, as a standalone library.
JavaScript
1
star
61

grunt-legacy-cli

Grunt's CLI methods, as a standalone library.
JavaScript
1
star
62

grunt-legacy-task

Grunt's task methods, as a standalone library.
JavaScript
1
star