Skip to content

Commit

Permalink
feat(vitest): add --disable-console-intercept option to allow optin…
Browse files Browse the repository at this point in the history
…g-out from automatic console log interception (#4786)
  • Loading branch information
hi-ogawa committed Jan 12, 2024
1 parent 1663f5c commit 43fa6ba
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/guide/cli.md
Expand Up @@ -102,6 +102,7 @@ Run only [benchmark](https://vitest.dev/guide/features.html#benchmarking-experim
| `--retry <times>` | Retry the test specific number of times if it fails |
| `--exclude <glob>` | Additional file globs to be excluded from test |
| `--expand-snapshot-diff` | Show full diff when snapshot fails |
| `--disable-console-intercept` | Disable automatic interception of console logging (default: `false`) |
| `--typecheck [options]` | Custom options for typecheck pool. If passed without options, enables typechecking |
| `--typecheck.enabled` | Enable typechecking alongside tests (default: `false`) |
| `--typecheck.only` | Run only typecheck tests. This automatically enables typecheck (default: `false`) |
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/defaults.ts
Expand Up @@ -101,6 +101,7 @@ const config = {
exclude: defaultExclude,
},
slowTestThreshold: 300,
disableConsoleIntercept: false,
} satisfies UserConfig

export const configDefaults = Object.freeze(config)
1 change: 1 addition & 0 deletions packages/vitest/src/node/cli.ts
Expand Up @@ -59,6 +59,7 @@ cli
.option('--diff <path>', 'Path to a diff config that will be used to generate diff interface')
.option('--exclude <glob>', 'Additional file globs to be excluded from test')
.option('--expand-snapshot-diff', 'Show full diff when snapshot fails')
.option('--disable-console-intercept', 'Disable automatic interception of console logging (default: `false`)')
.option('--typecheck [options]', 'Custom options for typecheck pool')
.option('--typecheck.enabled', 'Enable typechecking alongside tests (default: false)')
.option('--typecheck.only', 'Run only typecheck tests. This automatically enables typecheck (default: false)')
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/core.ts
Expand Up @@ -282,6 +282,7 @@ export class Vitest {
'pool',
'globals',
'expandSnapshotDiff',
'disableConsoleIntercept',
'retry',
'testNamePattern',
'passWithNoTests',
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/runtime/setup-node.ts
Expand Up @@ -55,7 +55,8 @@ export async function setupGlobalEnv(config: ResolvedConfig, { environment }: Re
getSourceMap: source => state.moduleCache.getSourceMap(source),
})

await setupConsoleLogSpy(state)
if (!config.disableConsoleIntercept)
await setupConsoleLogSpy(state)
}

export async function setupConsoleLogSpy(state: WorkerGlobalState) {
Expand Down
11 changes: 11 additions & 0 deletions packages/vitest/src/types/config.ts
Expand Up @@ -668,6 +668,17 @@ export interface InlineConfig {
* Show full diff when snapshot fails instead of a patch.
*/
expandSnapshotDiff?: boolean

/**
* By default, Vitest automatically intercepts console logging during tests for extra formatting of test file, test title, etc...
* This is also required for console log preview on Vitest UI.
* However, disabling such interception might help when you want to debug a code with normal synchronus terminal console logging.
*
* This option has no effect on browser pool since Vitest preserves original logging on browser devtools.
*
* @default false
*/
disableConsoleIntercept?: boolean
}

export interface TypecheckConfig {
Expand Down
5 changes: 5 additions & 0 deletions test/config/fixtures/console/basic.test.ts
@@ -0,0 +1,5 @@
import { test } from 'vitest'

test('basic', () => {
console.error("__test_console__");
})
3 changes: 3 additions & 0 deletions test/config/fixtures/console/vitest.config.ts
@@ -0,0 +1,3 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({})
17 changes: 17 additions & 0 deletions test/config/test/console.test.ts
@@ -0,0 +1,17 @@
import { expect, test } from 'vitest'
import { runVitest } from '../../test-utils'

test('default intercept', async () => {
const { stderr } = await runVitest({
root: './fixtures/console',
})
expect(stderr).toBe('stderr | basic.test.ts > basic\n__test_console__\n\n')
})

test('disable intercept', async () => {
const { stderr } = await runVitest({
root: './fixtures/console',
disableConsoleIntercept: true,
})
expect(stderr).toBe('__test_console__\n')
})

0 comments on commit 43fa6ba

Please sign in to comment.