Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Support named before/after hooks (fix #966, close #1116)

Jacob Wejendorpfad92887921e
diff --git a/lib/suite.js b/lib/suite.js
index 869bb88d..d403a83f 100644
--- a/lib/suite.js
+++ b/lib/suite.js
@@ -140,9 +140,15 @@ Suite.prototype.bail = function(bail){
* @api private
*/
-Suite.prototype.beforeAll = function(fn){
+Suite.prototype.beforeAll = function(title, fn){
if (this.pending) return this;
- var hook = new Hook('"before all" hook', fn);
+ if ('function' === typeof title) {
+ fn = title;
+ title = fn.name;
+ }
+ title = '"before all" hook' + (title ? ': ' + title : '');
+
+ var hook = new Hook(title, fn);
hook.parent = this;
hook.timeout(this.timeout());
hook.slow(this.slow());
@@ -160,9 +166,15 @@ Suite.prototype.beforeAll = function(fn){
* @api private
*/
-Suite.prototype.afterAll = function(fn){
+Suite.prototype.afterAll = function(title, fn){
if (this.pending) return this;
- var hook = new Hook('"after all" hook', fn);
+ if ('function' === typeof title) {
+ fn = title;
+ title = fn.name;
+ }
+ title = '"after all" hook' + (title ? ': ' + title : '');
+
+ var hook = new Hook(title, fn);
hook.parent = this;
hook.timeout(this.timeout());
hook.slow(this.slow());
@@ -180,9 +192,15 @@ Suite.prototype.afterAll = function(fn){
* @api private
*/
-Suite.prototype.beforeEach = function(fn){
+Suite.prototype.beforeEach = function(title, fn){
if (this.pending) return this;
- var hook = new Hook('"before each" hook', fn);
+ if ('function' === typeof title) {
+ fn = title;
+ title = fn.name;
+ }
+ title = '"before each" hook' + (title ? ': ' + title : '');
+
+ var hook = new Hook(title, fn);
hook.parent = this;
hook.timeout(this.timeout());
hook.slow(this.slow());
@@ -200,9 +218,15 @@ Suite.prototype.beforeEach = function(fn){
* @api private
*/
-Suite.prototype.afterEach = function(fn){
+Suite.prototype.afterEach = function(title, fn){
if (this.pending) return this;
- var hook = new Hook('"after each" hook', fn);
+ if ('function' === typeof title) {
+ fn = title;
+ title = fn.name;
+ }
+ title = '"after each" hook' + (title ? ': ' + title : '');
+
+ var hook = new Hook(title, fn);
hook.parent = this;
hook.timeout(this.timeout());
hook.slow(this.slow());
diff --git a/test/suite.js b/test/suite.js
index 8cacff8d..e457d64a 100644
--- a/test/suite.js
+++ b/test/suite.js
@@ -132,7 +132,7 @@ describe('Suite', function(){
describe('wraps the passed in function in a Hook', function(){
it('adds it to _beforeAll', function(){
- function fn(){}
+ var fn = function(){};
this.suite.beforeAll(fn);
this.suite._beforeAll.should.have.length(1);
@@ -140,6 +140,23 @@ describe('Suite', function(){
beforeAllItem.title.should.equal('"before all" hook');
beforeAllItem.fn.should.equal(fn);
});
+
+ it('appends title to hook', function(){
+ var fn = function(){};
+ this.suite.beforeAll('test', fn);
+
+ this.suite._beforeAll.should.have.length(1);
+ var beforeAllItem = this.suite._beforeAll[0];
+ beforeAllItem.title.should.equal('"before all" hook: test');
+ beforeAllItem.fn.should.equal(fn);
+
+ function namedFn(){}
+ this.suite.beforeAll(namedFn);
+ this.suite._beforeAll.should.have.length(2);
+ beforeAllItem = this.suite._beforeAll[1];
+ beforeAllItem.title.should.equal('"before all" hook: namedFn');
+ beforeAllItem.fn.should.equal(namedFn);
+ });
});
});
@@ -150,7 +167,7 @@ describe('Suite', function(){
describe('wraps the passed in function in a Hook', function(){
it('adds it to _afterAll', function(){
- function fn(){}
+ var fn = function(){};
this.suite.afterAll(fn);
this.suite._afterAll.should.have.length(1);
@@ -158,6 +175,22 @@ describe('Suite', function(){
afterAllItem.title.should.equal('"after all" hook');
afterAllItem.fn.should.equal(fn);
});
+ it('appends title to hook', function(){
+ var fn = function(){};
+ this.suite.afterAll('test', fn);
+
+ this.suite._afterAll.should.have.length(1);
+ var beforeAllItem = this.suite._afterAll[0];
+ beforeAllItem.title.should.equal('"after all" hook: test');
+ beforeAllItem.fn.should.equal(fn);
+
+ function namedFn(){}
+ this.suite.afterAll(namedFn);
+ this.suite._afterAll.should.have.length(2);
+ beforeAllItem = this.suite._afterAll[1];
+ beforeAllItem.title.should.equal('"after all" hook: namedFn');
+ beforeAllItem.fn.should.equal(namedFn);
+ });
});
});
@@ -168,7 +201,7 @@ describe('Suite', function(){
describe('wraps the passed in function in a Hook', function(){
it('adds it to _beforeEach', function(){
- function fn(){}
+ var fn = function(){};
this.suite.beforeEach(fn);
this.suite._beforeEach.should.have.length(1);
@@ -176,6 +209,23 @@ describe('Suite', function(){
beforeEachItem.title.should.equal('"before each" hook');
beforeEachItem.fn.should.equal(fn);
});
+
+ it('appends title to hook', function(){
+ var fn = function(){};
+ this.suite.beforeEach('test', fn);
+
+ this.suite._beforeEach.should.have.length(1);
+ var beforeAllItem = this.suite._beforeEach[0];
+ beforeAllItem.title.should.equal('"before each" hook: test');
+ beforeAllItem.fn.should.equal(fn);
+
+ function namedFn(){}
+ this.suite.beforeEach(namedFn);
+ this.suite._beforeEach.should.have.length(2);
+ beforeAllItem = this.suite._beforeEach[1];
+ beforeAllItem.title.should.equal('"before each" hook: namedFn');
+ beforeAllItem.fn.should.equal(namedFn);
+ });
});
});
@@ -186,7 +236,7 @@ describe('Suite', function(){
describe('wraps the passed in function in a Hook', function(){
it('adds it to _afterEach', function(){
- function fn(){}
+ var fn = function(){};
this.suite.afterEach(fn);
this.suite._afterEach.should.have.length(1);
@@ -194,6 +244,23 @@ describe('Suite', function(){
afterEachItem.title.should.equal('"after each" hook');
afterEachItem.fn.should.equal(fn);
});
+
+ it('appends title to hook', function(){
+ var fn = function(){};
+ this.suite.afterEach('test', fn);
+
+ this.suite._afterEach.should.have.length(1);
+ var beforeAllItem = this.suite._afterEach[0];
+ beforeAllItem.title.should.equal('"after each" hook: test');
+ beforeAllItem.fn.should.equal(fn);
+
+ function namedFn(){}
+ this.suite.afterEach(namedFn);
+ this.suite._afterEach.should.have.length(2);
+ beforeAllItem = this.suite._afterEach[1];
+ beforeAllItem.title.should.equal('"after each" hook: namedFn');
+ beforeAllItem.fn.should.equal(namedFn);
+ });
});
});