-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
54 lines (43 loc) · 1.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
var parser = require('tap-parser');
var assign = require('object.assign');
module.exports = function (opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
if (!opts) { opts = {}; }
if (opts.wait === undefined) { opts.wait = 1000; }
var p = parser();
var seen = { plan: null, asserts: [] };
var finished = false;
var ended = false;
function finish() {
finished = true;
p.on('complete', function (finalResult) {
cb(assign({}, finalResult, { asserts: seen.asserts }));
});
if (opts.wait && !ended) {
setTimeout(function () { p.end(); }, opts.wait);
} else { p.end(); }
}
function check() {
if (finished) { return; }
if (seen.plan === null || seen.asserts.length < seen.plan) { return; }
finish();
}
p.on('end', function () { ended = true; });
p.on('assert', function (result) {
seen.asserts.push(result);
check();
});
p.on('plan', function (plan) {
seen.plan = plan.end - plan.start;
check();
});
p.on('complete', function () {
if (finished) { return; }
finish();
});
return p;
};