forked from gummesson/tap-min
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
57 lines (48 loc) · 1.4 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
import {PassThrough} from 'node:stream'
import duplexer from 'duplexer3'
import hirestime from 'hirestime'
import {Parser} from 'tap-parser'
import ms from 'pretty-ms'
import chalk from 'chalk'
import util from 'node:util'
const createReporter = () => {
const onResults = (data) => {
const time = timer()
const msg = `${data.count} ${data.count > 1 ? 'tests' : 'test'} complete (${ms(time)})`
result.count = data.count
result.errors = data.failures
if (data.count === 0 || data.fail > 0) {
output.end('\n' + chalk.red(msg) + '\n')
} else {
output.end('\n' + chalk.green(msg) + '\n')
}
}
const input = new Parser(onResults)
const output = new PassThrough()
const result = duplexer(input, output)
input.on('assert', (assert) => {
if (assert.ok) return
output.write('\n' + chalk.white(assert.name) + '\n')
if (assert.diag) {
const d = assert.diag
output.write(
chalk.white(
`\tat: ${util.inspect(d.at)}\n` +
`\toperator: ${d.operator}\n`
) +
chalk.green(
`\texpected: ${util.inspect(d.expected)}\n`
) +
chalk.red(
`\tactual: ${util.inspect(d.actual)}\n\n`
) +
chalk.white(
d.stack + '\n\n'
)
)
}
})
const timer = hirestime() // todo: init when first test running
return result
}
export default createReporter