Skip to content

Commit

Permalink
Fixed: cascade beforeEach / afterEach. Closes #30
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Nov 19, 2011
1 parent ca7455e commit 752f7c6
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 2 deletions.
80 changes: 78 additions & 2 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)`.
*
Expand Down Expand Up @@ -231,15 +307,15 @@ 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);
self.emit('pass', test);
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();
});
Expand Down
58 changes: 58 additions & 0 deletions test/misc/cascade.js
Original file line number Diff line number Diff line change
@@ -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');
})
})
})
})

0 comments on commit 752f7c6

Please sign in to comment.