TDD with Browserify, Mocha, PhantomJS and WebDriver
npm install bant-mochify[![Build Status]](https://travis-ci.org/mantoni/mochify.js)
[![SemVer]](http://semver.org)
[![License]](https://github.com/mantoni/mochify.js/blob/master/LICENSE)
TDD with Browserify, Mocha, PhantomJS and WebDriver
- Fast roundtrip
- No test HTML page
- No server
- Selenium WebDriver & SauceLabs support
- Code coverage with [coverify][]
- Code coverage with [istanbul][] using
[mochify-istanbul][] plugin
- Short stack traces with relative paths
- Works with most Mocha reporters
This will install Mochify in your current project and add it to thedevDependencies:
```
npm install mochify --save-dev
- Install Phantom.JS: npm install phantomjs -g or download fromphantomjs
- Make sure that the executable is in your PATH or use--phantomjs
Configure "scripts" in your package.json so that your project ships with the
testing infrastructure:
``
"devDependencies" : {
"mochify" : "*"
},
"scripts" : {
"start" : "mochify --watch",
"phantom" : "mochify",
"wd" : "mochify --wd",
"cover" : "mochify --cover",
"test" : "npm run phantom && npm run wd && npm run cover"
}
To run from the command line, either run npm install mochify -g to havemochify available globally, or from within your project directory run:
``
node_modules/.bin/mochify
Browserifies ./test/*.js, decorated with a [Mocha test runner][], runs it in
PhantomJS with [phantomic][] and pass the output back to your console. Cleans
up your stack traces by mapping back to the original sources and throws out all
the lines from the test framework.
Run mochify --help to see all available options.
- --watch or -w use [watchify][] to watch your files and run the tests on--reporter
change.
- or -R changes the Mocha reporter (see further down).--grep
- sets the Mocha grep option.--invert
- sets the Mocha grep invert flag.--recursive
- include sub directories.--ui
- or -U changes the Mocha UI. Defaults to 'bdd'.--timeout
- or -t changes the Mocha timeout. Defaults to 2000.--require
- or -r requires the given module.--debug
- launches the WebKit debugger.--port
- uses a specific port for the PhantomJS server.--phantomjs
- uses a specific PhantomJS executable. If not specified,phantomjs
is expected on the $PATH.--web-security
- enables PhantomJS web security and forbids cross-domain XHR--cover
(default is true)
- checks code coverage with [coverify][].--node
- runs test cases on node (useful with --cover).--wd
- use [min-webdriver][] to run the tests in multiple real browsers.--url
- (only with --wd) runs the tests in the context of the given URL.--consolify output.html
- generate a standalone HTML page with [consolify][].--transform
- specifies a Browserify transform to add. Can be specified--plugin
multiple times. Options can be passed with [subargs][].
- specifies a Browserify plugin to add. Can be specified multiple--extension
times. Options can be passed with [subargs][].
- search for files with the extension in "require" statements.--yields
- or -y changes the yield interval to allow pending I/O to happen.--version
- or -v shows the Mochify version number.--help
- or -h shows usage and all available options.
- Download the «Selenium Server Standalone» JAR from here:
- Except for Firefox, you will also need drivers for each browser.
- The driver for Google Chrome can be found here:
- Put the drivers in the same directory as the JAR file and run:
``
java -jar selenium-server-standalone-2.39.0.jar
Create .min-wd in your project root:
``
{
"hostname" : "localhost",
"port" : 4444,
"browsers" : [{
"name" : "internet explorer",
"version" : "9"
}, {
"name" : "chrome"
}, {
"name" : "firefox"
}]
}
That's it! Now mochify --wd will run your Mocha test cases in the configured-g
browsers simultaniously. If you installed mochify without , you will havenode_modules/.bin/mochify --wd
to run .
Export your SauceLabs credentials:
``
export SAUCE_USERNAME="your-user-name"
export SAUCE_ACCESS_KEY="your-access-key"
Enable SauceLabs in your .min-wd file:
``
{
"sauceLabs": true
}
For more information about Selenium WebDriver and SourceLabs support can be
found on the [min-webdriver][] project page.
Mocha reporters known to work:
- min
- dot (default)
- list
- spec
- tap
- json
- doc
- xunit
- markdown
- landing
Note: Consuming the output of a machine readable reporter may not work as
expected with --wd.
`js
var mochify = require('mochify');
mochify('./test/*.js', {
reporter : 'tap',
cover : true
}).bundle();
`
- mochify() uses default settings and runs tests in ./test/*.jsmochify(paths)
- specifies the paths, a space delimited list of globsmochify(opts)
- configures options as described belowmochify(paths, opts)
- combines custom paths and options
All long form command line options can be used. E.g. --node can be configured{ node : true }
as , --reporter tab as { reporter : 'tab' } and so on.
Additional API options:
- output a stream that receives the test output (e.g. [through2][])
Install the [mochify-istanbul][] plugin:
`bash`
$ npm install mochify-istanbul --save-dev
Using a package.json script that can be run with npm run cover:
``
"scripts" : {
"cover" : "mochify --plugin [ mochify-istanbul --report cobertura ]"
}
Using the API:
`js
var mochify = require('mochify');
var istanbul = require('mochify-istanbul');
mochify().plugin(istanbul, {
report: ['text', 'html', 'text-summary']
}).bundle();
``
- Node 0.10, 0.12
- v2.5+
- Browserify 9.x
- v2.4
- Browserify 8.x
- v2.3
- Browserify 7.x
- v2.0, v2.1, v2.2
- Browserify 6.x
- Mocha 2.x
- v1.x
- Browserify 5.x
- Mocha 1.x
- v0.x
- Browserify 4.x
MIT
[Build Status]: http://img.shields.io/travis/mantoni/mochify.js.svg
[SemVer]: http://img.shields.io/:semver-%E2%9C%93-brightgreen.svg
[License]: http://img.shields.io/npm/l/mochify.svg
[watchify]: https://github.com/substack/watchify
[coverify]: https://github.com/substack/coverify
[istanbul]: https://github.com/gotwarlost/istanbul
[mochify-istanbul]: https://github.com/ferlores/mochify-istanbul
[min-webdriver]: https://github.com/mantoni/min-webdriver
[Mocha test runner]: https://github.com/mantoni/mocaccino.js
[phantomic]: https://github.com/mantoni/phantomic
[consolify]: https://github.com/mantoni/consolify
[subargs]: https://github.com/substack/subarg
[through2]: https://github.com/rvagg/through2