Skip to content

Commit 84f98e7

Browse files
committedDec 21, 2022
feat: project name
1 parent 6277884 commit 84f98e7

File tree

7 files changed

+30
-5
lines changed

7 files changed

+30
-5
lines changed
 

‎packages/vitest/src/node/reporters/base.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { ErrorWithDiff, File, Reporter, Task, TaskResultPack, UserConsoleLo
44
import { clearInterval, getFullName, getSuites, getTests, getTypecheckTests, hasFailed, hasFailedSnapshot, isNode, relativePath, setInterval } from '../../utils'
55
import type { Vitest } from '../../node'
66
import { F_RIGHT } from '../../utils/figures'
7-
import { divider, formatTimeString, getStateString, getStateSymbol, pointer, renderSnapshotSummary } from './renderers/utils'
7+
import { divider, formatProjectName, formatTimeString, getStateString, getStateSymbol, pointer, renderSnapshotSummary } from './renderers/utils'
88

99
const BADGE_PADDING = ' '
1010
const HELP_HINT = `${c.dim('press ')}${c.bold('h')}${c.dim(' to show help')}`
@@ -39,7 +39,6 @@ export abstract class BaseReporter implements Reporter {
3939

4040
onInit(ctx: Vitest) {
4141
this.ctx = ctx
42-
4342
ctx.logger.printBanner()
4443
this.start = performance.now()
4544
}
@@ -321,11 +320,12 @@ export abstract class BaseReporter implements Reporter {
321320
for (const [error, tasks] of errorsQueue) {
322321
for (const task of tasks) {
323322
const filepath = (task as File)?.filepath || ''
323+
const projectName = (task as File)?.projectName || task.file?.projectName
324324
let name = getFullName(task)
325325
if (filepath)
326326
name = `${name} ${c.dim(`[ ${this.relative(filepath)} ]`)}`
327327

328-
this.ctx.logger.error(`${c.red(c.bold(c.inverse(' FAIL ')))} ${name}`)
328+
this.ctx.logger.error(`${c.red(c.bold(c.inverse(' FAIL ')))} ${formatProjectName(projectName)}${name}`)
329329
}
330330
await this.ctx.logger.printError(error)
331331
errorDivider()

‎packages/vitest/src/node/reporters/renderers/listRenderer.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Benchmark, BenchmarkResult, SuiteHooks, Task, VitestRunMode } from
55
import { clearInterval, getTests, getTypecheckTests, isTypecheckTest, notNullish, setInterval } from '../../../utils'
66
import { F_RIGHT } from '../../../utils/figures'
77
import type { Logger } from '../../logger'
8-
import { getCols, getHookStateSymbol, getStateSymbol } from './utils'
8+
import { formatProjectName, getCols, getHookStateSymbol, getStateSymbol } from './utils'
99

1010
export interface ListRendererOptions {
1111
renderSucceed?: boolean
@@ -91,7 +91,10 @@ export function renderTree(tasks: Task[], options: ListRendererOptions, level =
9191

9292
for (const task of tasks) {
9393
let suffix = ''
94-
const prefix = ` ${getStateSymbol(task)} `
94+
let prefix = ` ${getStateSymbol(task)} `
95+
96+
if (level === 0 && task.type === 'suite' && task.projectName)
97+
prefix += formatProjectName(task.projectName)
9598

9699
if (task.type === 'test' && task.result?.retryCount && task.result.retryCount > 1)
97100
suffix += c.yellow(` (retry x${task.result.retryCount})`)

‎packages/vitest/src/node/reporters/renderers/utils.ts

+14
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,17 @@ export function duration(time: number, locale = 'en-us') {
195195
export function formatTimeString(date: Date) {
196196
return date.toTimeString().split(' ')[0]
197197
}
198+
199+
export function formatProjectName(name: string | undefined, suffix = ' ') {
200+
if (!name)
201+
return ''
202+
const index = name.split('').reduce((acc, v, idx) => acc + v.charCodeAt(0) + idx, 0)
203+
const colors = [
204+
c.blue,
205+
c.yellow,
206+
c.cyan,
207+
c.green,
208+
c.magenta,
209+
]
210+
return colors[index % colors.length](`|${name}|`) + suffix
211+
}

‎packages/vitest/src/runtime/collect.ts

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export async function collectTests(paths: string[], config: ResolvedConfig): Pro
4343
mode: 'run',
4444
filepath,
4545
tasks: [],
46+
projectName: config.name,
4647
}
4748

4849
clearCollectorContext()

‎packages/vitest/src/types/config.ts

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ export interface EnvironmentOptions {
3131
export type VitestRunMode = 'test' | 'benchmark' | 'typecheck'
3232

3333
export interface InlineConfig {
34+
/**
35+
* Name of the project. Will be used to display in the reporter.
36+
*/
37+
name?: string
38+
3439
/**
3540
* Benchmark options.
3641
*

‎packages/vitest/src/types/tasks.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface Suite extends TaskBase {
3737
tasks: Task[]
3838
filepath?: string
3939
benchmark?: BenchFactory
40+
projectName?: string
4041
}
4142

4243
export interface File extends Suite {

‎test/core/vitest.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export default defineConfig({
3838
],
3939
},
4040
test: {
41+
name: 'core',
4142
slowTestThreshold: 1000,
4243
testTimeout: 2000,
4344
setupFiles: [

0 commit comments

Comments
 (0)
Please sign in to comment.