diff --git a/docs/guide/cli.md b/docs/guide/cli.md index ab9cec5a4fc3..71d7b618c239 100644 --- a/docs/guide/cli.md +++ b/docs/guide/cli.md @@ -102,6 +102,7 @@ Run only [benchmark](https://vitest.dev/guide/features.html#benchmarking-experim | `--retry ` | Retry the test specific number of times if it fails | | `--exclude ` | 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`) | diff --git a/packages/vitest/src/defaults.ts b/packages/vitest/src/defaults.ts index 35615a5636f2..44d21c4a350f 100644 --- a/packages/vitest/src/defaults.ts +++ b/packages/vitest/src/defaults.ts @@ -101,6 +101,7 @@ const config = { exclude: defaultExclude, }, slowTestThreshold: 300, + disableConsoleIntercept: false, } satisfies UserConfig export const configDefaults = Object.freeze(config) diff --git a/packages/vitest/src/node/cli.ts b/packages/vitest/src/node/cli.ts index 7504532ea39d..a03dfdba6557 100644 --- a/packages/vitest/src/node/cli.ts +++ b/packages/vitest/src/node/cli.ts @@ -59,6 +59,7 @@ cli .option('--diff ', 'Path to a diff config that will be used to generate diff interface') .option('--exclude ', '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)') diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 8b498b3a61ab..655a6a563006 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -282,6 +282,7 @@ export class Vitest { 'pool', 'globals', 'expandSnapshotDiff', + 'disableConsoleIntercept', 'retry', 'testNamePattern', 'passWithNoTests', diff --git a/packages/vitest/src/runtime/setup-node.ts b/packages/vitest/src/runtime/setup-node.ts index 014329b51c97..1c13e0704578 100644 --- a/packages/vitest/src/runtime/setup-node.ts +++ b/packages/vitest/src/runtime/setup-node.ts @@ -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) { diff --git a/packages/vitest/src/types/config.ts b/packages/vitest/src/types/config.ts index 374e8eb046a3..b6f55da719fe 100644 --- a/packages/vitest/src/types/config.ts +++ b/packages/vitest/src/types/config.ts @@ -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 { diff --git a/test/config/fixtures/console/basic.test.ts b/test/config/fixtures/console/basic.test.ts new file mode 100644 index 000000000000..0e7f5a72024f --- /dev/null +++ b/test/config/fixtures/console/basic.test.ts @@ -0,0 +1,5 @@ +import { test } from 'vitest' + +test('basic', () => { + console.error("__test_console__"); +}) diff --git a/test/config/fixtures/console/vitest.config.ts b/test/config/fixtures/console/vitest.config.ts new file mode 100644 index 000000000000..abed6b2116e1 --- /dev/null +++ b/test/config/fixtures/console/vitest.config.ts @@ -0,0 +1,3 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({}) diff --git a/test/config/test/console.test.ts b/test/config/test/console.test.ts new file mode 100644 index 000000000000..4c7c56b15b26 --- /dev/null +++ b/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') +})