Skip to content

Commit

Permalink
fix(vitest): check color support for intercepted console logging (#4966)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Jan 16, 2024
1 parent 6c1cc78 commit 39a7169
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/vitest/src/runtime/console.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -128,7 +128,7 @@ export function createCustomConsole(state: WorkerGlobalState) {
return new Console({
stdout,
stderr,
colorMode: true,
colorMode: getColors().isColorSupported,
groupIndentation: 2,
})
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

5 changes: 5 additions & 0 deletions test/config/fixtures/console-color/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from 'vitest'

test("console color", () => {
console.log(true) // node console highlights primitive
})
3 changes: 3 additions & 0 deletions test/config/fixtures/console-color/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({})
3 changes: 2 additions & 1 deletion test/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*"
}
Expand Down
28 changes: 28 additions & 0 deletions test/config/test/console-color.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})

0 comments on commit 39a7169

Please sign in to comment.