[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

feat(vitest): add `--disable-console-intercept` option to allow opting-out from automatic console log interception (#4786)

authored by

Hiroshi Ogawa and committed by
GitHub
(Jan 12, 2024, 2:19 PM +0100) 43fa6baa 1663f5ca

+42 -1
+1
docs/guide/cli.md
··· 102 102 | `--retry <times>` | Retry the test specific number of times if it fails | 103 103 | `--exclude <glob>` | Additional file globs to be excluded from test | 104 104 | `--expand-snapshot-diff` | Show full diff when snapshot fails | 105 + | `--disable-console-intercept` | Disable automatic interception of console logging (default: `false`) | 105 106 | `--typecheck [options]` | Custom options for typecheck pool. If passed without options, enables typechecking | 106 107 | `--typecheck.enabled` | Enable typechecking alongside tests (default: `false`) | 107 108 | `--typecheck.only` | Run only typecheck tests. This automatically enables typecheck (default: `false`) |
+1
packages/vitest/src/defaults.ts
··· 101 101 exclude: defaultExclude, 102 102 }, 103 103 slowTestThreshold: 300, 104 + disableConsoleIntercept: false, 104 105 } satisfies UserConfig 105 106 106 107 export const configDefaults = Object.freeze(config)
+17
test/config/test/console.test.ts
··· 1 + import { expect, test } from 'vitest' 2 + import { runVitest } from '../../test-utils' 3 + 4 + test('default intercept', async () => { 5 + const { stderr } = await runVitest({ 6 + root: './fixtures/console', 7 + }) 8 + expect(stderr).toBe('stderr | basic.test.ts > basic\n__test_console__\n\n') 9 + }) 10 + 11 + test('disable intercept', async () => { 12 + const { stderr } = await runVitest({ 13 + root: './fixtures/console', 14 + disableConsoleIntercept: true, 15 + }) 16 + expect(stderr).toBe('__test_console__\n') 17 + })
+1
packages/vitest/src/node/cli.ts
··· 59 59 .option('--diff <path>', 'Path to a diff config that will be used to generate diff interface') 60 60 .option('--exclude <glob>', 'Additional file globs to be excluded from test') 61 61 .option('--expand-snapshot-diff', 'Show full diff when snapshot fails') 62 + .option('--disable-console-intercept', 'Disable automatic interception of console logging (default: `false`)') 62 63 .option('--typecheck [options]', 'Custom options for typecheck pool') 63 64 .option('--typecheck.enabled', 'Enable typechecking alongside tests (default: false)') 64 65 .option('--typecheck.only', 'Run only typecheck tests. This automatically enables typecheck (default: false)')
+1
packages/vitest/src/node/core.ts
··· 282 282 'pool', 283 283 'globals', 284 284 'expandSnapshotDiff', 285 + 'disableConsoleIntercept', 285 286 'retry', 286 287 'testNamePattern', 287 288 'passWithNoTests',
+2 -1
packages/vitest/src/runtime/setup-node.ts
··· 55 55 getSourceMap: source => state.moduleCache.getSourceMap(source), 56 56 }) 57 57 58 - await setupConsoleLogSpy(state) 58 + if (!config.disableConsoleIntercept) 59 + await setupConsoleLogSpy(state) 59 60 } 60 61 61 62 export async function setupConsoleLogSpy(state: WorkerGlobalState) {
+11
packages/vitest/src/types/config.ts
··· 668 668 * Show full diff when snapshot fails instead of a patch. 669 669 */ 670 670 expandSnapshotDiff?: boolean 671 + 672 + /** 673 + * By default, Vitest automatically intercepts console logging during tests for extra formatting of test file, test title, etc... 674 + * This is also required for console log preview on Vitest UI. 675 + * However, disabling such interception might help when you want to debug a code with normal synchronus terminal console logging. 676 + * 677 + * This option has no effect on browser pool since Vitest preserves original logging on browser devtools. 678 + * 679 + * @default false 680 + */ 681 + disableConsoleIntercept?: boolean 671 682 } 672 683 673 684 export interface TypecheckConfig {
+5
test/config/fixtures/console/basic.test.ts
··· 1 + import { test } from 'vitest' 2 + 3 + test('basic', () => { 4 + console.error("__test_console__"); 5 + })
+3
test/config/fixtures/console/vitest.config.ts
··· 1 + import { defineConfig } from 'vitest/config' 2 + 3 + export default defineConfig({})