☕️ Classic, reliable, trusted test framework for Node.js and the browser
Added async support
Tj Holowaychuk8c631ecf5f4b
diff --git a/Makefile b/Makefileindex 3c5267af..e845671a 100644--- a/Makefile+++ b/Makefile@@ -1,5 +1,5 @@-TESTS = test/unit/*.js+TESTS = test/unit/hook.async.jsREPORTER = listtest: test-unitdiff --git a/bin/mocha b/bin/mochaindex b6eeaf48..e3d4d9b0 100755--- a/bin/mocha+++ b/bin/mocha@@ -56,6 +56,10 @@ program.on('require', function(mod){program.parse(process.argv);+// infinite stack traces++Error.stackTraceLimit = Infinity; // TODO: config+// reportervar suite = new Suite('')diff --git a/lib/runner.js b/lib/runner.jsindex 0966ea41..5cb57b50 100644--- a/lib/runner.js+++ b/lib/runner.js@@ -39,6 +39,72 @@ function Runner(suite) {Runner.prototype.__proto__ = EventEmitter.prototype;+/**+ * Run hook `name` callbacks and then invoke `fn(err)`.+ *+ * @param {String} name+ * @param {Function} function+ * @api private+ */++Runner.prototype.hook = function(name, fn){+ var suite = this.suite+ , test = this.test+ , callbacks = suite[name + 'Callbacks']+ , pending = callbacks.length;++ function next(i) {+ var callback = callbacks[i]+ if (!callback) return fn();++ // async+ if (2 == callback.length) {+ // async+ try {+ callback(test, function(err){+ if (err) return fn(err);+ next(++i);+ });+ } catch (err) {+ fn(err);+ }+ return;+ }++ // serial+ try {+ callback(test);+ next(++i);+ } catch (err) {+ fn(err);+ }+ }++ next(0);+};++/**+ * Run the current test and callback `fn(err)`.+ *+ * @param {Function} fn+ * @api private+ */++Runner.prototype.runTest = function(fn){+ var test = this.test;++ // run the test+ try {+ // async+ if (test.async) return test.run(fn);+ // sync+ test.run();+ fn();+ } catch (err) {+ fn(err);+ }+};+/*** Run tests in the given `suite` and invoke* the callback `fn()` when complete.@@ -50,52 +116,43 @@ Runner.prototype.__proto__ = EventEmitter.prototype;Runner.prototype.runTests = function(suite, fn){var self = this- , tests = suite.tests;-- function run(test) {- if (!test) return fn();- self.emit('test', test);- // run any befores first- suite.beforeEachCallbacks.forEach(function(fn){- fn(test);- });- // run the test- try {- // async- if (test.async) {- test.run(function(err){- if (err) {- test.failed = true- self.emit('fail', test, err);- } else {- self.emit('pass', test);- }- self.emit('test end', test);- run(tests.shift());- });- // sync- } else {- test.run();- test.passed = true;- self.emit('pass', test);- }- } catch (err) {+ , tests = suite.tests+ , context+ , test;++ function next(err) {+ // error handling+ if (err) {+ if (!test) throw err;test.failed = true;+ if (context) test.title += ' ' + context;self.emit('fail', test, err);}- // run the next test- if (test.sync) {- self.emit('test end', test);- // teardown hook- suite.afterEachCallbacks.forEach(function(fn){- fn(test);+ // next test+ test = tests.shift();++ // all done+ if (!test) return fn();++ // execute test and hook(s)+ self.emit('test', self.test = test);+ context = 'in beforeEach hook';+ self.hook('beforeEach', function(err){+ context = '';+ if (err) return next(err);+ self.runTest(function(err){+ if (err) return next(err);+ self.emit('pass', test);+ test.passed = true;+ self.emit('test end', test);+ context = 'in afterEach hook';+ self.hook('afterEach', next);});- run(tests.shift());- }+ });}- run(tests.shift());+ next();};/**@@ -110,7 +167,7 @@ Runner.prototype.runTests = function(suite, fn){Runner.prototype.runSuite = function(suite, fn){var self = this;- this.emit('suite', suite);+ this.emit('suite', this.suite = suite);// pending testsvar pending = suite.suites.length + 1;diff --git a/test/unit/hook.async.js b/test/unit/hook.async.jsnew file mode 100644index 00000000..1b473e30--- /dev/null+++ b/test/unit/hook.async.js@@ -0,0 +1,54 @@++describe('async', function(){+ var calls = [];++ process.on('exit', function(){+ calls.should.have.length(6);+ });++ beforeEach(function(test, done){+ // not hit+ calls.push('parent before ' + test.title);+ });++ afterEach(function(test, done){+ // not hit+ calls.push('parent after ' + test.title);+ });++ describe('beforeEach()', function(){+ beforeEach(function(test, done){+ setTimeout(function(){+ calls.push('before ' + test.title);+ done();+ }, 20);+ });++ it('one', function(){+ calls.should.eql(['before one']);+ });++ it('two', function(){+ calls.should.eql([+ 'before one'+ , 'after one'+ , 'before two']);+ });++ it('three', function(){+ calls.should.eql([+ 'before one'+ , 'after one'+ , 'before two'+ , 'after two'+ , 'before three']);+ });++ afterEach(function(test, done){+ setTimeout(function(){+ calls.push('after ' + test.title);+ done();+ }, 20);+ });+ });+});\ No newline at end of filediff --git a/test/unit/beforeEach.serial.js b/test/unit/hook.serial.jssimilarity index 100%rename from test/unit/beforeEach.serial.jsrename to test/unit/hook.serial.jsdiff --git a/test/unit/beforeEach.nested.serial.js b/test/unit/hook.serial.nested.jssimilarity index 100%rename from test/unit/beforeEach.nested.serial.jsrename to test/unit/hook.serial.nested.js