fix: respect disableConsoleIntercept in browser mode (#10391) · vitest-dev/vitest@66110d2 (original) (raw)
File tree
- browser/src/client/tester
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -9,12 +9,8 @@ outline: deep | ||
| 9 | 9 | - **CLI:** `--disableConsoleIntercept` |
| 10 | 10 | - **Default:** `false` |
| 11 | 11 | |
| 12 | -By default, Vitest automatically intercepts console logging during tests for extra formatting of test file, test title, etc. | |
| 12 | +By default, Vitest intercepts console output during tests to add context such as the test file and test title. | |
| 13 | 13 | |
| 14 | -This is also required for console log preview on Vitest UI. | |
| 14 | +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. | |
| 15 | 15 | |
| 16 | -However, disabling such interception might help when you want to debug a code with normal synchronous terminal console logging. | |
| 17 | - | |
| 18 | -::: warning | |
| 19 | -This option has no effect on [browser tests](/guide/browser/) since Vitest preserves original logging in browser devtools. | |
| 20 | -::: | |
| 16 | +Disabling console interception can be useful when you want to debug code with normal synchronous terminal logging. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -142,7 +142,9 @@ async function prepareTestEnvironment(options: PrepareOptions) { | ||
| 142 | 142 | // @ts-expect-error mocking vitest apis |
| 143 | 143 | globalThis.__vitest_mocker__ = mocker |
| 144 | 144 | |
| 145 | -setupConsoleLogSpy() | |
| 145 | +if (!config.disableConsoleIntercept) { | |
| 146 | +setupConsoleLogSpy() | |
| 147 | +} | |
| 146 | 148 | setupDialogsSpy() |
| 147 | 149 | |
| 148 | 150 | const runner = await initiateRunner(state, mocker, config) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -833,11 +833,10 @@ export interface InlineConfig { | ||
| 833 | 833 | expandSnapshotDiff?: boolean |
| 834 | 834 | |
| 835 | 835 | /** |
| 836 | - * By default, Vitest automatically intercepts console logging during tests for extra formatting of test file, test title, etc... | |
| 837 | - * This is also required for console log preview on Vitest UI. | |
| 838 | - * However, disabling such interception might help when you want to debug a code with normal synchronous terminal console logging. | |
| 839 | - * | |
| 840 | - * This option has no effect on browser pool since Vitest preserves original logging on browser devtools. | |
| 836 | + * By default, Vitest intercepts console output during tests to add context such as the test file and test title. | |
| 837 | + * In browser mode, this interception is required to forward logs from the browser DevTools to the terminal. | |
| 838 | + * It is also required for console log previews in the Vitest UI. | |
| 839 | + * Disabling console interception can be useful when you want to debug code with normal synchronous terminal logging. | |
| 841 | 840 | * |
| 842 | 841 | * @default false |
| 843 | 842 | */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -208,6 +208,17 @@ error with a stack | ||
| 208 | 208 | }) |
| 209 | 209 | }) |
| 210 | 210 | |
| 211 | +test('disableConsoleIntercept', async () => { | |
| 212 | +const result = await runBrowserTests({ | |
| 213 | +root: './fixtures/print-logs', | |
| 214 | +project: [instances[0].browser], | |
| 215 | +disableConsoleIntercept: true, | |
| 216 | +}) | |
| 217 | +expect(result.stderr).toBe('') | |
| 218 | +expect(result.stdout).not.toContain('logging to stdout') | |
| 219 | +expect(result.stdout).not.toContain('hello from console.log') | |
| 220 | +}) | |
| 221 | + | |
| 211 | 222 | test(`stack trace points to correct file in every browser when failed`, async () => { |
| 212 | 223 | expect.assertions(30) |
| 213 | 224 | const { stderr } = await runBrowserTests({ |