-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
137 lines (115 loc) Β· 3.2 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const { Parser } = require("tap-parser");
const Through = require("through2");
const Duplexer = require("duplexer");
module.exports = function tapHTML(callback) {
const tap = new Parser();
const out = Through.obj();
const dup = Duplexer(tap, out);
let currentPlan = -1;
let currentAssertion = -1;
let data = [];
let plan = null;
const startTime = Date.now();
function pushTest(name) {
data.push({
type: "test",
name: name,
start: Date.now(),
assertions: [],
});
// get the current index of the plan
// so that we can use this to push the current assertions to it
currentPlan += 1;
currentAssertion = -1;
}
tap.on("comment", (res) => {
if (!plan) {
pushTest(res);
}
});
tap.on("plan", (res) => {
if (typeof res !== "string") return;
plan = res;
});
tap.on("extra", (res) => {
if (data && currentPlan > 0 && currentAssertion > 0) {
data[currentPlan]["assertions"][currentAssertion][
"console"
] += `${res}\n`;
}
});
tap.on("assert", (res) => {
// If no plan is registered yet, create a default plan.
if (currentPlan == -1) {
pushTest("default");
}
// TAP does not require a name. If no name is registered, set a default name.
if (!res.name) {
res.name = "test #" + res.id;
}
data[currentPlan].assertions.push({
type: "assert",
number: res.id,
name: res.name,
ok: res.ok,
diag: res.diag,
console: "",
end: Date.now(),
});
currentAssertion += 1;
});
tap.on("complete", (res) => {
res["time"] = Date.now() - startTime;
var plan = -1;
// combine and clean up tests
for (var i = 0; i < data.length; i++) {
// trims the name from having any extra new line breaks
data[i].name = data[i].name.trim();
// This is a top level plan
if (data[i].assertions.length === 0) {
// move on with the tests
plan = i;
data[plan].tests = [];
delete data[i].assertions;
} else if (plan === -1) {
// this is flat plan that has no parent do nothing
} else {
// We know this is part of the currentPlan
if (!data[plan]) {
data[plan] = {
tests: [],
};
} else {
data[plan].tests = data[plan].tests || [];
}
data[plan].tests.push(data[i]);
delete data[i];
}
}
data = data.filter((d) => d > "");
function calculateTime(test) {
if (test.end) return;
test.end = test.assertions[test.assertions.length - 1].end;
test.assertions.forEach((assertion) => {
assertion.start = test.start;
});
}
data.forEach((plan) => {
if (plan.tests && plan.tests.length === 0) {
// this is an empty test
plan.end = plan.start;
} else if (plan.tests && plan.tests.length > 0) {
for (var i = 0; i < plan.tests.length; i++) {
calculateTime(plan.tests[i]);
}
plan.end = plan.tests[plan.tests.length - 1].end;
} else {
// this is a flat test with only assertions
calculateTime(plan);
}
});
res["tests"] = data;
callback(res);
});
return dup;
};