Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async test setup #235

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions lib/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var bind = require('function-bind');
var has = require('has');
var regexpTest = bind.call(Function.call, RegExp.prototype.test);
var yamlIndicators = /\:|\-|\?/;
var debounce = require('debounce');
var nextTick = typeof setImmediate !== 'undefined'
? setImmediate
: process.nextTick
Expand Down Expand Up @@ -63,16 +64,23 @@ Results.prototype.createStream = function (opts) {
output.queue('TAP version 13\n');
self._stream.pipe(output);
}

nextTick(function next() {
var t;
while (t = getNextTest(self)) {
t.run();
if (!t.ended) return t.once('end', function(){ nextTick(next); });
}
self.emit('done');
});



var end = debounce(function () {
nextTick(function(){
var t;
while (t = getNextTest(self)) {
t.run();

if (!t.ended) return t.once('end', end);
}

self.emit('done');
});
}, opts.endWaitTime || 0);

end();

return output;
};

Expand All @@ -93,7 +101,7 @@ Results.prototype._watch = function (t) {
t.once('prerun', function () {
write('# ' + t.name + '\n');
});

t.on('result', function (res) {
if (typeof res === 'string') {
write('# ' + res + '\n');
Expand All @@ -105,7 +113,7 @@ Results.prototype._watch = function (t) {
if (res.ok) self.pass ++
else self.fail ++
});

t.on('test', function (st) { self._watch(st) });
};

Expand All @@ -114,7 +122,7 @@ Results.prototype.close = function () {
if (self.closed) self._stream.emit('error', new Error('ALREADY CLOSED'));
self.closed = true;
var write = function (s) { self._stream.queue(s) };

write('\n1..' + self.count + '\n');
write('# tests ' + self.count + '\n');
write('# pass ' + self.pass + '\n');
Expand All @@ -128,22 +136,22 @@ function encodeResult (res, count) {
var output = '';
output += (res.ok ? 'ok ' : 'not ok ') + count;
output += res.name ? ' ' + res.name.toString().replace(/\s+/g, ' ') : '';

if (res.skip) output += ' # SKIP';
else if (res.todo) output += ' # TODO';

output += '\n';
if (res.ok) return output;

var outer = ' ';
var inner = outer + ' ';
output += outer + '---\n';
output += inner + 'operator: ' + res.operator + '\n';

if (has(res, 'expected') || has(res, 'actual')) {
var ex = inspect(res.expected);
var ac = inspect(res.actual);

if (Math.max(ex.length, ac.length) > 65 || invalidYaml(ex) || invalidYaml(ac)) {
output += inner + 'expected: |-\n' + inner + ' ' + ex + '\n';
output += inner + 'actual: |-\n' + inner + ' ' + ac + '\n';
Expand All @@ -163,7 +171,7 @@ function encodeResult (res, count) {
output += inner + ' ' + lines[i] + '\n';
}
}

output += outer + '...\n';
return output;
}
Expand All @@ -172,7 +180,7 @@ function getNextTest (results) {
if (!results._only) {
return results.tests.shift();
}

do {
var t = results.tests.shift();
if (!t) continue;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"test": "test"
},
"dependencies": {
"debounce": "^1.0.0",
"deep-equal": "~1.0.0",
"defined": "~1.0.0",
"function-bind": "~1.0.2",
Expand Down
37 changes: 37 additions & 0 deletions test/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var tape = require('../');
var tap = require('tap');

tap.test('async test calls', function (tt) {
tt.plan(1);

var test = tape.createHarness();
var tc = tap.createConsumer();

test.createStream({endWaitTime: 20}).pipe(tc);

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();
});
});
});