Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Test hooks.

  • Loading branch information...
commit d890100e656d8c8d6c56a4e8dcd615c638b8ffbe 1 parent 73db284
Michelle Steigerwalt authored
Showing with 93 additions and 9 deletions.
  1. +39 −1 README.md
  2. +9 −2 describe.js
  3. +45 −6 tests/tests.js
40 README.md
View
@@ -121,4 +121,42 @@ call this.
### describe.logResults
-Gets the results and outputs them either to the DOM or the console.
+Gets the results and outputs them either to the DOM or the console.
+
+### Test Hooks
+
+Each test group supports beforeEach, afterEach, beforeAll, and afterAll as
+test hooks.
+
+#### Example
+
+ (function() {
+
+ var arr = [], bowties;
+
+ describe('array stuff', {
+ beforeAll: function() {
+ bowties = 'cool';
+ },
+ beforeEach: function() {
+ arr = [1,2,3];
+ },
+ afterEach: function() {
+ arr = [];
+ },
+ afterAll: function() {
+ tests = null;
+ },
+ 'bowties are cool': function() {
+ this.expect(bowties, 'cool');
+ },
+ 'arrays have three things': function() {
+ this.expect(arr.length, 3);
+ arr[arr.length] = 5;
+ },
+ 'arrays still have three things': function() {
+ this.expect(arr.length, 3);
+ }
+ });
+
+ }());
11 describe.js
View
@@ -105,14 +105,19 @@
Group.prototype.execute = function(callback) {
var pending = 0, results = {}, my = this, errors = {},
- total = 0, passed = 0;
+ total = 0, passed = 0,
+ hooks = {beforeEach:0, beforeAll:0, afterEach:0, afterAll:0};
for (var name in this.tests) {
- if (this.tests.hasOwnProperty(name)) pending++;
+ if (hooks[name]!==undefined) hooks[name] = this.tests[name];
+ else if (this.tests.hasOwnProperty(name)) pending++;
}
+ if (hooks.beforeAll) hooks.beforeAll();
for (name in this.tests) {
+ if (hooks[name]!==undefined) continue;
if (this.tests.hasOwnProperty(name)) (function(name){
+ if (hooks.beforeEach) hooks.beforeEach();
var returned = false;
total++;
runTest(my.tests[name], function(error) {
@@ -137,7 +142,9 @@
}
}, my.options);
}(name));
+ if (hooks.afterEach) hooks.afterEach();
}
+ if (hooks.afterAll) hooks.afterAll();
};
51 tests/tests.js
View
@@ -34,7 +34,11 @@ function failedPromise() {
};
}
-function promiseTimout() {
+function throwError() {
+ throw new Error("expected error");
+}
+
+function promiseTimeout() {
return {
thing: function(success, failure) {
setTimeout(success, 1000);
@@ -42,10 +46,6 @@ function promiseTimout() {
}
}
-function asyncNodeError(callback) {
- callback('test error', null);
-}
-
function asyncNodeData(callback) {
callback(null, 'data');
}
@@ -113,4 +113,43 @@ describe("promise callback style", {
}, {
callbackMode: 'promises'
-});
+});
+
+(function() {
+
+ var arr = [], bowties;
+
+ describe('test hooks', {
+ beforeAll: function() {
+ bowties = 'cool';
+ },
+ beforeEach: function() {
+ arr[arr.length] = 1;
+ arr[arr.length] = 2;
+ arr[arr.length] = 3;
+ },
+ afterEach: function() {
+ arr = [];
+ },
+ afterAll: function() {
+ bowties = null;
+ },
+ 'bowties are cool [beforeAll]': function() {
+ this.expect(bowties, 'cool');
+ },
+ 'arrays have three things [beforeEach]': function() {
+ this.expect(arr.length, 3);
+ arr[arr.length] = 5;
+ },
+ 'arrays still have three things [afterEach]': function() {
+ this.expect(arr.length, 3);
+ }
+ });
+
+ describe('final test hook test', {
+ "bowties aren't cool [afterAll]": function() {
+ this.expect(bowties, null);
+ }
+ });
+
+}());
Please sign in to comment.
Something went wrong with that request. Please try again.