Permalink
Browse files

Adding Before and After suite

  • Loading branch information...
phstc committed Jul 2, 2012
1 parent f0d2ecc commit d5b40e85e924e06cca560b4d8d6a264528bbab2b
Showing with 80 additions and 12 deletions.
  1. +46 −12 lib/vows/suite.js
  2. +34 −0 test/vows-suite-test.js
View
@@ -11,6 +11,8 @@ this.Suite = function (subject) {
this.batches = [];
this.options = { error: true };
this.reset();
+ this.beforeSuiteFunc = null;
+ this.afterSuiteFunc = null;
};
this.Suite.prototype = new(function () {
@@ -256,7 +258,30 @@ this.Suite.prototype = new(function () {
return this.reporter.report.apply(this.reporter, arguments);
};
- this.run = function (options, callback) {
+ this.beforeSuite = function(func){
+ this.beforeSuiteFunc = func;
+ return this;
+ }
+
+ this.afterSuite = function(func){
+ this.afterSuiteFunc = func;
+ return this;
+ }
+
+ this.run = function (options, callback) {
+ var that = this;
+ this.done = function(){
+ that.runWithin(options, callback);
+ }
+ // If there is a beforeSuite defined, the tests will run only after this.done() was called.
+ if(typeof(this.beforeSuiteFunc) === 'function') {
+ this.beforeSuiteFunc.call(this);
+ return;
+ }
+ this.done();
+ }
+
+ this.runWithin = function (options, callback) {
var that = this, start;
options = options || {};
@@ -294,16 +319,25 @@ this.Suite.prototype = new(function () {
});
}
} else {
- that.results.time = (new(Date) - start) / 1000;
- that.report(['finish', that.results]);
-
- if (callback) { callback(that.results) }
-
- if (that.results.honored + that.results.pending === that.results.total) {
- return 0;
- } else {
- return 1;
- }
+ (function(){
+ this.done = function(){
+ that.results.time = (new(Date) - start) / 1000;
+ that.report(['finish', that.results]);
+
+ if (callback) { callback(that.results) }
+
+ if (that.results.honored + that.results.pending === that.results.total) {
+ return 0;
+ } else {
+ return 1;
+ }
+ }
+ if(typeof(that.afterSuiteFunc) === 'function') {
+ that.afterSuiteFunc.call(this);
+ return;
+ }
+ this.done();
+ })();
}
})(this.batches.slice(0));
};
@@ -318,7 +352,7 @@ this.Suite.prototype = new(function () {
});
if (require.main === module) {
- return this.run();
+ return this.runWithin();
} else {
return module.exports[this.subject] = this;
}
View
@@ -0,0 +1,34 @@
+var assert = require('assert'),
+ vows = require('../lib/vows');
+
+
+(function(){
+ var changed = false;
+ vows.describe('before suite').addBatch({
+ 'with beforeSuite': {
+ topic: function(){
+ this.callback(changed);
+ },
+ 'should call before the batches': function(topic){
+ assert.isTrue(topic);
+ }
+ }
+ }).beforeSuite(function(){
+ changed = true;
+ this.done();
+ }).afterSuite(function(){
+ changed = false;
+ this.done();
+ }).export(module);
+
+ vows.describe('after suite').addBatch({
+ 'with afterSuite': {
+ topic: function(){
+ return changed;
+ },
+ 'should call after the batches': function(topic){
+ assert.isFalse(topic);
+ }
+ }
+ }).export(module);
+})();

0 comments on commit d5b40e8

Please sign in to comment.