☕️ Classic, reliable, trusted test framework for Node.js and the browser
Expose current test information to beforeEach, fixes #794
Valentin Agachi24856d4c721d
diff --git a/lib/runner.js b/lib/runner.jsindex 43698089..fd2bca52 100644--- a/lib/runner.js+++ b/lib/runner.js@@ -234,6 +234,8 @@ Runner.prototype.hook = function(name, fn){if (!hook) return fn();self.currentRunnable = hook;+ hook.ctx.currentTest = self.test;+self.emit('hook', hook);hook.on('error', function(err){@@ -246,6 +248,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.jsindex 221bebf1..1976f6fe 100644--- a/test/hook.async.js+++ b/test/hook.async.js@@ -13,16 +13,19 @@ describe('async', function(){, 'before all', 'parent before', 'before'+ , 'before test one', 'one', 'after', 'parent after', 'parent before', 'before'+ , 'before test two', 'two', 'after', 'parent after', 'parent before', 'before'+ , 'before test three', 'three', 'after', 'parent after'@@ -48,8 +51,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 +66,8 @@ describe('async', function(){'root before all', 'before all', 'parent before'- , 'before']);+ , 'before'+ , 'before test one']);calls.push('one');process.nextTick(done);})@@ -70,11 +78,13 @@ describe('async', function(){, 'before all', 'parent before', 'before'+ , 'before test one', 'one', 'after', 'parent after', 'parent before'- , 'before']);+ , 'before'+ , 'before test two']);calls.push('two');})@@ -84,16 +94,19 @@ describe('async', function(){, 'before all', 'parent before', 'before'+ , 'before test one', 'one', 'after', 'parent after', 'parent before', 'before'+ , 'before test two', 'two', 'after', 'parent after', 'parent before'- , 'before']);+ , 'before'+ , 'before test three']);calls.push('three');})diff --git a/test/hook.sync.js b/test/hook.sync.jsindex 81c772f9..eb20a209 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,13 @@ describe('serial', function(){calls.should.eql(['parent before', 'before'+ , 'before test one', 'one', 'after', 'parent after', 'parent before'- , 'before']);+ , 'before'+ , 'before test two']);calls.push('two');})@@ -36,16 +44,19 @@ describe('serial', function(){calls.should.eql(['parent before', 'before'+ , 'before test one', 'one', 'after', 'parent after', 'parent before', 'before'+ , 'before test two', 'two', 'after', 'parent after', 'parent before'- , 'before']);+ , 'before'+ , 'before test three']);calls.push('three');})@@ -57,16 +68,19 @@ describe('serial', function(){calls.should.eql(['parent before', 'before'+ , 'before test one', 'one', 'after', 'parent after', 'parent before', 'before'+ , 'before test two', 'two', 'after', 'parent after', 'parent before', 'before'+ , 'before test three', 'three', 'after', 'parent after']);diff --git a/test/hook.sync.nested.js b/test/hook.sync.nested.jsindex 22907a5b..0a040b7b 100644--- a/test/hook.sync.nested.js+++ b/test/hook.sync.nested.js@@ -5,6 +5,9 @@ describe('serial', function(){beforeEach(function(){calls.push('parent before');+ if (this.currentTest) {+ calls.push('parent before test ' + this.currentTest.title);+ }})afterEach(function(){@@ -12,49 +15,66 @@ describe('serial', function(){});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 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 before'+ , 'parent before test bar', 'parent after', '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 before'+ , 'parent before test bar', 'parent after', 'parent before'+ , 'parent before test one', 'before'+ , 'before test one', 'one', 'after', 'parent after', 'parent before'- , 'before']);+ , 'parent before test two'+ , 'before'+ , 'before test two']);calls.push('two');});