Skip to content

Commit

Permalink
Merge pull request #795 from avaly/fix-794
Browse files Browse the repository at this point in the history
Expose current test information to beforeEach/afterEach, fixes #794, #797
  • Loading branch information
tj committed Jul 1, 2013
2 parents 4a6a63b + 7fae7fb commit 8bc84fc
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 10 deletions.
3 changes: 3 additions & 0 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ Runner.prototype.hook = function(name, fn){
if (self.failures && suite.bail()) return fn();
self.currentRunnable = hook;

hook.ctx.currentTest = self.test;

self.emit('hook', hook);

hook.on('error', function(err){
Expand All @@ -247,6 +249,7 @@ Runner.prototype.hook = function(name, fn){
if (testError) self.fail(self.test, testError);
if (err) return self.failHook(hook, err);
self.emit('hook end', hook);
delete hook.ctx.currentTest;
next(++i);
});
}
Expand Down
29 changes: 26 additions & 3 deletions test/hook.async.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@ describe('async', function(){
, 'before all'
, 'parent before'
, 'before'
, 'before test one'
, 'one'
, 'after'
, 'after test one passed'
, 'parent after'
, 'parent before'
, 'before'
, 'before test two'
, 'two'
, 'after'
, 'after test two passed'
, 'parent after'
, 'parent before'
, 'before'
, 'before test three'
, 'three'
, 'after'
, 'after test three passed'
, 'parent after'
, 'after all'
, 'root after all']);
Expand All @@ -48,8 +54,12 @@ describe('async', function(){
});

beforeEach(function(done){
var ctx = this;
process.nextTick(function(){
calls.push('before');
if (ctx.currentTest) {
calls.push('before test ' + ctx.currentTest.title);
}
done();
})
})
Expand All @@ -59,7 +69,8 @@ describe('async', function(){
'root before all'
, 'before all'
, 'parent before'
, 'before']);
, 'before'
, 'before test one']);
calls.push('one');
process.nextTick(done);
})
Expand All @@ -70,11 +81,14 @@ describe('async', function(){
, 'before all'
, 'parent before'
, 'before'
, 'before test one'
, 'one'
, 'after'
, 'after test one passed'
, 'parent after'
, 'parent before'
, 'before']);
, 'before'
, 'before test two']);
calls.push('two');
})

Expand All @@ -84,22 +98,31 @@ describe('async', function(){
, 'before all'
, 'parent before'
, 'before'
, 'before test one'
, 'one'
, 'after'
, 'after test one passed'
, 'parent after'
, 'parent before'
, 'before'
, 'before test two'
, 'two'
, 'after'
, 'after test two passed'
, 'parent after'
, 'parent before'
, 'before']);
, 'before'
, 'before test three']);
calls.push('three');
})

afterEach(function(done){
var ctx = this;
process.nextTick(function(){
calls.push('after');
if (ctx.currentTest) {
calls.push('after test ' + ctx.currentTest.title + ' ' + ctx.currentTest.state);
}
done();
})
})
Expand Down
29 changes: 26 additions & 3 deletions test/hook.sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,85 @@ describe('serial', function(){
describe('hooks', function(){
beforeEach(function(){
calls.push('before');
if (this.currentTest) {
calls.push('before test ' + this.currentTest.title);
}
})

it('one', function(){
calls.should.eql(['parent before', 'before']);
calls.should.eql([
'parent before'
, 'before'
, 'before test one']);
calls.push('one');
})

it('two', function(){
calls.should.eql([
'parent before'
, 'before'
, 'before test one'
, 'one'
, 'after'
, 'after test one passed'
, 'parent after'
, 'parent before'
, 'before']);
, 'before'
, 'before test two']);
calls.push('two');
})

it('three', function(){
calls.should.eql([
'parent before'
, 'before'
, 'before test one'
, 'one'
, 'after'
, 'after test one passed'
, 'parent after'
, 'parent before'
, 'before'
, 'before test two'
, 'two'
, 'after'
, 'after test two passed'
, 'parent after'
, 'parent before'
, 'before']);
, 'before'
, 'before test three']);
calls.push('three');
})

afterEach(function(){
calls.push('after');
if (this.currentTest) {
calls.push('after test ' + this.currentTest.title + ' ' + this.currentTest.state);
}
})

after(function(){
calls.should.eql([
'parent before'
, 'before'
, 'before test one'
, 'one'
, 'after'
, 'after test one passed'
, 'parent after'
, 'parent before'
, 'before'
, 'before test two'
, 'two'
, 'after'
, 'after test two passed'
, 'parent after'
, 'parent before'
, 'before'
, 'before test three'
, 'three'
, 'after'
, 'after test three passed'
, 'parent after']);
})
})
Expand Down
41 changes: 37 additions & 4 deletions test/hook.sync.nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,94 @@ describe('serial', function(){

beforeEach(function(){
calls.push('parent before');
if (this.currentTest) {
calls.push('parent before test ' + this.currentTest.title);
}
})

afterEach(function(){
calls.push('parent after');
if (this.currentTest) {
calls.push('parent after test ' + this.currentTest.title + ' ' + this.currentTest.state);
}
});

it('foo', function(){
calls.should.eql(['parent before']);
calls.should.eql([
'parent before'
, 'parent before test foo']);
calls.push('foo');
})

it('bar', function(){
calls.should.eql([
'parent before'
, 'parent before test foo'
, 'foo'
, 'parent after'
, 'parent before']);
, 'parent after test foo passed'
, 'parent before'
, 'parent before test bar']);
})

describe('hooks', function(){
beforeEach(function(){
calls.push('before');
if (this.currentTest) {
calls.push('before test ' + this.currentTest.title);
}
})

it('one', function(){
calls.should.eql([
'parent before'
, 'parent before test foo'
, 'foo'
, 'parent after'
, 'parent after test foo passed'
, 'parent before'
, 'parent before test bar'
, 'parent after'
, 'parent after test bar passed'
, 'parent before'
, 'before']);
, 'parent before test one'
, 'before'
, 'before test one']);
calls.push('one');
})

it('two', function(){
calls.should.eql([
'parent before'
, 'parent before test foo'
, 'foo'
, 'parent after'
, 'parent after test foo passed'
, 'parent before'
, 'parent before test bar'
, 'parent after'
, 'parent after test bar passed'
, 'parent before'
, 'parent before test one'
, 'before'
, 'before test one'
, 'one'
, 'after'
, 'after test one passed'
, 'parent after'
, 'parent after test one passed'
, 'parent before'
, 'before']);
, 'parent before test two'
, 'before'
, 'before test two']);
calls.push('two');
});

afterEach(function(){
calls.push('after');
if (this.currentTest) {
calls.push('after test ' + this.currentTest.title + ' ' + this.currentTest.state);
}
})
})
})
Expand Down

0 comments on commit 8bc84fc

Please sign in to comment.