Skip to content

Commit

Permalink
feat: project name
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Dec 21, 2022
1 parent 6277884 commit 84f98e7
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ErrorWithDiff, File, Reporter, Task, TaskResultPack, UserConsoleLo
import { clearInterval, getFullName, getSuites, getTests, getTypecheckTests, hasFailed, hasFailedSnapshot, isNode, relativePath, setInterval } from '../../utils'
import type { Vitest } from '../../node'
import { F_RIGHT } from '../../utils/figures'
import { divider, formatTimeString, getStateString, getStateSymbol, pointer, renderSnapshotSummary } from './renderers/utils'
import { divider, formatProjectName, formatTimeString, getStateString, getStateSymbol, pointer, renderSnapshotSummary } from './renderers/utils'

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

onInit(ctx: Vitest) {
this.ctx = ctx

ctx.logger.printBanner()
this.start = performance.now()
}
Expand Down Expand Up @@ -321,11 +320,12 @@ export abstract class BaseReporter implements Reporter {
for (const [error, tasks] of errorsQueue) {
for (const task of tasks) {
const filepath = (task as File)?.filepath || ''
const projectName = (task as File)?.projectName || task.file?.projectName
let name = getFullName(task)
if (filepath)
name = `${name} ${c.dim(`[ ${this.relative(filepath)} ]`)}`

this.ctx.logger.error(`${c.red(c.bold(c.inverse(' FAIL ')))} ${name}`)
this.ctx.logger.error(`${c.red(c.bold(c.inverse(' FAIL ')))} ${formatProjectName(projectName)}${name}`)
}
await this.ctx.logger.printError(error)
errorDivider()
Expand Down
7 changes: 5 additions & 2 deletions packages/vitest/src/node/reporters/renderers/listRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Benchmark, BenchmarkResult, SuiteHooks, Task, VitestRunMode } from
import { clearInterval, getTests, getTypecheckTests, isTypecheckTest, notNullish, setInterval } from '../../../utils'
import { F_RIGHT } from '../../../utils/figures'
import type { Logger } from '../../logger'
import { getCols, getHookStateSymbol, getStateSymbol } from './utils'
import { formatProjectName, getCols, getHookStateSymbol, getStateSymbol } from './utils'

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

for (const task of tasks) {
let suffix = ''
const prefix = ` ${getStateSymbol(task)} `
let prefix = ` ${getStateSymbol(task)} `

if (level === 0 && task.type === 'suite' && task.projectName)
prefix += formatProjectName(task.projectName)

if (task.type === 'test' && task.result?.retryCount && task.result.retryCount > 1)
suffix += c.yellow(` (retry x${task.result.retryCount})`)
Expand Down
14 changes: 14 additions & 0 deletions packages/vitest/src/node/reporters/renderers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,17 @@ export function duration(time: number, locale = 'en-us') {
export function formatTimeString(date: Date) {
return date.toTimeString().split(' ')[0]
}

export function formatProjectName(name: string | undefined, suffix = ' ') {
if (!name)
return ''
const index = name.split('').reduce((acc, v, idx) => acc + v.charCodeAt(0) + idx, 0)
const colors = [
c.blue,
c.yellow,
c.cyan,
c.green,
c.magenta,
]
return colors[index % colors.length](`|${name}|`) + suffix
}
1 change: 1 addition & 0 deletions packages/vitest/src/runtime/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export async function collectTests(paths: string[], config: ResolvedConfig): Pro
mode: 'run',
filepath,
tasks: [],
projectName: config.name,
}

clearCollectorContext()
Expand Down
5 changes: 5 additions & 0 deletions packages/vitest/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export interface EnvironmentOptions {
export type VitestRunMode = 'test' | 'benchmark' | 'typecheck'

export interface InlineConfig {
/**
* Name of the project. Will be used to display in the reporter.
*/
name?: string

/**
* Benchmark options.
*
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/types/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface Suite extends TaskBase {
tasks: Task[]
filepath?: string
benchmark?: BenchFactory
projectName?: string
}

export interface File extends Suite {
Expand Down
1 change: 1 addition & 0 deletions test/core/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default defineConfig({
],
},
test: {
name: 'core',
slowTestThreshold: 1000,
testTimeout: 2000,
setupFiles: [
Expand Down

0 comments on commit 84f98e7

Please sign in to comment.