-
-
Notifications
You must be signed in to change notification settings - Fork 270
/
index.tsx
86 lines (82 loc) · 2.11 KB
/
index.tsx
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
import { LoadedConfig } from '@tapjs/config'
import { TAP, TestBase } from '@tapjs/core'
import { render } from 'ink'
import { isStream, Minipass } from 'minipass'
import React, { FC } from 'react'
import { Writable } from 'stream'
import { WriteStream } from 'tty'
import { Base } from './base.js'
import * as components from './components.js'
import { Dot } from './dot.js'
import * as hooks from './hooks.js'
import { JSONReport, JSONStream } from './json.js'
import { JUnit } from './junit.js'
import { MarkdownStream } from './markdown.js'
import { Min } from './min.js'
import { Terse } from './terse.js'
import * as utils from './utils.js'
export {
Base,
Terse,
Min,
Dot,
JSONReport,
JSONStream,
MarkdownStream,
JUnit,
hooks,
components,
utils,
}
export interface TapReportOpts {
test: TestBase
config: LoadedConfig
}
export type Reporter = FC<TapReportOpts>
export const types: Record<
string,
Reporter | typeof Minipass<string>
> = {}
export const addType = (
name: string,
report: Reporter | typeof Minipass<string>,
) => {
types[name] = report
}
addType('base', Base)
addType('terse', Terse)
addType('min', Min)
addType('dot', Dot)
addType('jsonstream', JSONStream)
addType('json', JSONReport)
addType('junit', JUnit)
addType('markdown', MarkdownStream)
export const report = async (
Type: string | Reporter | typeof Minipass<string>,
tap: TAP,
config: LoadedConfig,
stdout: Writable = process.stdout,
) => {
if (typeof Type === 'string') {
if (types.hasOwnProperty(Type)) {
Type = types[Type] as Reporter | typeof Minipass<string>
} else {
throw new TypeError(`unknown report type: ${Type}`)
}
}
tap.register()
if (isStream(Type.prototype)) {
const reporter = new (Type as typeof Minipass)()
tap.pipe(reporter).pipe(stdout)
} else {
// always show the cursor when we finish
tap.on('complete', () => process.stdout.write('\x1b[?25h'))
const T = Type as Reporter
const opts =
stdout === process.stdout ?
{ patchConsole: false }
: (stdout as WriteStream)
render(<T test={tap} config={config}></T>, opts)
}
return true
}