diff --git a/packages/vitest/src/runtime/console.ts b/packages/vitest/src/runtime/console.ts index d80d6e0ac19f..45f8a7b20718 100644 --- a/packages/vitest/src/runtime/console.ts +++ b/packages/vitest/src/runtime/console.ts @@ -1,7 +1,7 @@ import { Writable } from 'node:stream' import { Console } from 'node:console' import { relative } from 'node:path' -import { getSafeTimers } from '@vitest/utils' +import { getColors, getSafeTimers } from '@vitest/utils' import { RealDate } from '../integrations/mock/date' import type { WorkerGlobalState } from '../types' @@ -128,7 +128,7 @@ export function createCustomConsole(state: WorkerGlobalState) { return new Console({ stdout, stderr, - colorMode: true, + colorMode: getColors().isColorSupported, groupIndentation: 2, }) } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 23bf5a21ad9e..a9c368324222 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1557,6 +1557,9 @@ importers: test/config: devDependencies: + execa: + specifier: ^8.0.1 + version: 8.0.1 vite: specifier: ^5.0.0 version: 5.0.2(@types/node@18.18.9)(less@4.1.3) diff --git a/test/config/fixtures/console-color/basic.test.ts b/test/config/fixtures/console-color/basic.test.ts new file mode 100644 index 000000000000..bef8bc203b0f --- /dev/null +++ b/test/config/fixtures/console-color/basic.test.ts @@ -0,0 +1,5 @@ +import { test } from 'vitest' + +test("console color", () => { + console.log(true) // node console highlights primitive +}) diff --git a/test/config/fixtures/console-color/vitest.config.ts b/test/config/fixtures/console-color/vitest.config.ts new file mode 100644 index 000000000000..abed6b2116e1 --- /dev/null +++ b/test/config/fixtures/console-color/vitest.config.ts @@ -0,0 +1,3 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({}) diff --git a/test/config/package.json b/test/config/package.json index 598c81fc3ec0..4eb3d9bdd328 100644 --- a/test/config/package.json +++ b/test/config/package.json @@ -3,9 +3,10 @@ "type": "module", "private": true, "scripts": { - "test": "vitest run --typecheck.enabled" + "test": "vitest --typecheck.enabled" }, "devDependencies": { + "execa": "^8.0.1", "vite": "latest", "vitest": "workspace:*" } diff --git a/test/config/test/console-color.test.ts b/test/config/test/console-color.test.ts new file mode 100644 index 000000000000..52aba7692e28 --- /dev/null +++ b/test/config/test/console-color.test.ts @@ -0,0 +1,28 @@ +import { expect, test } from 'vitest' +import { execa } from 'execa' + +// use "execa" directly since "runVitestCli" strips color + +test('with color', async () => { + const proc = await execa('vitest', ['run', '--root=./fixtures/console-color'], { + env: { + CI: '1', + FORCE_COLOR: '1', + NO_COLOR: undefined, + GITHUB_ACTIONS: undefined, + }, + }) + expect(proc.stdout).toContain('\n\x1B[33mtrue\x1B[39m\n') +}) + +test('without color', async () => { + const proc = await execa('vitest', ['run', '--root=./fixtures/console-color'], { + env: { + CI: '1', + FORCE_COLOR: undefined, + NO_COLOR: '1', + GITHUB_ACTIONS: undefined, + }, + }) + expect(proc.stdout).toContain('\ntrue\n') +})