Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Fixed: cascade beforeEach / afterEach. Closes #30

TJ Holowaychuk752f7c65ccd0
diff --git a/lib/runner.js b/lib/runner.js
index 5a7f98f1..a53981f1 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -155,6 +155,82 @@ Runner.prototype.hook = function(name, fn){
});
};
+/**
+ * Run hook `name` for the given array of `suites`
+ * in order, and callback `fn(err)`.
+ *
+ * @param {String} name
+ * @param {Array} suites
+ * @param {Function} fn
+ * @api private
+ */
+
+Runner.prototype.hooks = function(name, suites, fn){
+ var self = this
+ , orig = this.suite;
+
+ function next(suite) {
+ self.suite = suite;
+
+ if (!suite) {
+ self.suite = orig;
+ return fn();
+ }
+
+ self.hook(name, function(err){
+ if (err) {
+ self.suite = orig;
+ return fn(err);
+ }
+
+ next(suites.pop());
+ });
+ }
+
+ next(suites.pop());
+};
+
+/**
+ * Run hooks from the top level down.
+ *
+ * @param {String} name
+ * @param {Function} fn
+ * @api private
+ */
+
+Runner.prototype.hookUp = function(name, fn){
+ var suites = [this.suite].concat(this.parents()).reverse();
+ this.hooks(name, suites, fn);
+};
+
+/**
+ * Run hooks from the bottom up.
+ *
+ * @param {String} name
+ * @param {Function} fn
+ * @api private
+ */
+
+Runner.prototype.hookDown = function(name, fn){
+ var suites = [this.suite].concat(this.parents());
+ this.hooks(name, suites, fn);
+};
+
+/**
+ * Return an array of parent Suites from
+ * closest to furthest.
+ *
+ * @return {Array}
+ * @api private
+ */
+
+Runner.prototype.parents = function(){
+ var suite = this.suite
+ , suites = [];
+ while (suite = suite.parent) suites.push(suite);
+ return suites;
+};
+
/**
* Run the current test and callback `fn(err)`.
*
@@ -231,7 +307,7 @@ Runner.prototype.runTests = function(suite, fn){
// execute test and hook(s)
self.emit('test', self.test = test);
- self.hook('beforeEach', function(err){
+ self.hookDown('beforeEach', function(err){
if (err) return self.failHook('beforeEach', err);
self.runTest(function(err){
if (err) return next(err);
@@ -239,7 +315,7 @@ Runner.prototype.runTests = function(suite, fn){
test.passed = true;
self.emit('test end', test);
if (err) return self.failHook('beforeEach', err);
- self.hook('afterEach', function(err){
+ self.hookUp('afterEach', function(err){
if (err) return self.failHook('afterEach', err);
next();
});
diff --git a/test/misc/cascade.js b/test/misc/cascade.js
new file mode 100644
index 00000000..7f5e585a
--- /dev/null
+++ b/test/misc/cascade.js
@@ -0,0 +1,58 @@
+
+describe('one', function(){
+ before(function(){
+ console.log('before one');
+ })
+
+ after(function(){
+ console.log('after one');
+ })
+
+ beforeEach(function(){
+ console.log(' before each one');
+ })
+
+ afterEach(function(){
+ console.log(' after each one');
+ })
+
+ describe('two', function(){
+ before(function(){
+ console.log(' before two');
+ })
+
+ after(function(){
+ console.log(' after two');
+ })
+
+ beforeEach(function(){
+ console.log(' before each two');
+ })
+
+ afterEach(function(){
+ console.log(' after each two');
+ })
+
+ describe('three', function(){
+ before(function(){
+ console.log(' before three');
+ })
+
+ after(function(){
+ console.log(' after three');
+ })
+
+ beforeEach(function(){
+ console.log(' before each three');
+ })
+
+ afterEach(function(){
+ console.log(' after each three');
+ })
+
+ it('should three', function(){
+ console.log(' TEST three');
+ })
+ })
+ })
+})