diff --git a/docs/config/disableconsoleintercept.md b/docs/config/disableconsoleintercept.md index 40c04f5f2..540b0fd73 100644 --- a/docs/config/disableconsoleintercept.md +++ b/docs/config/disableconsoleintercept.md @@ -9,12 +9,8 @@ outline: deep - **CLI:** `--disableConsoleIntercept` - **Default:** `false` -By default, Vitest automatically intercepts console logging during tests for extra formatting of test file, test title, etc. +By default, Vitest intercepts console output during tests to add context such as the test file and test title. -This is also required for console log preview on Vitest UI. +In [browser mode](/guide/browser/), this interception is required to forward logs from the browser DevTools to the terminal. It is also required for console log previews in the Vitest UI. -However, disabling such interception might help when you want to debug a code with normal synchronous terminal console logging. - -::: warning -This option has no effect on [browser tests](/guide/browser/) since Vitest preserves original logging in browser devtools. -::: +Disabling console interception can be useful when you want to debug code with normal synchronous terminal logging. diff --git a/packages/browser/src/client/tester/tester.ts b/packages/browser/src/client/tester/tester.ts index 18599b8cc..b75d5395c 100644 --- a/packages/browser/src/client/tester/tester.ts +++ b/packages/browser/src/client/tester/tester.ts @@ -142,7 +142,9 @@ async function prepareTestEnvironment(options: PrepareOptions) { // @ts-expect-error mocking vitest apis globalThis.__vitest_mocker__ = mocker - setupConsoleLogSpy() + if (!config.disableConsoleIntercept) { + setupConsoleLogSpy() + } setupDialogsSpy() const runner = await initiateRunner(state, mocker, config) diff --git a/packages/vitest/src/node/types/config.ts b/packages/vitest/src/node/types/config.ts index 452d86616..53a2e37d9 100644 --- a/packages/vitest/src/node/types/config.ts +++ b/packages/vitest/src/node/types/config.ts @@ -833,11 +833,10 @@ export interface InlineConfig { 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 synchronous terminal console logging. - * - * This option has no effect on browser pool since Vitest preserves original logging on browser devtools. + * By default, Vitest intercepts console output during tests to add context such as the test file and test title. + * In browser mode, this interception is required to forward logs from the browser DevTools to the terminal. + * It is also required for console log previews in the Vitest UI. + * Disabling console interception can be useful when you want to debug code with normal synchronous terminal logging. * * @default false */ diff --git a/test/browser/specs/runner.test.ts b/test/browser/specs/runner.test.ts index c37a79258..b17d96366 100644 --- a/test/browser/specs/runner.test.ts +++ b/test/browser/specs/runner.test.ts @@ -208,6 +208,17 @@ error with a stack }) }) +test('disableConsoleIntercept', async () => { + const result = await runBrowserTests({ + root: './fixtures/print-logs', + project: [instances[0].browser], + disableConsoleIntercept: true, + }) + expect(result.stderr).toBe('') + expect(result.stdout).not.toContain('logging to stdout') + expect(result.stdout).not.toContain('hello from console.log') +}) + test(`stack trace points to correct file in every browser when failed`, async () => { expect.assertions(30) const { stderr } = await runBrowserTests({