Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

beforeAll() and afterAll() functions

  • Loading branch information...
commit 92092215c25c9aff7a4d6295a9406e13f0e72604 1 parent 44173cc
@joelplane joelplane authored
View
11 context.js
@@ -1,4 +1,4 @@
-module.exports = (function(Test){
+module.exports = (function(Test, Setup){
var events = require('./events');
var EventEmitter = events.EventEmitter;
var sys = require('sys');
@@ -15,6 +15,7 @@ module.exports = (function(Test){
this._parentContext = parentContext;
this._subContexts = [];
this._tests = [];
+ this._setups = {};
EventEmitter.call(this);
/* Rename emit() method from EventEmitter to indicate that it is private */
@@ -56,6 +57,14 @@ module.exports = (function(Test){
Context.prototype.it = function(name, callback) {
this._tests.push(new Test(this, name, callback));
};
+ Context.prototype.beforeAll = function(callback) {
+ this._setups['beforeAll'] = this._setups['beforeAll'] || [];
+ this._setups['beforeAll'].push(new Setup(this, 'beforeAll', callback));
+ };
+ Context.prototype.afterAll = function(callback) {
+ this._setups['afterAll'] = this._setups['afterAll'] || [];
+ this._setups['afterAll'].push(new Setup(this, 'afterAll', callback));
+ };
Context.prototype._depth = function() {
var parent = this;
var i = 0;
View
65 examples/passing/setupAndTeardown.js
@@ -0,0 +1,65 @@
+var sys = require('sys');
+var test = require('../../testnode')();
+test.onFailureExitNonZero();
+
+/**
+ * This is a simple exmaple of the 'beforeAll' and 'afterAll' functions.
+ *
+ * IMPORTANT NOTE: The names of the functions are just to aid readability,
+ * but they do not accurately convey how the functions work:
+ *
+ * The beforeAll and afterAll functions are actually indentical.
+ * When they are run is actually dependent on their position in the
+ * code. For instance, if you to change his example and rename 'beforeAll'
+ * to 'afterAll', and rename the 'afterAll' to 'beforeAll', it would
+ * behave identically.
+ *
+ * Likewise, if you were to move the afterAll function to the top of the
+ * context block, you would change the behaviour. The afterAll function
+ * would execute BEFORE the tests.
+ */
+
+test.context("Example Test Suite", function() {
+ this.context("Example of beforeAll and afterAll", function() {
+ var a;
+
+ this.beforeAll(function(done){
+ a = 0;
+ done();
+ });
+
+ this.it("Test beforeAll has been called", function(test) {
+ a = a + 1;
+ test.assertEqual(1, a); // 'a' will be NaN if beforeAll function is not called
+ test.done();
+ });
+
+ this.it("Test beforeAll has been called once, followed by a test", function(test) {
+ a = a + 1;
+ test.assertEqual(2, a);
+ test.done();
+ });
+
+ /* You can't make assertion inside the afterAll function.
+ * If you need to, use a test (.it()) instead of afterAll()
+ */
+ this.afterAll(function(done){
+ global.someSideEffect = true;
+ sys.puts('setupAndTeardown.js: a = ' + a);
+ done();
+ });
+
+ });
+ this.context("Another context", function() {
+
+ /*
+ * The previous context's afterAll function will have been called before we
+ * get here.
+ */
+ this.it("Previous context's afterAll function was called", function(test) {
+ test.assert(global.someSideEffect);
+ test.done();
+ });
+
+ });
+});
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "testnode",
"description": "Testing library for Node",
- "version": "0.1.2",
+ "version": "0.1.3",
"dependencies" : {
"glob": "",
"ansi-color": ""
View
35 setup.js
@@ -0,0 +1,35 @@
+module.exports = (function(testQueue){
+ var events = require('./events');
+ var EventEmitter = events.EventEmitter;
+ var sys = require('sys');
+
+ var Setup = function(context, type, setupFunction) {
+ this._context = context;
+ this._type = type;
+ this._setupFunction = setupFunction;
+ EventEmitter.call(this);
+
+ /* Rename emit() method from EventEmitter to indicate that it is private */
+ this._emit = this.emit;
+ delete this['emit'];
+
+ Setup.emit('new', this);
+ var self = this;
+ // must do this next tick so context can listen to our events beforehand
+ process.nextTick(function(){
+ testQueue.put(self);
+ });
+ };
+ sys.inherits(Setup, EventEmitter);
+ events.classEvents(Setup);
+ Setup.prototype._call = function() {
+ var setup = this;
+ this._emit('setupStarted', this);
+ var resumeFunction = function() {
+ setup._emit('setupDone', setup);
+ };
+ this._setupFunction.call(this, resumeFunction);
+ };
+
+ return Setup;
+});
View
6 testQueue.js
@@ -14,9 +14,11 @@ module.exports = (function() {
if (q.array.length > 0) {
var top = q.array[0];
q.array = q.array.slice(1);
- top.on('testDone', function(){
+ var onDone = function() {
q.next();
- });
+ };
+ top.on('testDone', onDone);
+ top.on('setupDone', onDone);
top._call();
} else {
begun = false;
View
8 testnode.js
@@ -29,13 +29,17 @@ module.exports = (function(){
var testQueue = require('./testQueue')();
var Test = require('./test')(Assertion, testQueue, config.timeout);
require('./assertions')(Assertion);
- var Context = require('./context')(Test);
+ var Setup = require('./setup')(testQueue);
+ var Context = require('./context')(Test, Setup);
var main = new EventEmitter();
- /* Relay events emitted by Test and Context instances to the main object */
+ /* Relay events emitted to the main object */
Test.on('new', function(t){
events.relayEvents(t, main, ['assertionPassed', 'assertionFailed', 'testFlunk', 'testStarted', 'testTimeout', 'testDone']);
});
+ Setup.on('new', function(s){
+ events.relayEvents(s, main, ['setupStarted', 'setupDone']);
+ });
Context.on('new', function(ctx){
events.relayEvents(ctx, main, ['pushContext', 'popContext']);
});
Please sign in to comment.
Something went wrong with that request. Please try again.