Skip to content

Commit

Permalink
Add support for asynchronous teardowns.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Brockman authored and indexzero committed Nov 25, 2011
1 parent a44ee69 commit f39a4e2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
41 changes: 35 additions & 6 deletions lib/vows/suite.js
Expand Up @@ -324,16 +324,45 @@ this.tryEnd = function (batch) {
(k in batch.suite.results) && (batch.suite.results[k] += batch[k]);
});

// Run teardowns
if (batch.teardowns) {
for (var i = batch.teardowns.length - 1, ctx; i >= 0; i--) {
ctx = batch.teardowns[i];
ctx.tests.teardown.apply(ctx.env, ctx.topics);
runTeardown(batch.teardowns[i]);
}

maybeFinish();
}

function runTeardown(teardown) {
var env = Object.create(teardown.env);

Object.defineProperty(env, "callback", {
get: function () {
teardown.awaitingCallback = true;

return function () {
teardown.awaitingCallback = false;
maybeFinish();
};
}
})

teardown.tests.teardown.apply(env, teardown.topics);
}

batch.status = 'end';
batch.suite.report(['end']);
batch.promise.emit('end', batch.honored, batch.broken, batch.errored, batch.pending);
function maybeFinish() {
var pending = batch.teardowns.filter(function (teardown) {
return teardown.awaitingCallback;
});

if (pending.length === 0) {
finish();
}
}

function finish() {
batch.status = 'end';
batch.suite.report(['end']);
batch.promise.emit('end', batch.honored, batch.broken, batch.errored, batch.pending);
}
}
};
22 changes: 22 additions & 0 deletions test/vows-test.js
Expand Up @@ -459,3 +459,25 @@ vows.describe("Vows with sub events").addBatch({
}
}
}).export(module);

var tornDown = false

vows.describe("Vows with asynchonous teardowns").addBatch({
"Context with long-running teardown": {
"is run first": function () {},
teardown: function () {
var callback = this.callback;

setTimeout(function () {
tornDown = true;
callback();
}, 100);
}
}
}).addBatch({
"The next batch": {
"is not run until the teardown is complete": function () {
assert.ok(tornDown)
}
}
}).export(module);

0 comments on commit f39a4e2

Please sign in to comment.