☕️ Classic, reliable, trusted test framework for Node.js and the browser
before works in bdd style specs
James Carrd433a3ba7bbd
diff --git a/lib/interfaces/bdd.js b/lib/interfaces/bdd.jsindex ac825085..bc35d89f 100644--- a/lib/interfaces/bdd.js+++ b/lib/interfaces/bdd.js@@ -25,15 +25,24 @@ var Suite = require('../suite')module.exports = function(suite){var suites = [suite];-+suite.on('pre-require', function(context, file){+++ /**+ * Do this before each example+ *+ */+ context.before = function(fn){+ suites[0].addSetup(fn);+ };/*** Describe a "suite" with the given `title`* and callback `fn` containing nested suites* and/or tests.*/-+context.describe = function(title, fn){var suite = suite = new Suite(title);suites[0].addSuite(suite);@@ -52,4 +61,4 @@ module.exports = function(suite){suites[0].addTest(new Test(title, fn));};});-};\ No newline at end of file+};diff --git a/lib/runner.js b/lib/runner.jsindex 2dab1e30..8810ac76 100644--- a/lib/runner.js+++ b/lib/runner.js@@ -55,7 +55,10 @@ Runner.prototype.runTests = function(suite, fn){function run(test) {if (!test) return fn();self.emit('test', test);-+ // run any befores first+ suite.setup.forEach(function(fn){+ fn();+ });// run the testtry {// asyncdiff --git a/lib/suite.js b/lib/suite.jsindex c384d580..af5bad24 100644--- a/lib/suite.js+++ b/lib/suite.js@@ -22,6 +22,7 @@ function Suite(title) {this.title = title;this.suites = [];this.tests = [];+ this.setup = [];}/**@@ -30,6 +31,18 @@ function Suite(title) {Suite.prototype.__proto__ = EventEmitter.prototype;+/**+ * Add a test `setup`.+ *+ * @param {Function} fn+ * @return {Suite} for chaining+ * @api private+ */+Suite.prototype.addSetup = function(setup){+ this.setup.push(setup);+ return this;+}+/*** Add a test `suite`.*diff --git a/test/setup-test.js b/test/setup-test.jsindex 5df92f67..2f14fc3c 100644--- a/test/setup-test.js+++ b/test/setup-test.js@@ -1,9 +1,12 @@var assert = require('assert');describe("Setup", function(){-+ var x = 0;+ before(function(){+ x = 1;+ });it("should have executed before", function(){- assert.equal([1,2,3].indexOf(4), -1);+ assert.equal(x, 1);})});