Skip to content

Commit 00ebea6

Browse files
authored
refactor(reporters): base reporter readability improvements (#6889)
1 parent 9b3c3de commit 00ebea6

File tree

6 files changed

+246
-336
lines changed

6 files changed

+246
-336
lines changed

packages/vitest/src/node/logger.ts

+27-50
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { createLogUpdate } from 'log-update'
1212
import c from 'tinyrainbow'
1313
import { highlightCode } from '../utils/colors'
1414
import { printError } from './error'
15-
import { divider } from './reporters/renderers/utils'
15+
import { divider, withLabel } from './reporters/renderers/utils'
1616
import { RandomSequencer } from './sequencers/RandomSequencer'
1717

1818
export interface ErrorOptions {
@@ -25,6 +25,8 @@ export interface ErrorOptions {
2525
showCodeFrame?: boolean
2626
}
2727

28+
const PAD = ' '
29+
2830
const ESC = '\x1B['
2931
const ERASE_DOWN = `${ESC}J`
3032
const ERASE_SCROLLBACK = `${ESC}3J`
@@ -64,13 +66,18 @@ export class Logger {
6466
this.console.warn(...args)
6567
}
6668

67-
clearFullScreen(message: string) {
69+
clearFullScreen(message = '') {
6870
if (!this.ctx.config.clearScreen) {
6971
this.console.log(message)
7072
return
7173
}
7274

73-
this.console.log(`${CLEAR_SCREEN}${ERASE_SCROLLBACK}${message}`)
75+
if (message) {
76+
this.console.log(`${CLEAR_SCREEN}${ERASE_SCROLLBACK}${message}`)
77+
}
78+
else {
79+
(this.outputStream as Writable).write(`${CLEAR_SCREEN}${ERASE_SCROLLBACK}`)
80+
}
7481
}
7582

7683
clearScreen(message: string, force = false) {
@@ -201,23 +208,13 @@ export class Logger {
201208
printBanner() {
202209
this.log()
203210

204-
const versionTest = this.ctx.config.watch
205-
? c.blue(`v${this.ctx.version}`)
206-
: c.cyan(`v${this.ctx.version}`)
207-
const mode = this.ctx.config.watch ? c.blue(' DEV ') : c.cyan(' RUN ')
211+
const color = this.ctx.config.watch ? 'blue' : 'cyan'
212+
const mode = this.ctx.config.watch ? 'DEV' : 'RUN'
208213

209-
this.log(
210-
`${c.inverse(c.bold(mode))} ${versionTest} ${c.gray(
211-
this.ctx.config.root,
212-
)}`,
213-
)
214+
this.log(withLabel(color, mode, `v${this.ctx.version} `) + c.gray(this.ctx.config.root))
214215

215216
if (this.ctx.config.sequence.sequencer === RandomSequencer) {
216-
this.log(
217-
c.gray(
218-
` Running tests with seed "${this.ctx.config.sequence.seed}"`,
219-
),
220-
)
217+
this.log(PAD + c.gray(`Running tests with seed "${this.ctx.config.sequence.seed}"`))
221218
}
222219

223220
this.ctx.projects.forEach((project) => {
@@ -231,52 +228,32 @@ export class Logger {
231228
const origin = resolvedUrls?.local[0] ?? resolvedUrls?.network[0]
232229
const provider = project.browser.provider.name
233230
const providerString = provider === 'preview' ? '' : ` by ${provider}`
234-
this.log(
235-
c.dim(
236-
c.green(
237-
` ${output} Browser runner started${providerString} at ${new URL('/', origin)}`,
238-
),
239-
),
240-
)
231+
232+
this.log(PAD + c.dim(c.green(`${output} Browser runner started${providerString} at ${new URL('/', origin)}`)))
241233
})
242234

243235
if (this.ctx.config.ui) {
244-
this.log(
245-
c.dim(
246-
c.green(
247-
` UI started at http://${
248-
this.ctx.config.api?.host || 'localhost'
249-
}:${c.bold(`${this.ctx.server.config.server.port}`)}${
250-
this.ctx.config.uiBase
251-
}`,
252-
),
253-
),
254-
)
236+
const host = this.ctx.config.api?.host || 'localhost'
237+
const port = this.ctx.server.config.server.port
238+
const base = this.ctx.config.uiBase
239+
240+
this.log(PAD + c.dim(c.green(`UI started at http://${host}:${c.bold(port)}${base}`)))
255241
}
256242
else if (this.ctx.config.api?.port) {
257243
const resolvedUrls = this.ctx.server.resolvedUrls
258244
// workaround for https://github.com/vitejs/vite/issues/15438, it was fixed in vite 5.1
259-
const fallbackUrl = `http://${this.ctx.config.api.host || 'localhost'}:${
260-
this.ctx.config.api.port
261-
}`
262-
const origin
263-
= resolvedUrls?.local[0] ?? resolvedUrls?.network[0] ?? fallbackUrl
264-
this.log(c.dim(c.green(` API started at ${new URL('/', origin)}`)))
245+
const fallbackUrl = `http://${this.ctx.config.api.host || 'localhost'}:${this.ctx.config.api.port}`
246+
const origin = resolvedUrls?.local[0] ?? resolvedUrls?.network[0] ?? fallbackUrl
247+
248+
this.log(PAD + c.dim(c.green(`API started at ${new URL('/', origin)}`)))
265249
}
266250

267251
if (this.ctx.coverageProvider) {
268-
this.log(
269-
c.dim(' Coverage enabled with ')
270-
+ c.yellow(this.ctx.coverageProvider.name),
271-
)
252+
this.log(PAD + c.dim('Coverage enabled with ') + c.yellow(this.ctx.coverageProvider.name))
272253
}
273254

274255
if (this.ctx.config.standalone) {
275-
this.log(
276-
c.yellow(
277-
`\nVitest is running in standalone mode. Edit a test file to rerun tests.`,
278-
),
279-
)
256+
this.log(c.yellow(`\nVitest is running in standalone mode. Edit a test file to rerun tests.`))
280257
}
281258
else {
282259
this.log()

0 commit comments

Comments
 (0)