Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Added before[Each]/after[Each] support to exports interface

Tj Holowaychukb9f80c471bdd
diff --git a/lib/interfaces/exports.js b/lib/interfaces/exports.js
index 31d676d5..f90e87d3 100644
--- a/lib/interfaces/exports.js
+++ b/lib/interfaces/exports.js
@@ -32,7 +32,23 @@ module.exports = function(suite){
var suite;
for (var key in obj) {
if ('function' == typeof obj[key]) {
- suites[0].addTest(new Test(key, obj[key]));
+ var fn = obj[key];
+ switch (key) {
+ case 'before':
+ suites[0].beforeAll(fn);
+ break;
+ case 'after':
+ suites[0].afterAll(fn);
+ break;
+ case 'beforeEach':
+ suites[0].beforeEach(fn);
+ break;
+ case 'afterEach':
+ suites[0].afterEach(fn);
+ break;
+ default:
+ suites[0].addTest(new Test(key, fn));
+ }
} else {
suites[0].addSuite(suite = new Suite(key));
suites.unshift(suite);
diff --git a/test/exports.js b/test/exports.js
index eb15f2ad..8746cbcc 100644
--- a/test/exports.js
+++ b/test/exports.js
@@ -1,12 +1,41 @@
+var calls = [];
+
exports.Array = {
+ before: function(){
+ calls.push('before');
+ },
+
+ after: function(){
+ calls.push('after');
+ calls.should.eql([
+ 'before'
+ , 'before each'
+ , 'one'
+ , 'after each'
+ , 'before each'
+ , 'two'
+ , 'after each'
+ , 'after']);
+ },
+
'#indexOf()': {
+ beforeEach: function(){
+ calls.push('before each');
+ },
+
+ afterEach: function(){
+ calls.push('after each');
+ },
+
'should return -1 when the value is not present': function(){
+ calls.push('one');
[1,2,3].indexOf(5).should.equal(-1);
[1,2,3].indexOf(0).should.equal(-1);
},
'should return the correct index when the value is present': function(){
+ calls.push('two');
[1,2,3].indexOf(1).should.equal(0);
[1,2,3].indexOf(2).should.equal(1);
[1,2,3].indexOf(3).should.equal(2);