Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/vitest/src/node/cli/cac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import type { CLIOption, CLIOptions as CLIOptionsConfig } from './cli-config'
import { toArray } from '@vitest/utils/helpers'
import cac from 'cac'
import { normalize } from 'pathe'
import c from 'tinyrainbow'
import c, { disableDefaultColors } from 'tinyrainbow'
import { version } from '../../../package.json' with { type: 'json' }
import { isAgent } from '../../utils/env'
import { benchCliOptionsConfig, cliOptionsConfig, collectCliOptionsConfig } from './cli-config'
import { setupTabCompletions } from './completions'

Expand Down Expand Up @@ -74,6 +75,10 @@ function addCliOptions(cli: CAC | Command, options: CLIOptionsConfig<any>) {
}

export function createCLI(options: CliParseOptions = {}): CAC {
if (isAgent) {
disableDefaultColors()
}

const cli = cac('vitest')

cli.version(version)
Expand Down
3 changes: 1 addition & 2 deletions packages/vitest/src/node/cli/cli-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { ApiConfig } from '../types/config'
import type { CliOptions } from './cli-api'
import { defaultBrowserPort, defaultPort } from '../../constants'
import { ReportersMap } from '../reporters'

type NestedOption<T, V = Extract<T, Record<string, any>>> = V extends
| never
Expand Down Expand Up @@ -141,7 +140,7 @@ export const cliOptionsConfig: VitestCLIOptions = {
},
reporters: {
alias: 'reporter',
description: `Specify reporters (${Object.keys(ReportersMap).join(', ')})`,
description: `Specify reporters (default, agent, blob, verbose, dot, json, tap, tap-flat, junit, tree, hanging-process, github-actions)`,
argument: '<name>',
subcommands: null, // don't support custom objects
array: true,
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/node/config/serializeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { TestProject } from '../project'
import type { ApiConfig, SerializedConfig } from '../types/config'
import { configDefaults } from '../../defaults'
import { isAgent } from '../../utils/env'

export function serializeConfig(project: TestProject): SerializedConfig {
const { config, globalConfig } = project
Expand Down Expand Up @@ -146,5 +147,6 @@ export function serializeConfig(project: TestProject): SerializedConfig {
config.slowTestThreshold
?? globalConfig.slowTestThreshold
?? configDefaults.slowTestThreshold,
isAgent,
}
}
1 change: 1 addition & 0 deletions packages/vitest/src/runtime/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export interface SerializedConfig {
tagsFilter: string[] | undefined
strictTags: boolean
slowTestThreshold: number | undefined
isAgent: boolean
}

export interface SerializedCoverageConfig {
Expand Down
5 changes: 5 additions & 0 deletions packages/vitest/src/runtime/workers/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { WorkerRequest, WorkerResponse } from '../../node/pools/types'
import type { WorkerSetupContext } from '../../types/worker'
import type { VitestWorker } from './types'
import { serializeError } from '@vitest/utils/error'
import { disableDefaultColors } from 'tinyrainbow'
import { Traces } from '../../utils/traces'
import * as listeners from '../listeners'
import { createRuntimeRpc } from '../rpc'
Expand Down Expand Up @@ -49,6 +50,10 @@ export function init(worker: Options): void {
process.env.VITEST_WORKER_ID = String(message.workerId)
reportMemory = message.options.reportMemory

if (message.context.config.isAgent) {
disableDefaultColors()
}

traces ??= await new Traces({
enabled: message.traces.enabled,
sdkPath: message.traces.sdkPath,
Expand Down
32 changes: 16 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ catalog:
tinyexec: ^1.0.2
tinyglobby: ^0.2.15
tinyhighlight: ^0.3.2
tinyrainbow: ^3.0.3
tinyrainbow: ^3.1.0
tinyspy: ^4.0.4
typescript: ^5.9.3
unocss: ^66.6.6
Expand Down
16 changes: 15 additions & 1 deletion test/config/test/console-color.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest'
import { runVitest } from '../../test-utils'
import { runVitest, runVitestCli } from '../../test-utils'

test('with color', async () => {
const { stdout } = await runVitest({
Expand Down Expand Up @@ -30,6 +30,20 @@ test('without color', async () => {
expect(stdout).not.toContain('\x1B[33mtrue\x1B[39m\n')
})

test('agent', async () => {
// Agent check is done on module import, so new process is needed
const { stdout } = await runVitestCli({
preserveAnsi: true,
nodeOptions: { env: { AI_AGENT: 'copilot' } },
}, '--root', 'fixtures/console-color', '--reporter', 'default')

expect.soft(stdout).toContain('true\n')
expect.soft(stdout).not.toContain('\x1B[33mtrue\x1B[39m\n')

expect.soft(stdout).toContain(' RUN')
expect.soft(stdout).not.toContain('\x1B[46m RUN')
})

test.skipIf(process.platform === 'win32')('without color, forks pool in non-TTY parent', async () => {
const { stdout } = await runVitest({
root: 'fixtures/console-color',
Expand Down
Loading