Skip to content

Commit 43fa6ba

Browse files
authoredJan 12, 2024
feat(vitest): add --disable-console-intercept option to allow opting-out from automatic console log interception (#4786)
1 parent 1663f5c commit 43fa6ba

File tree

9 files changed

+42
-1
lines changed

9 files changed

+42
-1
lines changed
 

‎docs/guide/cli.md

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Run only [benchmark](https://vitest.dev/guide/features.html#benchmarking-experim
102102
| `--retry <times>` | Retry the test specific number of times if it fails |
103103
| `--exclude <glob>` | Additional file globs to be excluded from test |
104104
| `--expand-snapshot-diff` | Show full diff when snapshot fails |
105+
| `--disable-console-intercept` | Disable automatic interception of console logging (default: `false`) |
105106
| `--typecheck [options]` | Custom options for typecheck pool. If passed without options, enables typechecking |
106107
| `--typecheck.enabled` | Enable typechecking alongside tests (default: `false`) |
107108
| `--typecheck.only` | Run only typecheck tests. This automatically enables typecheck (default: `false`) |

‎packages/vitest/src/defaults.ts

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ const config = {
101101
exclude: defaultExclude,
102102
},
103103
slowTestThreshold: 300,
104+
disableConsoleIntercept: false,
104105
} satisfies UserConfig
105106

106107
export const configDefaults = Object.freeze(config)

‎packages/vitest/src/node/cli.ts

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ cli
5959
.option('--diff <path>', 'Path to a diff config that will be used to generate diff interface')
6060
.option('--exclude <glob>', 'Additional file globs to be excluded from test')
6161
.option('--expand-snapshot-diff', 'Show full diff when snapshot fails')
62+
.option('--disable-console-intercept', 'Disable automatic interception of console logging (default: `false`)')
6263
.option('--typecheck [options]', 'Custom options for typecheck pool')
6364
.option('--typecheck.enabled', 'Enable typechecking alongside tests (default: false)')
6465
.option('--typecheck.only', 'Run only typecheck tests. This automatically enables typecheck (default: false)')

‎packages/vitest/src/node/core.ts

+1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ export class Vitest {
282282
'pool',
283283
'globals',
284284
'expandSnapshotDiff',
285+
'disableConsoleIntercept',
285286
'retry',
286287
'testNamePattern',
287288
'passWithNoTests',

‎packages/vitest/src/runtime/setup-node.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export async function setupGlobalEnv(config: ResolvedConfig, { environment }: Re
5555
getSourceMap: source => state.moduleCache.getSourceMap(source),
5656
})
5757

58-
await setupConsoleLogSpy(state)
58+
if (!config.disableConsoleIntercept)
59+
await setupConsoleLogSpy(state)
5960
}
6061

6162
export async function setupConsoleLogSpy(state: WorkerGlobalState) {

‎packages/vitest/src/types/config.ts

+11
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,17 @@ export interface InlineConfig {
668668
* Show full diff when snapshot fails instead of a patch.
669669
*/
670670
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
671682
}
672683

673684
export interface TypecheckConfig {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test } from 'vitest'
2+
3+
test('basic', () => {
4+
console.error("__test_console__");
5+
})
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/test/console.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
})

0 commit comments

Comments
 (0)
Please sign in to comment.