-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (134 loc) · 3.77 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
'use strict'
const { relative } = require('path')
const cross = 'x'
const check = '√'
const color = (code, end) => str => `\x1B[${code}m${str}\x1B[${end}m`
const bold = color(1, 22)
const dim = color(2, 22)
const red = color(31, 39)
const green = color(32, 39)
const pad = count => ' '.repeat(count)
const header = (test, duration, passed) =>
(!passed ? red(`${cross} `) : green(`${check} `)) +
test +
dim(` [${duration}ms]`) +
'\n'
const cleanTermLink = link => link.replace(process.cwd(), '').replace(/^\//, '')
const printEvents = (baseTestEvent, depth = baseTestEvent.depth || 0) => {
const children = baseTestEvent.children || []
const hasErrors = children.find(d => d.hasError)
let output = ''
output +=
pad(depth) +
header(
baseTestEvent.name,
baseTestEvent.diagnostics.duration_ms,
!hasErrors
)
children.forEach(childTestEvents => {
if (!childTestEvents.hasError) return
if (childTestEvents.children) {
output += printEvents(childTestEvents, depth + childTestEvents.depth + 1)
} else {
output +=
pad(depth + childTestEvents.depth + 1) +
red('x ') +
childTestEvents.name +
'\n' +
pad(depth + childTestEvents.depth + 1) +
dim(cleanTermLink(childTestEvents.termLink)) +
'\n' +
red(
pad(depth + childTestEvents.depth + 1) +
new Error(childTestEvents.error).message
.replace(/(\n)/g, '\n ' + pad(depth + childTestEvents.depth + 1))
.toString() +
'\n'
)
}
})
return output
}
// runner
const run = {
tree: [],
create: event => {
const hasError = event.type == 'test:fail'
return {
name: event.data.name,
depth: event.data.nesting,
diagnostics: event.data.details || {},
file: event.data.file,
termLink: `${event.data.file}:${event.data.line}:${event.data.column}`,
hasError,
startOfFile: event.data.line === 1 && event.data.column === 1,
error: hasError ? event.data.details.error : null,
}
},
add: (event, completion) => {
if (event.startOfFile) return
const existingEventInd = run.tree.findIndex(
d => d.termLink === event.termLink
)
if (existingEventInd > -1) {
if (completion) {
Object.assign(run.tree[existingEventInd], event)
return
}
}
if (event.depth === 0) {
run.tree.push(event)
return
}
let parentNode
for (let i = run.tree.length - 1; i >= 0; i--) {
const node = run.tree[i]
if (node.file === event.file && node.depth === event.depth - 1) {
parentNode = node
break
}
}
if (!parentNode) {
return
}
event.parent = parentNode
;(parentNode.children ||= []).push(event)
run.tree.push(event)
},
end: () => {
if (!run.tree.length) return
let output =
dim('\n> Test ') + bold(`${relative(process.cwd(), run.tree[0].file)}\n`)
for (let node of run.tree) {
if (node.depth !== 0) continue
output += printEvents(node)
}
return output
},
}
module.exports = async function* minimalReporter(source) {
const diagnosticErrors = []
for await (const event of source) {
switch (event.type) {
case 'test:start':
run.add(run.create(event))
break
case 'test:pass':
case 'test:fail':
run.add(run.create(event), true)
break
case 'test:diagnostic':
if (event.data.message.toLowerCase().includes('error'))
diagnosticErrors.push(event.data.message)
break
case 'test:stderr':
yield `${event.data.message}\n`
break
default:
break
}
}
yield run.end()
if (diagnosticErrors.length)
yield `Diagnostic Errors:\n` + diagnosticErrors.join('\n')
}