☕️ Classic, reliable, trusted test framework for Node.js and the browser
Release 0.0.7
Tj Holowaychukd746a3271ae3
diff --git a/History.md b/History.mdindex 8eeb5d57..268bc64c 100644--- a/History.md+++ b/History.md@@ -1,4 +1,14 @@+0.0.7 / 2011-11-25+==================++ * Added `Hook`+ * Added `Runnable`+ * Changed: `Test` is `Runnable`+ * Fixed global leak reporting in hooks+ * Fixed: > 2 calls to done() only report the error once+ * Fixed: clear timer on failure. Closes #80+0.0.6 / 2011-11-25==================diff --git a/lib/mocha.js b/lib/mocha.jsindex bc37165f..f2c8ed8c 100644--- a/lib/mocha.js+++ b/lib/mocha.js@@ -9,7 +9,7 @@* Library version.*/-exports.version = '0.0.6';+exports.version = '0.0.7';exports.interfaces = require('./interfaces');exports.reporters = require('./reporters');diff --git a/mocha.js b/mocha.jsindex 347ccdac..f9e04e49 100644--- a/mocha.js+++ b/mocha.js@@ -114,6 +114,42 @@ exports.getWindowSize = function(){};}); // module: browser/tty.js+require.register("hook.js", function(module, exports, require){++/**+ * Module dependencies.+ */++var Runnable = require('./runnable');++/**+ * Expose `Hook`.+ */++module.exports = Hook;++/**+ * Initialize a new `Hook` with the given `title` and callback `fn`.+ *+ * @param {String} title+ * @param {Function} fn+ * @api private+ */++function Hook(title, fn) {+ Runnable.call(this, title, fn);+}++/**+ * Inherit from `Runnable.prototype`.+ */++Hook.prototype = new Runnable;+Hook.prototype.constructor = Hook;+++}); // module: hook.js+require.register("interfaces/bdd.js", function(module, exports, require){/**@@ -360,12 +396,14 @@ require.register("mocha.js", function(module, exports, require){* Library version.*/-exports.version = '0.0.5';+exports.version = '0.0.6';exports.interfaces = require('./interfaces');exports.reporters = require('./reporters');+exports.Runnable = require('./runnable');exports.Runner = require('./runner');exports.Suite = require('./suite');+exports.Hook = require('./hook');exports.Test = require('./test');}); // module: mocha.js@@ -1431,6 +1469,130 @@ function TAP(runner) {}}); // module: reporters/tap.js+require.register("runnable.js", function(module, exports, require){++/**+ * Module dependencies.+ */++var EventEmitter = require('browser/events').EventEmitter;++/**+ * Expose `Runnable`.+ */++module.exports = Runnable;++/**+ * Initialize a new `Runnable` with the given `title` and callback `fn`.+ *+ * @param {String} title+ * @param {Function} fn+ * @api private+ */++function Runnable(title, fn) {+ this.title = title;+ this.fn = fn;+ this.async = fn && fn.length;+ this.sync = ! this.async;+ this.timeout(2000);+}++/**+ * Inherit from `EventEmitter.prototype`.+ */++Runnable.prototype = new EventEmitter;+Runnable.prototype.constructor = Runnable;+++/**+ * Set & get timeout `ms`.+ *+ * @param {Number} ms+ * @return {Runnable|Number} ms or self+ * @api private+ */++Runnable.prototype.timeout = function(ms){+ if (0 == arguments.length) return this._timeout;+ this._timeout = ms;+ return this;+};++/**+ * Return the full title generated by recursively+ * concatenating the parent's full title.+ *+ * @return {String}+ * @api public+ */++Runnable.prototype.fullTitle = function(){+ return this.parent.fullTitle() + ' ' + this.title;+};++/**+ * Run the test and invoke `fn(err)`.+ *+ * @param {Function} fn+ * @api private+ */++Runnable.prototype.run = function(fn){+ var self = this+ , ms = this._timeout+ , start = new Date+ , finished+ , emitted+ , timer;++ // timeout+ if (this.async) {+ timer = setTimeout(function(){+ fn(new Error('timeout of ' + ms + 'ms exceeded'));+ }, ms);+ }++ // called multiple times+ function multiple() {+ if (emitted) return;+ emitted = true;+ self.emit('error', new Error('done() called multiple times'));+ }++ // finished+ function done(err) {+ if (finished) return multiple();+ clearTimeout(timer);+ self.duration = new Date - start;+ finished = true;+ fn(err);+ }++ // async+ if (this.async) {+ try {+ this.fn(done);+ } catch (err) {+ done(err);+ }+ return;+ }++ // sync+ try {+ if (!this.pending) this.fn();+ this.duration = new Date - start;+ fn();+ } catch (err) {+ fn(err);+ }+};++}); // module: runnable.js+require.register("runner.js", function(module, exports, require){/**@@ -1458,6 +1620,8 @@ module.exports = Runner;* - `suite end` (suite) all tests (and sub-suites) have finished* - `test` (test) test execution started* - `test end` (test) test completed+ * - `hook` (hook) hook execution started+ * - `hook end` (hook) hook complete* - `pass` (test) test passed* - `fail` (test, err) test failed*@@ -1470,6 +1634,7 @@ function Runner(suite) {this.total = suite.total();this.globals = Object.keys(global).concat(['errno']);this.on('test end', function(test){ self.checkGlobals(test); });+ this.on('hook end', function(hook){ self.checkGlobals(hook); });this.grep(/.*/);}@@ -1528,23 +1693,26 @@ Runner.prototype.fail = function(test, err){};/**- * Fail the given `hook` name.+ * Fail the given `hook` with `err`.+ *+ * Hook failures (currently) hard-exit due+ * to that fact that a failing hook will+ * surely cause subsequent tests to fail,+ * causing jumbled reporting.*- * @param {String} hook+ * @param {Hook} hook* @param {Error} err* @api private*/Runner.prototype.failHook = function(hook, err){- var test = new Test(hook + ' hook', noop);- test.parent = this.suite;- this.fail(test, err);+ this.fail(hook, err);this.emit('end');process.exit(0);};/**- * Run hook `name` callbacks and then invoke `fn(err)`.+ * Run hook `name` callbacks and then invoke `fn()`.** @param {String} name* @param {Function} function@@ -1553,43 +1721,26 @@ Runner.prototype.failHook = function(hook, err){Runner.prototype.hook = function(name, fn){var suite = this.suite- , callbacks = suite[name + 'Callbacks']+ , hooks = suite['_' + name], ms = suite._timeout+ , self = this, timer;function next(i) {- var callback = callbacks[i];- if (!callback) return fn();-- // async- if (1 == callback.length) {- // timeout- timer = setTimeout(function(){- fn(new Error('timeout of ' + ms + 'ms exceeded'));- }, ms);-- // async- try {- callback(function(err){- clearTimeout(timer);- if (err) return fn(err);- next(++i);- });- } catch (err) {- fn(err);- }- return;- }+ var hook = hooks[i];+ if (!hook) return fn();- // serial- try {- callback();- process.nextTick(function(){- next(++i);- });- } catch (err) {- fn(err);- }+ self.emit('hook', hook);++ hook.on('error', function(err){+ self.failHook(hook, err);+ });++ hook.run(function(err){+ if (err) return self.failHook(hook, err);+ self.emit('hook end', hook);+ next(++i);+ });}process.nextTick(function(){@@ -1684,32 +1835,14 @@ Runner.prototype.runTest = function(fn){var test = this.test, self = this;- // async- if (test.async) {- try {- test.run(function(err){- if (test.finished) {- self.fail(test, new Error('done() called multiple times'));- return;- }- fn(err);- });- } catch (err) {- fn(err);- }-- return;+ try {+ test.on('error', function(err){+ self.fail(test, err);+ });+ test.run(fn);+ } catch (err) {+ fn(err);}-- // sync- process.nextTick(function(){- try {- test.run();- fn();- } catch (err) {- fn(err);- }- });};/**@@ -1751,18 +1884,13 @@ Runner.prototype.runTests = function(suite, fn){// execute test and hook(s)self.emit('test', self.test = test);- self.hookDown('beforeEach', function(err){- if (err) return self.failHook('beforeEach', err);+ self.hookDown('beforeEach', function(){self.runTest(function(err){if (err) return next(err);self.emit('pass', test);test.passed = true;self.emit('test end', test);- if (err) return self.failHook('beforeEach', err);- self.hookUp('afterEach', function(err){- if (err) return self.failHook('afterEach', err);- next();- });+ self.hookUp('afterEach', next);});});}@@ -1793,15 +1921,13 @@ Runner.prototype.runSuite = function(suite, fn){function done() {self.suite = suite;- self.hook('afterAll', function(err){- if (err) return self.failHook('afterAll', err);+ self.hook('afterAll', function(){self.emit('suite end', suite);fn();});}- this.hook('beforeAll', function(err){- if (err) return self.failHook('beforeAll', err);+ this.hook('beforeAll', function(){self.runTests(suite, next);});};@@ -1839,7 +1965,8 @@ require.register("suite.js", function(module, exports, require){* Module dependencies.*/-var EventEmitter = require('browser/events').EventEmitter;+var EventEmitter = require('browser/events').EventEmitter+ , Hook = require('./hook');/*** Expose `Suite`.@@ -1886,10 +2013,10 @@ function Suite(title) {this.title = title;this.suites = [];this.tests = [];- this.beforeAllCallbacks = [];- this.beforeEachCallbacks = [];- this.afterAllCallbacks = [];- this.afterEachCallbacks = [];+ this._beforeEach = [];+ this._beforeAll = [];+ this._afterEach = [];+ this._afterAll = [];this.root = !title;this.timeout(2000);}@@ -1925,8 +2052,10 @@ Suite.prototype.timeout = function(ms){*/Suite.prototype.beforeAll = function(fn){- this.beforeAllCallbacks.push(fn);- this.emit('beforeAll', fn);+ var hook = new Hook('"before all" hook', fn);+ hook.parent = this;+ this._beforeAll.push(hook);+ this.emit('beforeAll', hook);return this;};@@ -1939,8 +2068,10 @@ Suite.prototype.beforeAll = function(fn){*/Suite.prototype.afterAll = function(fn){- this.afterAllCallbacks.push(fn);- this.emit('afterAll', fn);+ var hook = new Hook('"after all" hook', fn);+ hook.parent = this;+ this._afterAll.push(hook);+ this.emit('afterAll', hook);return this;};@@ -1953,8 +2084,10 @@ Suite.prototype.afterAll = function(fn){*/Suite.prototype.beforeEach = function(fn){- this.beforeEachCallbacks.push(fn);- this.emit('beforeEach', fn);+ var hook = new Hook('"before each" hook', fn);+ hook.parent = this;+ this._beforeEach.push(hook);+ this.emit('beforeEach', hook);return this;};@@ -1967,8 +2100,10 @@ Suite.prototype.beforeEach = function(fn){*/Suite.prototype.afterEach = function(fn){- this.afterEachCallbacks.push(fn);- this.emit('afterEach', fn);+ var hook = new Hook('"after each" hook', fn);+ hook.parent = this;+ this._afterEach.push(hook);+ this.emit('afterEach', hook);return this;};@@ -2037,6 +2172,12 @@ Suite.prototype.total = function(){require.register("test.js", function(module, exports, require){+/**+ * Module dependencies.+ */++var Runnable = require('./runnable');+/*** Expose `Test`.*/@@ -2052,73 +2193,17 @@ module.exports = Test;*/function Test(title, fn) {- this.title = title;- this.fn = fn;+ Runnable.call(this, title, fn);this.pending = !fn;- this.async = fn && fn.length;- this.sync = ! this.async;- this.timeout(2000);}/**- * Set timeout `ms`.- *- * @param {Number} ms- * @return {Test} for chaining- * @api private+ * Inherit from `Runnable.prototype`.*/-Test.prototype.timeout = function(ms){- this._timeout = ms;- return this;-};+Test.prototype = new Runnable;+Test.prototype.constructor = Test;-/**- * Return the full title generated by recursively- * concatenating the parent's full title.- *- * @return {String}- * @api public- */--Test.prototype.fullTitle = function(){- return this.parent.fullTitle() + ' ' + this.title;-};--/**- * Run the test and invoke `fn(err)`.- *- * @param {Function} fn- * @api private- */--Test.prototype.run = function(fn){- var timer- , self = this- , ms = this._timeout- , start = new Date;-- // timeout- if (this.async) {- timer = setTimeout(function(){- fn(new Error('timeout of ' + ms + 'ms exceeded'));- }, ms);- }-- // async- if (this.async) {- this.fn(function(err){- clearTimeout(timer);- self.duration = new Date - start;- fn(err);- self.finished = true;- });- // sync- } else {- if (!this.pending) this.fn();- this.duration = new Date - start;- }-};}); // module: test.jsdiff --git a/package.json b/package.jsonindex 3567c87a..2c711ecc 100644--- a/package.json+++ b/package.json@@ -1,6 +1,6 @@{"name": "mocha"- , "version": "0.0.6"+ , "version": "0.0.7", "description": "Test framework inspired by JSpec, Expresso, & Qunit", "keywords": ["test", "bdd", "tdd", "tap"], "bin": { "mocha": "./bin/mocha" }