Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Added tests and stubs for .before() / .after()

Tj Holowaychuk1e93ae0a4cd7
diff --git a/lib/interfaces/bdd.js b/lib/interfaces/bdd.js
index d4a29417..ed6a1856 100644
--- a/lib/interfaces/bdd.js
+++ b/lib/interfaces/bdd.js
@@ -28,6 +28,22 @@ module.exports = function(suite){
suite.on('pre-require', function(context, file){
+ /**
+ * Execute before running tests.
+ */
+
+ context.before = function(fn){
+ suites[0].beforeAll(fn);
+ };
+
+ /**
+ * Execute after running tests.
+ */
+
+ context.after = function(fn){
+ suites[0].afterAll(fn);
+ };
+
/**
* Execute before each test case.
*/
diff --git a/lib/suite.js b/lib/suite.js
index 6e29c018..78f7d5dd 100644
--- a/lib/suite.js
+++ b/lib/suite.js
@@ -22,7 +22,9 @@ function Suite(title) {
this.title = title;
this.suites = [];
this.tests = [];
+ this.beforeAllCallbacks = [];
this.beforeEachCallbacks = [];
+ this.afterAllCallbacks = [];
this.afterEachCallbacks = [];
}
@@ -32,6 +34,31 @@ function Suite(title) {
Suite.prototype.__proto__ = EventEmitter.prototype;
+/**
+ * Run `fn(test[, done])` before running tests.
+ *
+ * @param {Function} fn
+ * @return {Suite} for chaining
+ * @api private
+ */
+
+Suite.prototype.beforeAll = function(fn){
+ this.beforeAllCallbacks.push(fn);
+ return this;
+}
+/**
+ * Run `fn(test[, done])` after running tests.
+ *
+ * @param {Function} fn
+ * @return {Suite} for chaining
+ * @api private
+ */
+
+Suite.prototype.afterAll = function(fn){
+ this.afterAllCallbacks.push(fn);
+ return this;
+}
+
/**
* Run `fn(test[, done])` before each test case.
*
diff --git a/test/unit/hook.async.js b/test/unit/hook.async.js
index ce62852e..5e271fe5 100644
--- a/test/unit/hook.async.js
+++ b/test/unit/hook.async.js
@@ -3,7 +3,15 @@ describe('async', function(){
var calls = [];
process.on('exit', function(){
- calls.should.have.length(6);
+ calls.should.eql([
+ 'before all'
+ , 'before one'
+ , 'after one'
+ , 'before two'
+ , 'after two'
+ , 'before three'
+ , 'after three'
+ , 'after all']);
})
beforeEach(function(test, done){
@@ -17,6 +25,14 @@ describe('async', function(){
})
describe('beforeEach()', function(){
+ before(function(){
+ calls.push('before all');
+ });
+
+ after(function(){
+ calls.push('after all');
+ });
+
beforeEach(function(test, done){
process.nextTick(function(){
calls.push('before ' + test.title);
@@ -25,20 +41,24 @@ describe('async', function(){
})
it('one', function(done){
- calls.should.eql(['before one']);
+ calls.should.eql([
+ 'before all'
+ , 'before one']);
process.nextTick(done);
})
it('two', function(){
calls.should.eql([
- 'before one'
+ 'before all'
+ , 'before one'
, 'after one'
, 'before two']);
})
it('three', function(){
calls.should.eql([
- 'before one'
+ 'before all'
+ , 'before one'
, 'after one'
, 'before two'
, 'after two'
@@ -52,4 +72,4 @@ describe('async', function(){
})
})
})
-})
\ No newline at end of file
+})