diff --git a/lib/runner.js b/lib/runner.js index 1889794db6..c42e449751 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -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){ @@ -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); }); } diff --git a/test/hook.async.js b/test/hook.async.js index 221bebf181..fd62932c4b 100644 --- a/test/hook.async.js +++ b/test/hook.async.js @@ -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']); @@ -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(); }) }) @@ -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); }) @@ -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'); }) @@ -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(); }) }) diff --git a/test/hook.sync.js b/test/hook.sync.js index 81c772f96a..c446c6aac7 100644 --- a/test/hook.sync.js +++ b/test/hook.sync.js @@ -13,10 +13,16 @@ 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'); }) @@ -24,11 +30,14 @@ describe('serial', 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'); }) @@ -36,39 +45,53 @@ describe('serial', 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']); }) }) diff --git a/test/hook.sync.nested.js b/test/hook.sync.nested.js index 22907a5bd3..94c00da5f7 100644 --- a/test/hook.sync.nested.js +++ b/test/hook.sync.nested.js @@ -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); + } }) }) })