/
base.ts
232 lines (195 loc) · 8.48 KB
/
base.ts
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { performance } from 'perf_hooks'
import { relative } from 'pathe'
import c from 'picocolors'
import type { ErrorWithDiff, File, Reporter, Task, TaskResultPack, UserConsoleLog } from '../../types'
import { getFullName, getSuites, getTests, hasFailed, hasFailedSnapshot } from '../../utils'
import type { Vitest } from '../../node'
import { version } from '../../../package.json'
import { F_RIGHT } from '../../utils/figures'
import { divider, getStateString, getStateSymbol, pointer, renderSnapshotSummary } from './renderers/utils'
const BADGE_PADDING = ' '
const HELP_HINT = `${c.dim('press ')}${c.bold('h')}${c.dim(' to show help')}`
const HELP_UPDATE_SNAP = c.dim('press ') + c.bold(c.yellow('u')) + c.dim(' to update snapshot')
const HELP_QUITE = `${c.dim('press ')}${c.bold('q')}${c.dim(' to quit')}`
const WAIT_FOR_CHANGE_PASS = `\n${c.bold(c.inverse(c.green(' PASS ')))}${c.green(' Waiting for file changes...')}`
const WAIT_FOR_CHANGE_FAIL = `\n${c.bold(c.inverse(c.red(' FAIL ')))}${c.red(' Tests failed. Watching for file changes...')}`
export abstract class BaseReporter implements Reporter {
start = 0
end = 0
watchFilters?: string[]
isTTY = process.stdout.isTTY && !process.env.CI
ctx: Vitest = undefined!
constructor() {
this.registerUnhandledRejection()
}
onInit(ctx: Vitest) {
this.ctx = ctx
this.ctx.log()
const versionTest = this.ctx.config.watch
? c.blue(`v${version}`)
: c.cyan(`v${version}`)
const mode = this.ctx.config.watch
? c.blue(' DEV ')
: c.cyan(' RUN ')
this.ctx.log(`${c.inverse(c.bold(mode))} ${versionTest} ${c.gray(this.ctx.config.root)}`)
if (this.ctx.config.ui)
this.ctx.log(c.dim(c.green(` UI started at http://${this.ctx.config.api?.host || 'localhost'}:${c.bold(`${this.ctx.server.config.server.port}`)}`)))
else if (this.ctx.config.api)
this.ctx.log(c.dim(c.green(` API started at http://${this.ctx.config.api?.host || 'localhost'}:${c.bold(`${this.ctx.config.api.port}`)}`)))
this.ctx.log()
this.start = performance.now()
}
relative(path: string) {
return relative(this.ctx.config.root, path)
}
async onFinished(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) {
this.end = performance.now()
await this.reportSummary(files)
if (errors.length) {
process.exitCode = 1
const errorMessage = c.red(c.bold(
`\nVitest catched ${errors.length} unhandled error${errors.length > 1 ? 's' : ''} during the test run. This might cause false positive tests.`
+ '\nPlease, resolve all the errors to make sure your tests are not affected.',
))
this.ctx.log(c.red(divider(c.bold(c.inverse(' Unhandled Errors ')))))
this.ctx.log(errorMessage)
await Promise.all(errors.map(async (err) => {
await this.ctx.printError(err, true, (err as ErrorWithDiff).type || 'Unhandled Error')
}))
this.ctx.log(c.red(divider()))
}
}
onTaskUpdate(packs: TaskResultPack[]) {
if (this.isTTY)
return
for (const pack of packs) {
const task = this.ctx.state.idMap.get(pack[0])
if (task && 'filepath' in task && task.result?.state && task.result?.state !== 'run') {
const tests = getTests(task)
const failed = tests.filter(t => t.result?.state === 'fail')
const skipped = tests.filter(t => t.mode === 'skip' || t.mode === 'todo')
let state = c.dim(`${tests.length} test${tests.length > 1 ? 's' : ''}`)
if (failed.length)
state += ` ${c.dim('|')} ${c.red(`${failed.length} failed`)}`
if (skipped.length)
state += ` ${c.dim('|')} ${c.yellow(`${skipped.length} skipped`)}`
let suffix = c.dim(' (') + state + c.dim(')')
if (task.result.duration)
suffix += c.yellow(` ${Math.round(task.result.duration)}${c.dim('ms')}`)
this.ctx.log(` ${getStateSymbol(task)} ${task.name} ${suffix}`)
// print short errors, full errors will be at the end in summary
for (const test of failed) {
this.ctx.log(c.red(` ${pointer} ${getFullName(test)}`))
this.ctx.log(c.red(` ${F_RIGHT} ${(test.result!.error as any)?.message}`))
}
}
}
}
async onWatcherStart() {
const files = this.ctx.state.getFiles()
const errors = this.ctx.state.getUnhandledErrors()
const failed = errors.length > 0 || hasFailed(files)
const failedSnap = hasFailedSnapshot(files)
if (failed)
this.ctx.log(WAIT_FOR_CHANGE_FAIL)
else
this.ctx.log(WAIT_FOR_CHANGE_PASS)
const hints = [HELP_HINT]
if (failedSnap)
hints.unshift(HELP_UPDATE_SNAP)
else
hints.push(HELP_QUITE)
this.ctx.log(BADGE_PADDING + hints.join(c.dim(', ')))
}
async onWatcherRerun(files: string[], trigger?: string) {
this.watchFilters = files
this.ctx.state.clearErrors()
this.ctx.clearScreen()
this.ctx.log(`\n${c.inverse(c.bold(c.blue(' RERUN ')))}${trigger ? c.dim(` ${this.relative(trigger)}\n`) : ''}`)
this.start = performance.now()
}
onUserConsoleLog(log: UserConsoleLog) {
if (this.ctx.config.silent)
return
const task = log.taskId ? this.ctx.state.idMap.get(log.taskId) : undefined
this.ctx.log(c.gray(log.type + c.dim(` | ${task ? getFullName(task) : 'unknown test'}`)))
process[log.type].write(`${log.content}\n`)
}
onServerRestart() {
this.ctx.log(c.cyan('Restarted due to config changes...'))
}
async reportSummary(files: File[]) {
const suites = getSuites(files)
const tests = getTests(files)
const failedSuites = suites.filter(i => i.result?.error)
const failedTests = tests.filter(i => i.result?.state === 'fail')
const failedTotal = failedSuites.length + failedTests.length
let current = 1
const errorDivider = () => this.ctx.error(`${c.red(c.dim(divider(`[${current++}/${failedTotal}]`, undefined, 1)))}\n`)
if (failedSuites.length) {
this.ctx.error(c.red(divider(c.bold(c.inverse(` Failed Suites ${failedSuites.length} `)))))
this.ctx.error()
await this.printTaskErrors(failedSuites, errorDivider)
}
if (failedTests.length) {
this.ctx.error(c.red(divider(c.bold(c.inverse(` Failed Tests ${failedTests.length} `)))))
this.ctx.error()
await this.printTaskErrors(failedTests, errorDivider)
}
const executionTime = this.end - this.start
const threadTime = files.reduce((acc, test) => acc + Math.max(0, test.result?.duration || 0) + Math.max(0, test.collectDuration || 0), 0)
const padTitle = (str: string) => c.dim(`${str.padStart(10)} `)
const time = (time: number) => {
if (time > 1000)
return `${(time / 1000).toFixed(2)}s`
return `${Math.round(time)}ms`
}
const snapshotOutput = renderSnapshotSummary(this.ctx.config.root, this.ctx.snapshot.summary)
if (snapshotOutput.length) {
this.ctx.log(snapshotOutput.map((t, i) => i === 0
? `${padTitle('Snapshots')} ${t}`
: `${padTitle('')} ${t}`,
).join('\n'))
if (snapshotOutput.length > 1)
this.ctx.log()
}
this.ctx.log(padTitle('Test Files'), getStateString(files))
this.ctx.log(padTitle('Tests'), getStateString(tests))
if (this.watchFilters)
this.ctx.log(padTitle('Time'), time(threadTime))
else
this.ctx.log(padTitle('Time'), time(executionTime) + c.gray(` (in thread ${time(threadTime)}, ${(executionTime / threadTime * 100).toFixed(2)}%)`))
this.ctx.log()
}
private async printTaskErrors(tasks: Task[], errorDivider: () => void) {
const errorsQueue: [error: ErrorWithDiff | undefined, tests: Task[]][] = []
for (const task of tasks) {
// merge identical errors
const error = task.result?.error
const errorItem = error?.stackStr && errorsQueue.find(i => i[0]?.stackStr === error.stackStr)
if (errorItem)
errorItem[1].push(task)
else
errorsQueue.push([error, [task]])
}
for (const [error, tasks] of errorsQueue) {
for (const task of tasks) {
const filepath = (task as File)?.filepath || ''
let name = getFullName(task)
if (filepath)
name = `${name} ${c.dim(`[ ${this.relative(filepath)} ]`)}`
this.ctx.error(`${c.red(c.bold(c.inverse(' FAIL ')))} ${name}`)
}
await this.ctx.printError(error)
errorDivider()
}
}
registerUnhandledRejection() {
process.on('unhandledRejection', async (err) => {
process.exitCode = 1
await this.ctx.printError(err, true, 'Unhandled Rejection')
this.ctx.error('\n\n')
process.exit(1)
})
}
}