diff --git a/lib/results.js b/lib/results.js index 37bb1917..ab7850a4 100644 --- a/lib/results.js +++ b/lib/results.js @@ -7,6 +7,7 @@ var through = require('through'); var resumer = require('resumer'); var inspect = require('object-inspect'); var callBound = require('call-bind/callBound'); +var debounce = require('debounce'); var has = require('has'); var regexpTest = callBound('RegExp.prototype.test'); var $split = callBound('String.prototype.split'); @@ -81,6 +82,14 @@ Results.prototype.createStream = function (opts) { self._stream.pipe(output); } + self._end(); + + return output; +}; + +Results.prototype._end = debounce(function () { + var self = this; + if (!this._isRunning) { this._isRunning = true; nextTick(function next() { @@ -92,15 +101,14 @@ Results.prototype.createStream = function (opts) { self.emit('done'); }); } - - return output; -}; +}, 1); Results.prototype.push = function (t) { var self = this; $push(self.tests, t); self._watch(t); self.emit('_push', t); + self._end(); }; Results.prototype.only = function (t) { diff --git a/package.json b/package.json index 21e14946..6e082960 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ }, "dependencies": { "call-bind": "^1.0.0", + "debounce": "^1.2.0", "deep-equal": "^2.0.5", "defined": "^1.0.0", "dotignore": "^0.1.2", diff --git a/test/async.js b/test/async.js new file mode 100644 index 00000000..f643b5ad --- /dev/null +++ b/test/async.js @@ -0,0 +1,37 @@ +var tape = require('../'); +var tap = require('tap'); +var concat = require('concat-stream'); + +tap.test('async test calls', function (tt) { + tt.plan(1); + + var test = tape.createHarness(); + + test.createStream().pipe(concat(function(){})); // Ignore output + + function run1(callback){ + test('first', function (t) { + t.plan(1); + + t.pass(); + + setTimeout(callback, 10); + }); + } + + function run2(callback){ + test('second', function (t) { + t.plan(1); + + t.pass(); + + setTimeout(callback, 10); + }); + } + + run1(function(){ + run2(function(){ + tt.pass(); + }); + }); +});