From c9def140ee0f7d3d44dc9c3e2b420192090d1c90 Mon Sep 17 00:00:00 2001 From: James Halliday Date: Wed, 1 May 2013 23:07:04 -0700 Subject: [PATCH] no longer getting already closed errors by tracking the number of running tests --- lib/results.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/results.js b/lib/results.js index 8aca786e..d5de6124 100644 --- a/lib/results.js +++ b/lib/results.js @@ -16,9 +16,8 @@ module.exports = function (test) { nextTick(function next () { var t = results.tests.shift(); - if (!t && results.subtests) return; + if (!t && results.running) return; if (!t) return results.close(); - t.on('end', next); t.run(); }); @@ -31,13 +30,14 @@ function Results (stream) { this.pass = 0; this.stream = stream; this.tests = []; - this.subtests = 0; + this.running = 0; } Results.prototype.push = function (t, parentT) { var self = this; var write = function (s) { self.stream.queue(s) }; t.on('prerun', function () { + self.running ++; write('# ' + t.name + '\n'); }); if (parentT) { @@ -52,10 +52,9 @@ Results.prototype.push = function (t, parentT) { var subtests = 0; t.on('test', function (st) { subtests ++; - self.subtests ++; st.on('end', function () { + self.running --; subtests --; - self.subtests --; if (subtests === 0 && !plan) t.emit('end'); nextTick(function () { onend.call(t) }); }); @@ -79,16 +78,14 @@ Results.prototype.push = function (t, parentT) { function onend () { if (this.ended) return; if (subtests !== 0) return; - if (!plan && self.tests.length === 0) { - nextTick(function () { - if (!plan && self.tests.length === 0) { - self.close(); - } - }); + self.running --; + + if (!self.running && self.tests.length === 0) { + self.close(); } - else if (!plan && self.tests.length) { + else if (!self.running) { var t = self.tests.shift(); - nextTick(function () { t.run() }); + t.run(); } } };