Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
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,
}
}
6 changes: 6 additions & 0 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import { SnapshotManager } from '@vitest/snapshot/manager'
import { deepClone, deepMerge, nanoid, toArray } from '@vitest/utils/helpers'
import { serializeValue } from '@vitest/utils/serialize'
import { join, normalize, relative } from 'pathe'
import { disableDefaultColors } from 'tinyrainbow'
import { isRunnableDevEnvironment } from 'vite'
import { version } from '../../package.json' with { type: 'json' }
import { distDir } from '../paths'
import { wildcardPatternToRegExp } from '../utils/base'
import { isAgent } from '../utils/env'
import { NativeModuleRunner } from '../utils/nativeModuleRunner'
import { convertTasksToEvents } from '../utils/tasks'
import { Traces } from '../utils/traces'
Expand Down Expand Up @@ -137,6 +139,10 @@ export class Vitest {
cliOptions: UserConfig,
options: VitestOptions = {},
) {
if (isAgent) {
disableDefaultColors()
}

this._cliOptions = cliOptions
this.logger = new Logger(this, options.stdout, options.stderr)
this.packageInstaller = options.packageInstaller || new VitestPackageInstaller()
Expand Down
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 @@ -83,7 +83,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