☕️ Classic, reliable, trusted test framework for Node.js and the browser
add ability to delay root suite; closes #362, closes #1124
Christopher Hillerb4312b0e6e6d
diff --git a/Makefile b/Makefileindex f7542a11..f62d0023 100644--- a/Makefile+++ b/Makefile@@ -142,6 +142,12 @@ test-reporters:--reporter $(REPORTER) \test/reporters/*.js+test-delay:+ @./bin/mocha \+ --reporter $(REPORTER) \+ --delay \+ test/delay/*.js+test-only:@./bin/mocha \--reporter $(REPORTER) \diff --git a/bin/_mocha b/bin/_mochaindex 190c9f04..477b844f 100755--- a/bin/_mocha+++ b/bin/_mocha@@ -96,6 +96,7 @@ program.option('--trace', 'trace function calls').option('--trace-deprecation', 'show stack traces on deprecations').option('--watch-extensions <ext>,...', 'additional extensions to monitor with --watch', list, [])+ .option('--delay', 'wait for async suite definition')program.name = 'mocha';@@ -281,6 +282,10 @@ if (program.growl) mocha.growl();if (program.asyncOnly) mocha.asyncOnly();+// --delay++if (program.delay) mocha.delay();+// --globalsmocha.globals(globals);diff --git a/lib/interfaces/bdd.js b/lib/interfaces/bdd.jsindex d212da29..f9f08a3e 100644--- a/lib/interfaces/bdd.js+++ b/lib/interfaces/bdd.js@@ -29,38 +29,13 @@ module.exports = function(suite){suite.on('pre-require', function(context, file, mocha){- /**- * Execute before running tests.- */-- context.before = function(name, fn){- suites[0].beforeAll(name, fn);- };-- /**- * Execute after running tests.- */-- context.after = function(name, fn){- suites[0].afterAll(name, fn);- };-- /**- * Execute before each test case.- */-- context.beforeEach = function(name, fn){- suites[0].beforeEach(name, fn);- };-- /**- * Execute after each test case.- */-- context.afterEach = function(name, fn){- suites[0].afterEach(name, fn);- };+ var common = require('./common')(suites, context);+ context.before = common.before;+ context.after = common.after;+ context.beforeEach = common.beforeEach;+ context.afterEach = common.afterEach;+ context.run = mocha.options.delay && common.runWithSuite(suite);/*** Describe a "suite" with the given `title`* and callback `fn` containing nested suites@@ -135,5 +110,6 @@ module.exports = function(suite){context.it.skip = function(title){context.it(title);};+});};diff --git a/lib/interfaces/common.js b/lib/interfaces/common.jsnew file mode 100644index 00000000..1ccd3390--- /dev/null+++ b/lib/interfaces/common.js@@ -0,0 +1,58 @@+/**+ * Functions common to more than one interface+ * @module lib/interfaces/common+ */++'use strict';++module.exports = function (suites, context) {++ return {+ /**+ * This is only present if flag --delay is passed into Mocha. It triggers+ * root suite execution. Returns a function which runs the root suite.+ */+ runWithSuite: function runWithSuite(suite) {+ return function run() {+ suite.run();+ };+ },++ /**+ * Execute before running tests.+ */+ before: function (name, fn) {+ suites[0].beforeAll(name, fn);+ },++ /**+ * Execute after running tests.+ */+ after: function (name, fn) {+ suites[0].afterAll(name, fn);+ },++ /**+ * Execute before each test case.+ */+ beforeEach: function (name, fn) {+ suites[0].beforeEach(name, fn);+ },++ /**+ * Execute after each test case.+ */+ afterEach: function (name, fn) {+ suites[0].afterEach(name, fn);+ },++ test: {+ /**+ * Pending test case.+ */+ skip: function (title) {+ context.test(title);+ }+ }+ }+};diff --git a/lib/interfaces/qunit.js b/lib/interfaces/qunit.jsindex fee8ac44..6668967c 100644--- a/lib/interfaces/qunit.js+++ b/lib/interfaces/qunit.js@@ -37,38 +37,13 @@ module.exports = function(suite){suite.on('pre-require', function(context, file, mocha){- /**- * Execute before running tests.- */-- context.before = function(name, fn){- suites[0].beforeAll(name, fn);- };-- /**- * Execute after running tests.- */-- context.after = function(name, fn){- suites[0].afterAll(name, fn);- };-- /**- * Execute before each test case.- */-- context.beforeEach = function(name, fn){- suites[0].beforeEach(name, fn);- };-- /**- * Execute after each test case.- */-- context.afterEach = function(name, fn){- suites[0].afterEach(name, fn);- };+ var common = require('./common')(suites, context);+ context.before = common.before;+ context.after = common.after;+ context.beforeEach = common.beforeEach;+ context.afterEach = common.afterEach;+ context.run = mocha.options.delay && common.runWithSuite(suite);/*** Describe a "suite" with the given `title`.*/@@ -113,12 +88,7 @@ module.exports = function(suite){mocha.grep(new RegExp(reString));};- /**- * Pending test case.- */+ context.test.skip = common.test.skip;- context.test.skip = function(title){- context.test(title);- };});};diff --git a/lib/interfaces/tdd.js b/lib/interfaces/tdd.jsindex 951fc9e1..13bc2a33 100644--- a/lib/interfaces/tdd.js+++ b/lib/interfaces/tdd.js@@ -37,38 +37,13 @@ module.exports = function(suite){suite.on('pre-require', function(context, file, mocha){- /**- * Execute before each test case.- */-- context.setup = function(name, fn){- suites[0].beforeEach(name, fn);- };-- /**- * Execute after each test case.- */-- context.teardown = function(name, fn){- suites[0].afterEach(name, fn);- };-- /**- * Execute before the suite.- */-- context.suiteSetup = function(name, fn){- suites[0].beforeAll(name, fn);- };-- /**- * Execute after the suite.- */-- context.suiteTeardown = function(name, fn){- suites[0].afterAll(name, fn);- };+ var common = require('./common')(suites, context);+ context.setup = common.beforeEach;+ context.teardown = common.afterEach;+ context.suiteSetup = common.before;+ context.suiteTeardown = common.after;+ context.run = mocha.options.delay && common.runWithSuite(suite);/*** Describe a "suite" with the given `title`* and callback `fn` containing nested suites@@ -129,12 +104,6 @@ module.exports = function(suite){mocha.grep(new RegExp(reString));};- /**- * Pending test case.- */-- context.test.skip = function(title){- context.test(title);- };+ context.test.skip = common.test.skip;});};diff --git a/lib/mocha.js b/lib/mocha.jsindex fa05ce59..4795d82f 100644--- a/lib/mocha.js+++ b/lib/mocha.js@@ -99,6 +99,7 @@ function Mocha(options) {exports.suite = context.suite || context.describe;exports.teardown = context.teardown || context.afterEach;exports.test = context.test || context.it;+ exports.run = context.run;});}@@ -382,6 +383,16 @@ Mocha.prototype.noHighlighting = function() {return this;};+/**+ * Delay root suite execution.+ * @returns {Mocha}+ * @api public+ */+Mocha.prototype.delay = function delay() {+ this.options.delay = true;+ return this;+};+/*** Run tests and invoke `fn()` when complete.*@@ -389,13 +400,12 @@ Mocha.prototype.noHighlighting = function() {* @return {Runner}* @api public*/-Mocha.prototype.run = function(fn){if (this.files.length) this.loadFiles();var suite = this.suite;var options = this.options;options.files = this.files;- var runner = new exports.Runner(suite);+ var runner = new exports.Runner(suite, options.delay);var reporter = new this._reporter(runner, options);runner.ignoreLeaks = false !== options.ignoreLeaks;runner.asyncOnly = options.asyncOnly;diff --git a/lib/runner.js b/lib/runner.jsindex 80246a12..634c4b28 100644--- a/lib/runner.js+++ b/lib/runner.js@@ -50,13 +50,17 @@ module.exports = Runner;* - `fail` (test, err) test failed* - `pending` (test) test pending*+ * @param {Suite} suite Root suite+ * @param {boolean} [delay] Whether or not to delay execution of root suite+ * until ready.* @api public*/-function Runner(suite) {+function Runner(suite, delay) {var self = this;this._globals = [];this._abort = false;+ this._delay = delay;this.suite = suite;this.total = suite.total();this.failures = 0;@@ -597,13 +601,23 @@ Runner.prototype.uncaught = function(err){*/Runner.prototype.run = function(fn){- var self = this;+ var self = this,+ rootSuite = this.suite;+fn = fn || function(){};function uncaught(err){self.uncaught(err);}+ function start() {+ self.emit('start');+ self.runSuite(rootSuite, function(){+ debug('finished running');+ self.emit('end');+ });+ }+debug('start');// callback@@ -613,16 +627,19 @@ Runner.prototype.run = function(fn){fn(self.failures);});- // run suites- this.emit('start');- this.runSuite(this.suite, function(){- debug('finished running');- self.emit('end');- });-// uncaught exceptionprocess.on('uncaughtException', uncaught);+ if (this._delay) {+ // for reporters, I guess.+ // might be nice to debounce some dots while we wait.+ this.emit('waiting', rootSuite);+ rootSuite.once('run', start);+ }+ else {+ start();+ }+return this;};diff --git a/lib/suite.js b/lib/suite.jsindex 65cdbffc..edc820e2 100644--- a/lib/suite.js+++ b/lib/suite.js@@ -62,6 +62,7 @@ function Suite(title, parentContext) {this._enableTimeouts = true;this._slow = 75;this._bail = false;+ this.delayed = false;}/**@@ -343,3 +344,12 @@ Suite.prototype.eachTest = function(fn){});return this;};++/**+ * This will run the root suite if we happen to be running in delayed mode.+ */+Suite.prototype.run = function run() {+ if (this.root) {+ this.emit('run');+ }+};diff --git a/mocha.js b/mocha.jsindex 5039bfaf..fe1a02da 100644--- a/mocha.js+++ b/mocha.js@@ -953,38 +953,13 @@ module.exports = function(suite){suite.on('pre-require', function(context, file, mocha){- /**- * Execute before running tests.- */-- context.before = function(name, fn){- suites[0].beforeAll(name, fn);- };-- /**- * Execute after running tests.- */-- context.after = function(name, fn){- suites[0].afterAll(name, fn);- };-- /**- * Execute before each test case.- */-- context.beforeEach = function(name, fn){- suites[0].beforeEach(name, fn);- };-- /**- * Execute after each test case.- */-- context.afterEach = function(name, fn){- suites[0].afterEach(name, fn);- };+ var common = require('./common')(suites, context);+ context.before = common.before;+ context.after = common.after;+ context.beforeEach = common.beforeEach;+ context.afterEach = common.afterEach;+ context.run = mocha.options.delay && common.runWithSuite(suite);/*** Describe a "suite" with the given `title`* and callback `fn` containing nested suites@@ -1059,11 +1034,74 @@ module.exports = function(suite){context.it.skip = function(title){context.it(title);};+});};}); // module: interfaces/bdd.js+require.register("interfaces/common.js", function(module, exports, require){+/**+ * Functions common to more than one interface+ * @module lib/interfaces/common+ */++'use strict';++module.exports = function (suites, context) {++ return {+ /**+ * This is only present if flag --delay is passed into Mocha. It triggers+ * root suite execution. Returns a function which runs the root suite.+ */+ runWithSuite: function runWithSuite(suite) {+ return function run() {+ suite.run();+ };+ },++ /**+ * Execute before running tests.+ */+ before: function (name, fn) {+ suites[0].beforeAll(name, fn);+ },++ /**+ * Execute after running tests.+ */+ after: function (name, fn) {+ suites[0].afterAll(name, fn);+ },++ /**+ * Execute before each test case.+ */+ beforeEach: function (name, fn) {+ suites[0].beforeEach(name, fn);+ },++ /**+ * Execute after each test case.+ */+ afterEach: function (name, fn) {+ suites[0].afterEach(name, fn);+ },++ test: {+ /**+ * Pending test case.+ */+ skip: function (title) {+ context.test(title);+ }+ }+ }+};++}); // module: interfaces/common.js+require.register("interfaces/exports.js", function(module, exports, require){/*** Module dependencies.@@ -1177,38 +1215,13 @@ module.exports = function(suite){suite.on('pre-require', function(context, file, mocha){- /**- * Execute before running tests.- */-- context.before = function(name, fn){- suites[0].beforeAll(name, fn);- };-- /**- * Execute after running tests.- */-- context.after = function(name, fn){- suites[0].afterAll(name, fn);- };-- /**- * Execute before each test case.- */-- context.beforeEach = function(name, fn){- suites[0].beforeEach(name, fn);- };-- /**- * Execute after each test case.- */-- context.afterEach = function(name, fn){- suites[0].afterEach(name, fn);- };+ var common = require('./common')(suites, context);+ context.before = common.before;+ context.after = common.after;+ context.beforeEach = common.beforeEach;+ context.afterEach = common.afterEach;+ context.run = mocha.options.delay && common.runWithSuite(suite);/*** Describe a "suite" with the given `title`.*/@@ -1253,13 +1266,8 @@ module.exports = function(suite){mocha.grep(new RegExp(reString));};- /**- * Pending test case.- */+ context.test.skip = common.test.skip;- context.test.skip = function(title){- context.test(title);- };});};@@ -1305,38 +1313,13 @@ module.exports = function(suite){suite.on('pre-require', function(context, file, mocha){- /**- * Execute before each test case.- */-- context.setup = function(name, fn){- suites[0].beforeEach(name, fn);- };-- /**- * Execute after each test case.- */-- context.teardown = function(name, fn){- suites[0].afterEach(name, fn);- };-- /**- * Execute before the suite.- */-- context.suiteSetup = function(name, fn){- suites[0].beforeAll(name, fn);- };-- /**- * Execute after the suite.- */-- context.suiteTeardown = function(name, fn){- suites[0].afterAll(name, fn);- };+ var common = require('./common')(suites, context);+ context.setup = common.beforeEach;+ context.teardown = common.afterEach;+ context.suiteSetup = common.before;+ context.suiteTeardown = common.after;+ context.run = mocha.options.delay && common.runWithSuite(suite);/*** Describe a "suite" with the given `title`* and callback `fn` containing nested suites@@ -1397,13 +1380,7 @@ module.exports = function(suite){mocha.grep(new RegExp(reString));};- /**- * Pending test case.- */-- context.test.skip = function(title){- context.test(title);- };+ context.test.skip = common.test.skip;});};@@ -1511,6 +1488,7 @@ function Mocha(options) {exports.suite = context.suite || context.describe;exports.teardown = context.teardown || context.afterEach;exports.test = context.test || context.it;+ exports.run = context.run;});}@@ -1794,6 +1772,16 @@ Mocha.prototype.noHighlighting = function() {return this;};+/**+ * Delay root suite execution.+ * @returns {Mocha}+ * @api public+ */+Mocha.prototype.delay = function delay() {+ this.options.delay = true;+ return this;+};+/*** Run tests and invoke `fn()` when complete.*@@ -1801,13 +1789,12 @@ Mocha.prototype.noHighlighting = function() {* @return {Runner}* @api public*/-Mocha.prototype.run = function(fn){if (this.files.length) this.loadFiles();var suite = this.suite;var options = this.options;options.files = this.files;- var runner = new exports.Runner(suite);+ var runner = new exports.Runner(suite, options.delay);var reporter = new this._reporter(runner, options);runner.ignoreLeaks = false !== options.ignoreLeaks;runner.asyncOnly = options.asyncOnly;@@ -4596,13 +4583,17 @@ module.exports = Runner;* - `fail` (test, err) test failed* - `pending` (test) test pending*+ * @param {Suite} suite Root suite+ * @param {boolean} [delay] Whether or not to delay execution of root suite+ * until ready.* @api public*/-function Runner(suite) {+function Runner(suite, delay) {var self = this;this._globals = [];this._abort = false;+ this._delay = delay;this.suite = suite;this.total = suite.total();this.failures = 0;@@ -5147,13 +5138,23 @@ Runner.prototype.uncaught = function(err){*/Runner.prototype.run = function(fn){- var self = this;+ var self = this,+ rootSuite = this.suite;+fn = fn || function(){};function uncaught(err){self.uncaught(err);}+ function start() {+ self.emit('start');+ self.runSuite(rootSuite, function(){+ debug('finished running');+ self.emit('end');+ });+ }+debug('start');// callback@@ -5163,16 +5164,19 @@ Runner.prototype.run = function(fn){fn(self.failures);});- // run suites- this.emit('start');- this.runSuite(this.suite, function(){- debug('finished running');- self.emit('end');- });-// uncaught exceptionprocess.on('uncaughtException', uncaught);+ if (this._delay) {+ // for reporters, I guess.+ // might be nice to debounce some dots while we wait.+ this.emit('waiting', rootSuite);+ rootSuite.once('run', start);+ }+ else {+ start();+ }+return this;};@@ -5313,6 +5317,7 @@ function Suite(title, parentContext) {this._enableTimeouts = true;this._slow = 75;this._bail = false;+ this.delayed = false;}/**@@ -5599,6 +5604,15 @@ Suite.prototype.eachTest = function(fn){return this;};+/**+ * This will run the root suite if we happen to be running in delayed mode.+ */+Suite.prototype.run = function run() {+ if (this.root) {+ this.emit('run');+ }+};+}); // module: suite.jsrequire.register("test.js", function(module, exports, require){diff --git a/test/delay/delay.js b/test/delay/delay.jsnew file mode 100644index 00000000..ce74db51--- /dev/null+++ b/test/delay/delay.js@@ -0,0 +1,19 @@+var MS = 500,+ start = new Date().getTime();++setTimeout(function () {+ describe('delayed execution', function () {+ it('should have waited ' + MS + 'ms to run this suite', function () {+ (new Date().getTime() - MS >= start).should.be.true;+ });++ it('should have no effect if attempted twice in the same suite',+ function () {+ true.should.be.true;+ run();+ true.should.be.true;+ });+ });+ run();++}, MS);