Skip to content

armgit / mocha

Public Imported from GitHub
0

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

tear down for tdd style

James Carre692aae2cb9a
diff --git a/lib/interfaces/tdd.js b/lib/interfaces/tdd.js
index 0f43d18d..1eab5023 100644
--- a/lib/interfaces/tdd.js
+++ b/lib/interfaces/tdd.js
@@ -35,6 +35,15 @@ module.exports = function(suite){
context.setup = function(fn){
suites[0].addSetup(fn);
}
+
+ /**
+ * Defines a tearDown function to execute after
+ * the execution of each test.
+ */
+
+ context.tearDown = function(fn){
+ suites[0].addTearDown(fn);
+ }
/**
* Describe a "suite" with the given `title`
* and callback `fn` containing nested suites
diff --git a/lib/runner.js b/lib/runner.js
index ff8b7d2f..78e443b9 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -94,6 +94,10 @@ Runner.prototype.runTests = function(suite, fn){
// run the next test
if (test.sync) {
self.emit('test end', test);
+ // teardown hook
+ suite.tearDown.forEach(function(fn){
+ fn();
+ });
run(tests.shift());
}
}
diff --git a/lib/suite.js b/lib/suite.js
index af5bad24..19f1a973 100644
--- a/lib/suite.js
+++ b/lib/suite.js
@@ -19,10 +19,11 @@ module.exports = Suite;
*/
function Suite(title) {
- this.title = title;
- this.suites = [];
- this.tests = [];
- this.setup = [];
+ this.title = title;
+ this.suites = [];
+ this.tests = [];
+ this.setup = [];
+ this.tearDown = [];
}
/**
@@ -42,6 +43,17 @@ Suite.prototype.addSetup = function(setup){
this.setup.push(setup);
return this;
}
+/**
+ * Add a test `tearDown`.
+ *
+ * @param {Function} fn
+ * @return {Suite} for chaining
+ * @api private
+ */
+Suite.prototype.addTearDown = function(tearDown){
+ this.tearDown.push(tearDown);
+ return this;
+}
/**
* Add a test `suite`.