Automatically run client-side mocha specs via grunt/mocha/PhantomJS
This plugin requires Grunt
~0.4.0. Use a
0.1.x tag of this plugin to use with Grunt
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-mocha --save-dev
Run this task with the
grunt mocha command.
There are a number of options available. Please review the minimatch options here.
This defines which HTML spec files to run using PhantomJS. These are the same files you would open to run tests in a browser.
Write reporter output to a file. Useful if you need a file to feed your CI bot.
mocha:test:src: 'tests/**/*.html'reporter: 'XUnit'dest: './test/output/xunit.out'
NOTE: This will probably default to true in
grunt-mocha injects a script into the PhantomJS instance that loads your HTML spec files. The file sets up a reporter and listeners so the output can be output in the command line. This option will call
mocha.run() after the script is injected, ensuring that the proper listeners are setup.
You may want to set this to false if your files are loaded asynchronously via AMD and call
mocha.run in your own callback.
In HTML spec:
<!-- run mocha after all test are loaded -->
mocha:test:src: 'tests/**/*.html'options:run: true
Instead of files, hit these URLs. Usually used in conjunction with the connect task to spin up a server for testing.
connect:server:options:port: 8888base: '.'mocha:test:options:urls: ''
grunt connect mocha
PhantomJS timeout in milliseconds. If nothing happens within 5 seconds, exit.
mocha:test:src: 'tests/**/*.html'options:timeout: 10000
grunt.warn and exit the grunt task on the first failed test. This only calls
grunt.warn after the entire spec file is finished.
mocha:test:src: 'tests/**/*.html'options:bail: true
console.log calls from PhantomJS to the command line. Only used for very quick and dirty debugging. It is highly recommended that you open the failing spec file in a browser so you can use much richer debugging tools.
mocha:test:src: 'tests/**/*.html'options:log: true
Fail and output script errors.
mocha:test:src: 'tests/**/*.html'options:logErrors: true
A mocha options simple object. Very few options are currently supported. Actually, I think
grep is the only one.
mocha:test:src: 'tests/**/*.html'options:mocha:grep: 'router*'
The reporter to use. Note: XUnit and those types of reporters should probably use the
mocha:test:files: 'tests/**/*.html'options:reporter: 'Nyan'
Custom reporter example: Example:
mocha:test:files: 'tests/**/*.html'optionsreporter: './path/to/custom/reporter' // included via require
The PhantomJS -> Grunt superdimensional conduit uses
alert. If you have disabled or aliased alert in your app, this won't work. I have conveniently set a global
window so you can conditionally override alert in your app.
To take a screenshot:
var container = 0; // or any DOM nodeconsole;
To change the resolution of the viewport:
run: trueoption (see this task's Gruntfile.js for details);
userAgentin a test html file and run tests only in a real browser (see test2.html for details).
tasks/phantomjs after you include
mocha.js and run
mocha.setup in your HTML file. The helper will override
mocha.setup if it detects PhantomJS. See test.html.
Mocha must be included via script tag in the header. There is no need to load Mocha via AMD. You may load other testing libs via AMD if that gives you a fuzzy feeling.
Example setup with AMD (advanced): https://gist.github.com/2655876
Copyright (c) 2013 Kelly Miyashiro Licensed under the MIT license.