123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- /*!
- * mocha
- * Copyright(c) 2011 TJ Holowaychuk <[email protected]>
- * MIT Licensed
- */
- /**
- * Module dependencies.
- */
- var path = require('path')
- , utils = require('./utils');
- /**
- * Expose `Mocha`.
- */
- exports = module.exports = Mocha;
- /**
- * Expose internals.
- */
- exports.utils = utils;
- exports.interfaces = require('./interfaces');
- exports.reporters = require('./reporters');
- exports.Runnable = require('./runnable');
- exports.Context = require('./context');
- exports.Runner = require('./runner');
- exports.Suite = require('./suite');
- exports.Hook = require('./hook');
- exports.Test = require('./test');
- /**
- * Return image `name` path.
- *
- * @param {String} name
- * @return {String}
- * @api private
- */
- function image(name) {
- return __dirname + '/../images/' + name + '.png';
- }
- /**
- * Setup mocha with `options`.
- *
- * Options:
- *
- * - `ui` name "bdd", "tdd", "exports" etc
- * - `reporter` reporter instance, defaults to `mocha.reporters.Dot`
- * - `globals` array of accepted globals
- * - `timeout` timeout in milliseconds
- * - `bail` bail on the first test failure
- * - `slow` milliseconds to wait before considering a test slow
- * - `ignoreLeaks` ignore global leaks
- * - `grep` string or regexp to filter tests with
- *
- * @param {Object} options
- * @api public
- */
- function Mocha(options) {
- options = options || {};
- this.files = [];
- this.options = options;
- this.grep(options.grep);
- this.suite = new exports.Suite('', new exports.Context);
- this.ui(options.ui);
- this.bail(options.bail);
- this.reporter(options.reporter);
- if (typeof options.timeout === 'number') this.timeout(options.timeout);
- if (options.slow) this.slow(options.slow);
- }
- /**
- * Enable or disable bailing on the first failure.
- *
- * @param {Boolean} [bail]
- * @api public
- */
- Mocha.prototype.bail = function(bail){
- if (0 == arguments.length) bail = true;
- this.suite.bail(bail);
- return this;
- };
- /**
- * Add test `file`.
- *
- * @param {String} file
- * @api public
- */
- Mocha.prototype.addFile = function(file){
- this.files.push(file);
- return this;
- };
- /**
- * Set reporter to `reporter`, defaults to "dot".
- *
- * @param {String|Function} reporter name or constructor
- * @api public
- */
- Mocha.prototype.reporter = function(reporter){
- if ('function' == typeof reporter) {
- this._reporter = reporter;
- } else {
- reporter = reporter || 'dot';
- try {
- this._reporter = require('./reporters/' + reporter);
- } catch (err) {
- this._reporter = require(reporter);
- }
- if (!this._reporter) throw new Error('invalid reporter "' + reporter + '"');
- }
- return this;
- };
- /**
- * Set test UI `name`, defaults to "bdd".
- *
- * @param {String} bdd
- * @api public
- */
- Mocha.prototype.ui = function(name){
- name = name || 'bdd';
- this._ui = exports.interfaces[name];
- if (!this._ui) throw new Error('invalid interface "' + name + '"');
- this._ui = this._ui(this.suite);
- return this;
- };
- /**
- * Load registered files.
- *
- * @api private
- */
- Mocha.prototype.loadFiles = function(fn){
- var self = this;
- var suite = this.suite;
- var pending = this.files.length;
- this.files.forEach(function(file){
- file = path.resolve(file);
- suite.emit('pre-require', global, file, self);
- suite.emit('require', require(file), file, self);
- suite.emit('post-require', global, file, self);
- --pending || (fn && fn());
- });
- };
- /**
- * Enable growl support.
- *
- * @api private
- */
- Mocha.prototype._growl = function(runner, reporter) {
- var notify = require('growl');
- runner.on('end', function(){
- var stats = reporter.stats;
- if (stats.failures) {
- var msg = stats.failures + ' of ' + runner.total + ' tests failed';
- notify(msg, { name: 'mocha', title: 'Failed', image: image('error') });
- } else {
- notify(stats.passes + ' tests passed in ' + stats.duration + 'ms', {
- name: 'mocha'
- , title: 'Passed'
- , image: image('ok')
- });
- }
- });
- };
- /**
- * Add regexp to grep, if `re` is a string it is escaped.
- *
- * @param {RegExp|String} re
- * @return {Mocha}
- * @api public
- */
- Mocha.prototype.grep = function(re){
- this.options.grep = 'string' == typeof re
- ? new RegExp(utils.escapeRegexp(re))
- : re;
- return this;
- };
- /**
- * Invert `.grep()` matches.
- *
- * @return {Mocha}
- * @api public
- */
- Mocha.prototype.invert = function(){
- this.options.invert = true;
- return this;
- };
- /**
- * Ignore global leaks.
- *
- * @param {Boolean} ignore
- * @return {Mocha}
- * @api public
- */
- Mocha.prototype.ignoreLeaks = function(ignore){
- this.options.ignoreLeaks = !!ignore;
- return this;
- };
- /**
- * Enable global leak checking.
- *
- * @return {Mocha}
- * @api public
- */
- Mocha.prototype.checkLeaks = function(){
- this.options.ignoreLeaks = false;
- return this;
- };
- /**
- * Enable growl support.
- *
- * @return {Mocha}
- * @api public
- */
- Mocha.prototype.growl = function(){
- this.options.growl = true;
- return this;
- };
- /**
- * Ignore `globals` array or string.
- *
- * @param {Array|String} globals
- * @return {Mocha}
- * @api public
- */
- Mocha.prototype.globals = function(globals){
- this.options.globals = (this.options.globals || []).concat(globals);
- return this;
- };
- /**
- * Set the timeout in milliseconds.
- *
- * @param {Number} timeout
- * @return {Mocha}
- * @api public
- */
- Mocha.prototype.timeout = function(timeout){
- this.suite.timeout(timeout);
- return this;
- };
- /**
- * Set slowness threshold in milliseconds.
- *
- * @param {Number} slow
- * @return {Mocha}
- * @api public
- */
- Mocha.prototype.slow = function(slow){
- this.suite.slow(slow);
- return this;
- };
- /**
- * Makes all tests async (accepting a callback)
- *
- * @return {Mocha}
- * @api public
- */
- Mocha.prototype.asyncOnly = function(){
- this.options.asyncOnly = true;
- return this;
- };
- /**
- * Run tests and invoke `fn()` when complete.
- *
- * @param {Function} fn
- * @return {Runner}
- * @api public
- */
- Mocha.prototype.run = function(fn){
- if (this.files.length) this.loadFiles();
- var suite = this.suite;
- var options = this.options;
- var runner = new exports.Runner(suite);
- var reporter = new this._reporter(runner);
- runner.ignoreLeaks = false !== options.ignoreLeaks;
- runner.asyncOnly = options.asyncOnly;
- if (options.grep) runner.grep(options.grep, options.invert);
- if (options.globals) runner.globals(options.globals);
- if (options.growl) this._growl(runner, reporter);
- return runner.run(fn);
- };
|