Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

bumping to 1.21.1 for missing build

Christopher Hiller12009bc91b4d
diff --git a/bower.json b/bower.json
index ae9d9d2e..ff11b98e 100644
--- a/bower.json
+++ b/bower.json
@@ -1,6 +1,6 @@
{
"name": "mocha",
- "version": "1.21.0",
+ "version": "1.21.1",
"main": [
"mocha.js",
"mocha.css"
diff --git a/component.json b/component.json
index 55ec3a9d..3112d474 100644
--- a/component.json
+++ b/component.json
@@ -1,6 +1,6 @@
{
"name": "mocha",
- "version": "1.21.0",
+ "version": "1.21.1",
"repo": "visionmedia/mocha",
"description": "simple, flexible, fun test framework",
"keywords": [
diff --git a/mocha.js b/mocha.js
index 19edc87d..ab843050 100644
--- a/mocha.js
+++ b/mocha.js
@@ -788,10 +788,25 @@ Context.prototype.runnable = function(runnable){
*/
Context.prototype.timeout = function(ms){
+ if (arguments.length === 0) return this.runnable().timeout();
this.runnable().timeout(ms);
return this;
};
+/**
+ * Set test timeout `enabled`.
+ *
+ * @param {Boolean} enabled
+ * @return {Context} self
+ * @api private
+ */
+
+Context.prototype.enableTimeouts = function (enabled) {
+ this.runnable().enableTimeouts(enabled);
+ return this;
+};
+
+
/**
* Set test slowness threshold `ms`.
*
@@ -951,6 +966,7 @@ module.exports = function(suite){
context.describe = context.context = function(title, fn){
var suite = Suite.create(suites[0], title);
+ suite.file = file;
suites.unshift(suite);
fn.call(suite);
suites.shift();
@@ -991,6 +1007,7 @@ module.exports = function(suite){
var suite = suites[0];
if (suite.pending) var fn = null;
var test = new Test(title, fn);
+ test.file = file;
suite.addTest(test);
return test;
};
@@ -1051,7 +1068,7 @@ module.exports = function(suite){
suite.on('require', visit);
- function visit(obj) {
+ function visit(obj, file) {
var suite;
for (var key in obj) {
if ('function' == typeof obj[key]) {
@@ -1070,7 +1087,9 @@ module.exports = function(suite){
suites[0].afterEach(fn);
break;
default:
- suites[0].addTest(new Test(key, fn));
+ var test = new Test(key, fn);
+ test.file = file;
+ suites[0].addTest(test);
}
} else {
var suite = Suite.create(suites[0], key);
@@ -1172,6 +1191,7 @@ module.exports = function(suite){
context.suite = function(title){
if (suites.length > 1) suites.shift();
var suite = Suite.create(suites[0], title);
+ suite.file = file;
suites.unshift(suite);
return suite;
};
@@ -1193,6 +1213,7 @@ module.exports = function(suite){
context.test = function(title, fn){
var test = new Test(title, fn);
+ test.file = file;
suites[0].addTest(test);
return test;
};
@@ -1299,6 +1320,7 @@ module.exports = function(suite){
context.suite = function(title, fn){
var suite = Suite.create(suites[0], title);
+ suite.file = file;
suites.unshift(suite);
fn.call(suite);
suites.shift();
@@ -1335,6 +1357,7 @@ module.exports = function(suite){
var suite = suites[0];
if (suite.pending) var fn = null;
var test = new Test(title, fn);
+ test.file = file;
suite.addTest(test);
return test;
};
@@ -1373,7 +1396,15 @@ require.register("mocha.js", function(module, exports, require){
*/
var path = require('browser/path')
- , utils = require('./utils');
+ , utils = require('./utils')
+ , join = path.join
+ , cwd = process.cwd();
+
+/**
+ * Add node_modules directory to use local modules for ui and reporters.
+ */
+
+module.paths.push(cwd, join(cwd, 'node_modules'));
/**
* Expose `Mocha`.
@@ -1413,7 +1444,7 @@ function image(name) {
* Options:
*
* - `ui` name "bdd", "tdd", "exports" etc
- * - `reporter` reporter instance, defaults to `mocha.reporters.Dot`
+ * - `reporter` reporter instance, defaults to `mocha.reporters.spec`
* - `globals` array of accepted globals
* - `timeout` timeout in milliseconds
* - `bail` bail on the first test failure
@@ -1436,6 +1467,7 @@ function Mocha(options) {
this.reporter(options.reporter);
if (null != options.timeout) this.timeout(options.timeout);
this.useColors(options.useColors)
+ if (options.enableTimeouts !== null) this.enableTimeouts(options.enableTimeouts);
if (options.slow) this.slow(options.slow);
this.suite.on('pre-require', function (context) {
@@ -1480,7 +1512,7 @@ Mocha.prototype.addFile = function(file){
};
/**
- * Set reporter to `reporter`, defaults to "dot".
+ * Set reporter to `reporter`, defaults to "spec".
*
* @param {String|Function} reporter name or constructor
* @api public
@@ -1490,7 +1522,7 @@ Mocha.prototype.reporter = function(reporter){
if ('function' == typeof reporter) {
this._reporter = reporter;
} else {
- reporter = reporter || 'dot';
+ reporter = reporter || 'spec';
var _reporter;
try { _reporter = require('./reporters/' + reporter); } catch (err) {};
if (!_reporter) try { _reporter = require(reporter); } catch (err) {};
@@ -1696,6 +1728,21 @@ Mocha.prototype.slow = function(slow){
return this;
};
+/**
+ * Enable timeouts.
+ *
+ * @param {Boolean} enabled
+ * @return {Mocha}
+ * @api public
+ */
+
+Mocha.prototype.enableTimeouts = function(enabled) {
+ this.suite.enableTimeouts(arguments.length && enabled !== undefined
+ ? enabled
+ : true);
+ return this
+};
+
/**
* Makes all tests async (accepting a callback)
*
@@ -2031,8 +2078,8 @@ exports.list = function(failures){
// explicitly show diff
if (err.showDiff && sameType(actual, expected)) {
escape = false;
- err.actual = actual = stringify(canonicalize(actual));
- err.expected = expected = stringify(canonicalize(expected));
+ err.actual = actual = utils.stringify(actual);
+ err.expected = expected = utils.stringify(expected);
}
// actual / expected diff
@@ -2294,53 +2341,6 @@ function colorLines(name, str) {
}).join('\n');
}
-/**
- * Stringify `obj`.
- *
- * @param {Object} obj
- * @return {String}
- * @api private
- */
-
-function stringify(obj) {
- if (obj instanceof RegExp) return obj.toString();
- return JSON.stringify(obj, null, 2);
-}
-
-/**
- * Return a new object that has the keys in sorted order.
- * @param {Object} obj
- * @return {Object}
- * @api private
- */
-
- function canonicalize(obj, stack) {
- stack = stack || [];
-
- if (utils.indexOf(stack, obj) !== -1) return obj;
-
- var canonicalizedObj;
-
- if ('[object Array]' == {}.toString.call(obj)) {
- stack.push(obj);
- canonicalizedObj = utils.map(obj, function(item) {
- return canonicalize(item, stack);
- });
- stack.pop();
- } else if (typeof obj === 'object' && obj !== null) {
- stack.push(obj);
- canonicalizedObj = {};
- utils.forEach(utils.keys(obj).sort(), function(key) {
- canonicalizedObj[key] = canonicalize(obj[key], stack);
- });
- stack.pop();
- } else {
- canonicalizedObj = obj;
- }
-
- return canonicalizedObj;
- }
-
/**
* Check that a / b have the same type.
*
@@ -2356,7 +2356,6 @@ function sameType(a, b) {
return a == b;
}
-
}); // module: reporters/base.js
require.register("reporters/doc.js", function(module, exports, require){
@@ -2415,6 +2414,13 @@ function Doc(runner) {
var code = utils.escape(utils.clean(test.fn.toString()));
console.log('%s <dd><pre><code>%s</code></pre></dd>', indent(), code);
});
+
+ runner.on('fail', function(test, err){
+ console.log('%s <dt class="error">%s</dt>', indent(), utils.escape(test.title));
+ var code = utils.escape(utils.clean(test.fn.toString()));
+ console.log('%s <dd class="error"><pre><code>%s</code></pre></dd>', indent(), code);
+ console.log('%s <dd class="error">%s</dd>', indent(), utils.escape(err));
+ });
}
}); // module: reporters/doc.js
@@ -2447,13 +2453,14 @@ function Dot(runner) {
var self = this
, stats = this.stats
, width = Base.window.width * .75 | 0
- , n = 0;
+ , n = -1;
runner.on('start', function(){
process.stdout.write('\n ');
});
runner.on('pending', function(test){
+ if (++n % width == 0) process.stdout.write('\n ');
process.stdout.write(color('pending', Base.symbols.dot));
});
@@ -2486,6 +2493,7 @@ F.prototype = Base.prototype;
Dot.prototype = new F;
Dot.prototype.constructor = Dot;
+
}); // module: reporters/dot.js
require.register("reporters/html-cov.js", function(module, exports, require){
@@ -3107,10 +3115,10 @@ function JSONReporter(runner) {
runner.on('end', function(){
var obj = {
- stats: self.stats
- , tests: tests.map(clean)
- , failures: failures.map(clean)
- , passes: passes.map(clean)
+ stats: self.stats,
+ tests: tests.map(clean),
+ failures: failures.map(clean),
+ passes: passes.map(clean)
};
process.stdout.write(JSON.stringify(obj, null, 2));
@@ -3128,11 +3136,13 @@ function JSONReporter(runner) {
function clean(test) {
return {
- title: test.title
- , fullTitle: test.fullTitle()
- , duration: test.duration
+ title: test.title,
+ fullTitle: test.fullTitle(),
+ duration: test.duration,
+ err: test.err
}
}
+
}); // module: reporters/json.js
require.register("reporters/landing.js", function(module, exports, require){
@@ -3720,7 +3730,6 @@ NyanCat.prototype.constructor = NyanCat;
}); // module: reporters/nyan.js
require.register("reporters/progress.js", function(module, exports, require){
-
/**
* Module dependencies.
*/
@@ -3758,7 +3767,8 @@ function Progress(runner, options) {
, width = Base.window.width * .50 | 0
, total = runner.total
, complete = 0
- , max = Math.max;
+ , max = Math.max
+ , lastN = -1;
// default chars
options.open = options.open || '[';
@@ -3781,6 +3791,12 @@ function Progress(runner, options) {
, n = width * percent | 0
, i = width - n;
+ if (lastN === n && !options.verbose) {
+ // Don't re-render the line if it hasn't changed
+ return;
+ }
+ lastN = n;
+
cursor.CR();
process.stdout.write('\u001b[J');
process.stdout.write(color('progress', ' ' + options.open));
@@ -4071,8 +4087,7 @@ function test(test) {
if ('failed' == test.state) {
var err = test.err;
- attrs.message = escape(err.message);
- console.log(tag('testcase', attrs, false, tag('failure', attrs, false, cdata(err.stack))));
+ console.log(tag('testcase', attrs, false, tag('failure', {}, false, cdata(escape(err.message) + "\n" + err.stack))));
} else if (test.pending) {
console.log(tag('testcase', attrs, false, tag('skipped', {}, true)));
} else {
@@ -4155,6 +4170,7 @@ function Runnable(title, fn) {
this.sync = ! this.async;
this._timeout = 2000;
this._slow = 75;
+ this._enableTimeouts = true;
this.timedOut = false;
}
@@ -4201,6 +4217,21 @@ Runnable.prototype.slow = function(ms){
return this;
};
+/**
+ * Set and & get timeout `enabled`.
+ *
+ * @param {Boolean} enabled
+ * @return {Runnable|Boolean} enabled or self
+ * @api private
+ */
+
+Runnable.prototype.enableTimeouts = function(enabled){
+ if (arguments.length === 0) return this._enableTimeouts;
+ debug('enableTimeouts %s', enabled);
+ this._enableTimeouts = enabled;
+ return this;
+};
+
/**
* Return the full title generated by recursively
* concatenating the parent's full title.
@@ -4249,6 +4280,7 @@ Runnable.prototype.resetTimeout = function(){
var self = this;
var ms = this.timeout() || 1e9;
+ if (!this._enableTimeouts) return;
this.clearTimeout();
this.timer = setTimeout(function(){
self.callback(new Error('timeout of ' + ms + 'ms exceeded'));
@@ -4275,7 +4307,6 @@ Runnable.prototype.globals = function(arr){
Runnable.prototype.run = function(fn){
var self = this
- , ms = this.timeout()
, start = new Date
, ctx = this.ctx
, finished
@@ -4292,11 +4323,13 @@ Runnable.prototype.run = function(fn){
// finished
function done(err) {
+ var ms = self.timeout();
if (self.timedOut) return;
if (finished) return multiple(err);
self.clearTimeout();
self.duration = new Date - start;
finished = true;
+ if (!err && self.duration > ms && self._enableTimeouts) err = new Error('timeout of ' + ms + 'ms exceeded');
fn(err);
}
@@ -4310,7 +4343,13 @@ Runnable.prototype.run = function(fn){
try {
this.fn.call(ctx, function(err){
if (err instanceof Error || toString.call(err) === "[object Error]") return done(err);
- if (null != err) return done(new Error('done() invoked with non-Error: ' + err));
+ if (null != err) {
+ if (Object.prototype.toString.call(err) === '[object Object]') {
+ return done(new Error('done() invoked with non-Error: ' + JSON.stringify(err)));
+ } else {
+ return done(new Error('done() invoked with non-Error: ' + err));
+ }
+ }
done();
});
} catch (err) {
@@ -4338,7 +4377,13 @@ Runnable.prototype.run = function(fn){
var result = fn.call(ctx);
if (result && typeof result.then === 'function') {
self.resetTimeout();
- result.then(function(){ done() }, done);
+ result
+ .then(function() {
+ done()
+ },
+ function(reason) {
+ done(reason || new Error('Promise rejected with no or falsy reason'))
+ });
} else {
done();
}
@@ -4515,7 +4560,6 @@ Runner.prototype.checkGlobals = function(test){
var ok = this._globals;
var globals = this.globalProps();
- var isNode = process.kill;
var leaks;
if (test) {
@@ -4887,7 +4931,13 @@ Runner.prototype.runSuite = function(suite, fn){
*/
Runner.prototype.uncaught = function(err){
- debug('uncaught exception %s', err.message);
+ if (err) {
+ debug('uncaught exception %s', err.message);
+ } else {
+ debug('uncaught undefined exception');
+ err = new Error('Catched undefined error, did you throw without specifying what?');
+ }
+
var runnable = this.currentRunnable;
if (!runnable || 'failed' == runnable.state) return;
runnable.clearTimeout();
@@ -5067,7 +5117,7 @@ exports.create = function(parent, title){
function Suite(title, parentContext) {
this.title = title;
- var context = function () {};
+ var context = function() {};
context.prototype = parentContext;
this.ctx = new context();
this.suites = [];
@@ -5079,6 +5129,7 @@ function Suite(title, parentContext) {
this._afterAll = [];
this.root = !title;
this._timeout = 2000;
+ this._enableTimeouts = true;
this._slow = 75;
this._bail = false;
}
@@ -5105,6 +5156,7 @@ Suite.prototype.clone = function(){
debug('clone');
suite.ctx = this.ctx;
suite.timeout(this.timeout());
+ suite.enableTimeouts(this.enableTimeouts());
suite.slow(this.slow());
suite.bail(this.bail());
return suite;
@@ -5126,6 +5178,21 @@ Suite.prototype.timeout = function(ms){
return this;
};
+/**
+ * Set timeout `enabled`.
+ *
+ * @param {Boolean} enabled
+ * @return {Suite|Boolean} self or enabled
+ * @api private
+ */
+
+Suite.prototype.enableTimeouts = function(enabled){
+ if (arguments.length === 0) return this._enableTimeouts;
+ debug('enableTimeouts %s', enabled);
+ this._enableTimeouts = enabled;
+ return this;
+}
+
/**
* Set slow `ms` or short-hand such as "2s".
*
@@ -5176,6 +5243,7 @@ Suite.prototype.beforeAll = function(title, fn){
var hook = new Hook(title, fn);
hook.parent = this;
hook.timeout(this.timeout());
+ hook.enableTimeouts(this.enableTimeouts());
hook.slow(this.slow());
hook.ctx = this.ctx;
this._beforeAll.push(hook);
@@ -5202,6 +5270,7 @@ Suite.prototype.afterAll = function(title, fn){
var hook = new Hook(title, fn);
hook.parent = this;
hook.timeout(this.timeout());
+ hook.enableTimeouts(this.enableTimeouts());
hook.slow(this.slow());
hook.ctx = this.ctx;
this._afterAll.push(hook);
@@ -5228,6 +5297,7 @@ Suite.prototype.beforeEach = function(title, fn){
var hook = new Hook(title, fn);
hook.parent = this;
hook.timeout(this.timeout());
+ hook.enableTimeouts(this.enableTimeouts());
hook.slow(this.slow());
hook.ctx = this.ctx;
this._beforeEach.push(hook);
@@ -5254,6 +5324,7 @@ Suite.prototype.afterEach = function(title, fn){
var hook = new Hook(title, fn);
hook.parent = this;
hook.timeout(this.timeout());
+ hook.enableTimeouts(this.enableTimeouts());
hook.slow(this.slow());
hook.ctx = this.ctx;
this._afterEach.push(hook);
@@ -5272,6 +5343,7 @@ Suite.prototype.afterEach = function(title, fn){
Suite.prototype.addSuite = function(suite){
suite.parent = this;
suite.timeout(this.timeout());
+ suite.enableTimeouts(this.enableTimeouts());
suite.slow(this.slow());
suite.bail(this.bail());
this.suites.push(suite);
@@ -5290,6 +5362,7 @@ Suite.prototype.addSuite = function(suite){
Suite.prototype.addTest = function(test){
test.parent = this;
test.timeout(this.timeout());
+ test.enableTimeouts(this.enableTimeouts());
test.slow(this.slow());
test.ctx = this.ctx;
this.tests.push(test);
@@ -5558,16 +5631,19 @@ function ignored(path){
* @api private
*/
-exports.files = function(dir, ret){
+exports.files = function(dir, ext, ret){
ret = ret || [];
+ ext = ext || ['js'];
+
+ var re = new RegExp('\\.(' + ext.join('|') + ')$');
fs.readdirSync(dir)
.filter(ignored)
.forEach(function(path){
path = join(dir, path);
if (fs.statSync(path).isDirectory()) {
- exports.files(path, ret);
- } else if (path.match(/\.(js|coffee|litcoffee|coffee.md)$/)) {
+ exports.files(path, ext, ret);
+ } else if (path.match(re)) {
ret.push(path);
}
});
@@ -5598,7 +5674,7 @@ exports.slug = function(str){
exports.clean = function(str) {
str = str
.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/, '')
- .replace(/^function *\(.*\) *{/, '')
+ .replace(/^function *\(.*\) *{|\(.*\) *=> *{?/, '')
.replace(/\s+\}$/, '');
var spaces = str.match(/^\n?( *)/)[1].length
@@ -5669,7 +5745,7 @@ function highlight(js) {
.replace(/('.*?')/gm, '<span class="string">$1</span>')
.replace(/(\d+\.\d+)/gm, '<span class="number">$1</span>')
.replace(/(\d+)/gm, '<span class="number">$1</span>')
- .replace(/\bnew *(\w+)/gm, '<span class="keyword">new</span> <span class="init">$1</span>')
+ .replace(/\bnew[ \t]+(\w+)/gm, '<span class="keyword">new</span> <span class="init">$1</span>')
.replace(/\b(function|new|throw|return|var|if|else)\b/gm, '<span class="keyword">$1</span>')
}
@@ -5687,9 +5763,57 @@ exports.highlightTags = function(name) {
}
};
+
+/**
+ * Stringify `obj`.
+ *
+ * @param {Object} obj
+ * @return {String}
+ * @api private
+ */
+
+exports.stringify = function(obj) {
+ if (obj instanceof RegExp) return obj.toString();
+ return JSON.stringify(exports.canonicalize(obj), null, 2).replace(/,(\n|$)/g, '$1');
+}
+
+/**
+ * Return a new object that has the keys in sorted order.
+ * @param {Object} obj
+ * @return {Object}
+ * @api private
+ */
+
+exports.canonicalize = function(obj, stack) {
+ stack = stack || [];
+
+ if (exports.indexOf(stack, obj) !== -1) return '[Circular]';
+
+ var canonicalizedObj;
+
+ if ({}.toString.call(obj) === '[object Array]') {
+ stack.push(obj);
+ canonicalizedObj = exports.map(obj, function(item) {
+ return exports.canonicalize(item, stack);
+ });
+ stack.pop();
+ } else if (typeof obj === 'object' && obj !== null) {
+ stack.push(obj);
+ canonicalizedObj = {};
+ exports.forEach(exports.keys(obj).sort(), function(key) {
+ canonicalizedObj[key] = exports.canonicalize(obj[key], stack);
+ });
+ stack.pop();
+ } else {
+ canonicalizedObj = obj;
+ }
+
+ return canonicalizedObj;
+ }
+
}); // module: utils.js
// The global object is "self" in Web Workers.
-global = (function() { return this; })();
+var global = (function() { return this; })();
/**
* Save timer references to avoid Sinon interfering (see GH-237).
@@ -5716,13 +5840,20 @@ process.stdout = {};
var uncaughtExceptionHandlers = [];
+var originalOnerrorHandler = global.onerror;
+
/**
* Remove uncaughtException listener.
+ * Revert to original onerror handler if previously defined.
*/
process.removeListener = function(e, fn){
if ('uncaughtException' == e) {
- global.onerror = function() {};
+ if (originalOnerrorHandler) {
+ global.onerror = originalOnerrorHandler;
+ } else {
+ global.onerror = function() {};
+ }
var i = Mocha.utils.indexOf(uncaughtExceptionHandlers, fn);
if (i != -1) { uncaughtExceptionHandlers.splice(i, 1); }
}
@@ -5825,12 +5956,12 @@ mocha.run = function(fn){
if (query.grep) mocha.grep(query.grep);
if (query.invert) mocha.invert();
- return Mocha.prototype.run.call(mocha, function(){
+ return Mocha.prototype.run.call(mocha, function(err){
// The DOM Document is not available in Web Workers.
if (global.document) {
Mocha.utils.highlightTags('code');
}
- if (fn) fn();
+ if (fn) fn(err);
});
};
diff --git a/package.json b/package.json
index e7de7fd0..f864f895 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "mocha",
- "version": "1.21.0",
+ "version": "1.21.1",
"description": "simple, flexible, fun test framework",
"keywords": [
"mocha",