Skip to content

Commit 39a7169

Browse files
authored
fix(vitest): check color support for intercepted console logging (#4966)
1 parent 6c1cc78 commit 39a7169

File tree

6 files changed

+43
-3
lines changed

6 files changed

+43
-3
lines changed

packages/vitest/src/runtime/console.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Writable } from 'node:stream'
22
import { Console } from 'node:console'
33
import { relative } from 'node:path'
4-
import { getSafeTimers } from '@vitest/utils'
4+
import { getColors, getSafeTimers } from '@vitest/utils'
55
import { RealDate } from '../integrations/mock/date'
66
import type { WorkerGlobalState } from '../types'
77

@@ -128,7 +128,7 @@ export function createCustomConsole(state: WorkerGlobalState) {
128128
return new Console({
129129
stdout,
130130
stderr,
131-
colorMode: true,
131+
colorMode: getColors().isColorSupported,
132132
groupIndentation: 2,
133133
})
134134
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test } from 'vitest'
2+
3+
test("console color", () => {
4+
console.log(true) // node console highlights primitive
5+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({})

test/config/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
"type": "module",
44
"private": true,
55
"scripts": {
6-
"test": "vitest run --typecheck.enabled"
6+
"test": "vitest --typecheck.enabled"
77
},
88
"devDependencies": {
9+
"execa": "^8.0.1",
910
"vite": "latest",
1011
"vitest": "workspace:*"
1112
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { expect, test } from 'vitest'
2+
import { execa } from 'execa'
3+
4+
// use "execa" directly since "runVitestCli" strips color
5+
6+
test('with color', async () => {
7+
const proc = await execa('vitest', ['run', '--root=./fixtures/console-color'], {
8+
env: {
9+
CI: '1',
10+
FORCE_COLOR: '1',
11+
NO_COLOR: undefined,
12+
GITHUB_ACTIONS: undefined,
13+
},
14+
})
15+
expect(proc.stdout).toContain('\n\x1B[33mtrue\x1B[39m\n')
16+
})
17+
18+
test('without color', async () => {
19+
const proc = await execa('vitest', ['run', '--root=./fixtures/console-color'], {
20+
env: {
21+
CI: '1',
22+
FORCE_COLOR: undefined,
23+
NO_COLOR: '1',
24+
GITHUB_ACTIONS: undefined,
25+
},
26+
})
27+
expect(proc.stdout).toContain('\ntrue\n')
28+
})

0 commit comments

Comments
 (0)