Skip to content

Commit 4af5df3

Browse files
authored
feat: support custom colors for test.name (#7809)
1 parent 78a3d27 commit 4af5df3

File tree

30 files changed

+166
-49
lines changed

30 files changed

+166
-49
lines changed

docs/config/index.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,11 @@ When defined, Vitest will run all matched files with `import.meta.vitest` inside
146146

147147
### name
148148

149-
- **Type:** `string`
149+
- **Type:** `string | { label: string, color?: LabelColor }`
150+
151+
Assign a custom name to the test project or Vitest process. The name will be visible in the CLI and UI, and available in the Node.js API via [`project.name`](/advanced/api/test-project#name).
150152

151-
Assign a custom name to the test project or Vitest process. The name will be visible in the CLI and available in the Node.js API via [`project.name`](/advanced/api/test-project#name).
153+
Color used by CLI and UI can be changed by providing an object with `color` property.
152154

153155
### server {#server}
154156

docs/guide/workspace.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ export default defineConfig({
102102
{
103103
test: {
104104
include: ['tests/**/*.{node}.test.{ts,js}'],
105-
name: 'node',
105+
// color of the name label can be changed
106+
name: { label: 'node', color: 'green' },
106107
environment: 'node',
107108
}
108109
}

packages/ui/client/components/explorer/ExplorerItem.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,13 @@ const projectNameTextColor = computed(() => {
158158
case 'blue':
159159
case 'green':
160160
case 'magenta':
161+
case 'black':
162+
case 'red':
161163
return 'white'
164+
165+
case 'yellow':
166+
case 'cyan':
167+
case 'white':
162168
default:
163169
return 'black'
164170
}

packages/ui/client/composables/client/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ watch(
161161
client.rpc.getFiles(),
162162
client.rpc.getConfig(),
163163
client.rpc.getUnhandledErrors(),
164-
client.rpc.getResolvedProjectNames(),
164+
client.rpc.getResolvedProjectLabels(),
165165
])
166166
if (_config.standalone) {
167167
const filenames = await client.rpc.getTestFiles()

packages/ui/client/composables/client/static.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ export function createStaticClient(): VitestClient {
5151
getResolvedProjectNames: () => {
5252
return metadata.projects
5353
},
54+
getResolvedProjectLabels: () => {
55+
return []
56+
},
5457
getModuleGraph: async (projectName, id) => {
5558
return metadata.moduleGraph[projectName]?.[id]
5659
},

packages/ui/client/composables/explorer/tree.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class ExplorerTree {
1919
private resumeEndRunId: ReturnType<typeof setTimeout> | undefined
2020
constructor(
2121
public projects: string[] = [],
22+
public colors = new Map<string, string | undefined>(),
2223
private onTaskUpdateCalled: boolean = false,
2324
private resumeEndTimeout = 500,
2425
public root = <RootTreeNode>{
@@ -54,8 +55,10 @@ export class ExplorerTree {
5455
this.rafCollector = useRafFn(this.runCollect.bind(this), { fpsLimit: 10, immediate: false })
5556
}
5657

57-
loadFiles(remoteFiles: File[], projects: string[]) {
58-
this.projects.splice(0, this.projects.length, ...projects)
58+
loadFiles(remoteFiles: File[], projects: { name: string; color?: string }[]) {
59+
this.projects.splice(0, this.projects.length, ...projects.map(p => p.name))
60+
this.colors = new Map(projects.map(p => [p.name, p.color]))
61+
5962
runLoadFiles(
6063
remoteFiles,
6164
true,

packages/ui/client/composables/explorer/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export function createOrUpdateFileNode(
7171
duration: file.result?.duration != null ? Math.round(file.result?.duration) : undefined,
7272
filepath: file.filepath,
7373
projectName: file.projectName || '',
74-
projectNameColor: getProjectNameColor(file.projectName),
74+
projectNameColor: explorerTree.colors.get(file.projectName || '') || getProjectNameColor(file.projectName),
7575
collectDuration: file.collectDuration,
7676
setupDuration: file.setupDuration,
7777
environmentLoad: file.environmentLoad,

packages/vitest/src/api/setup.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { WebSocket } from 'ws'
66
import type { Vitest } from '../node/core'
77
import type { Reporter } from '../node/types/reporter'
88
import type { SerializedTestSpecification } from '../runtime/types/utils'
9-
import type { Awaitable, ModuleGraphData, UserConsoleLog } from '../types/general'
9+
import type { Awaitable, LabelColor, ModuleGraphData, UserConsoleLog } from '../types/general'
1010
import type {
1111
TransformResultWithSource,
1212
WebSocketEvents,
@@ -90,6 +90,9 @@ export function setup(ctx: Vitest, _server?: ViteDevServer): void {
9090
getResolvedProjectNames(): string[] {
9191
return ctx.projects.map(p => p.name)
9292
},
93+
getResolvedProjectLabels(): { name: string; color?: LabelColor }[] {
94+
return ctx.projects.map(p => ({ name: p.name, color: p.color }))
95+
},
9396
async getTransformResult(projectName: string, id, browser = false) {
9497
const project = ctx.getProjectByName(projectName)
9598
const result: TransformResultWithSource | null | undefined = browser

packages/vitest/src/api/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { File, TaskEventPack, TaskResultPack } from '@vitest/runner'
22
import type { BirpcReturn } from 'birpc'
33
import type { SerializedConfig } from '../runtime/config'
44
import type { SerializedTestSpecification } from '../runtime/types/utils'
5-
import type { Awaitable, ModuleGraphData, UserConsoleLog } from '../types/general'
5+
import type { Awaitable, LabelColor, ModuleGraphData, UserConsoleLog } from '../types/general'
66

77
interface SourceMap {
88
file: string
@@ -32,7 +32,10 @@ export interface WebSocketHandlers {
3232
getTestFiles: () => Promise<SerializedTestSpecification[]>
3333
getPaths: () => string[]
3434
getConfig: () => SerializedConfig
35+
// TODO: Remove in v4
36+
/** @deprecated -- Use `getResolvedProjectLabels` instead */
3537
getResolvedProjectNames: () => string[]
38+
getResolvedProjectLabels: () => { name: string; color?: LabelColor }[]
3639
getModuleGraph: (
3740
projectName: string,
3841
id: string,

packages/vitest/src/node/config/resolveConfig.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ export function resolveConfig(
145145
resolved.project = toArray(resolved.project)
146146
resolved.provide ??= {}
147147

148+
resolved.name = typeof options.name === 'string'
149+
? options.name
150+
: (options.name?.label || '')
151+
152+
resolved.color = typeof options.name !== 'string' ? options.name?.color : undefined
153+
148154
const inspector = resolved.inspect || resolved.inspectBrk
149155

150156
resolved.inspector = {

0 commit comments

Comments
 (0)