From ab55637653c40c47d9f6ffe694f6c2335dc73260 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 2 Nov 2023 15:58:06 +0100 Subject: [PATCH] fix(browser): disable hijacking ES modules until vi.mock is implemented (#4414) --- docs/config/index.md | 5 ++--- packages/vitest/src/node/config.ts | 2 +- packages/vitest/src/runtime/runners/benchmark.ts | 1 - test/browser/vitest.config.mts | 1 + test/coverage-test/test/coverage.test.ts | 5 +++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index 273a519b3017..321ab6dcf0f9 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -1424,15 +1424,14 @@ To have a better type safety when using built-in providers, you can add one of t #### browser.slowHijackESM - **Type:** `boolean` -- **Default:** `true` +- **Default:** `false` - **Version:** Since Vitest 0.31.0 When running tests in Node.js Vitest can use its own module resolution to easily mock modules with `vi.mock` syntax. However it's not so easy to replicate ES module resolution in browser, so we need to transform your source files before browser can consume it. This option has no effect on tests running inside Node.js. -This options is enabled by default when running in the browser. If you don't rely on spying on ES modules with `vi.spyOn` and don't use `vi.mock`, you can disable this to get a slight boost to performance. - +If you rely on spying on ES modules with `vi.spyOn`, you can enable this experimental feature to allow spying on module exports. ### clearMocks diff --git a/packages/vitest/src/node/config.ts b/packages/vitest/src/node/config.ts index 8a5c4a47083a..e60f81204fde 100644 --- a/packages/vitest/src/node/config.ts +++ b/packages/vitest/src/node/config.ts @@ -377,7 +377,7 @@ export function resolveConfig( resolved.browser ??= {} as any resolved.browser.enabled ??= false resolved.browser.headless ??= isCI - resolved.browser.slowHijackESM ??= true + resolved.browser.slowHijackESM ??= false resolved.browser.isolate ??= true resolved.browser.api = resolveApiServerConfig(resolved.browser) || { diff --git a/packages/vitest/src/runtime/runners/benchmark.ts b/packages/vitest/src/runtime/runners/benchmark.ts index 8ad538863fe3..87838b007153 100644 --- a/packages/vitest/src/runtime/runners/benchmark.ts +++ b/packages/vitest/src/runtime/runners/benchmark.ts @@ -1,4 +1,3 @@ -import { performance } from 'node:perf_hooks' import type { Suite, Task, VitestRunner, VitestRunnerImportSource } from '@vitest/runner' import { updateTask as updateRunnerTask } from '@vitest/runner' import { createDefer, getSafeTimers } from '@vitest/utils' diff --git a/test/browser/vitest.config.mts b/test/browser/vitest.config.mts index 7cbdb14d5a80..784ae11c0bdc 100644 --- a/test/browser/vitest.config.mts +++ b/test/browser/vitest.config.mts @@ -15,6 +15,7 @@ export default defineConfig({ headless: false, provider: process.env.PROVIDER || 'webdriverio', isolate: false, + slowHijackESM: true, }, alias: { '#src': resolve(dir, './src'), diff --git a/test/coverage-test/test/coverage.test.ts b/test/coverage-test/test/coverage.test.ts index 254d4f47902d..42ae5fe6ea51 100644 --- a/test/coverage-test/test/coverage.test.ts +++ b/test/coverage-test/test/coverage.test.ts @@ -6,7 +6,6 @@ import virtualFile1 from 'virtual:vitest-custom-virtual-file-1' import { implicitElse } from '../src/implicitElse' import { useImportEnv } from '../src/importEnv' import { second } from '../src/function-count' -import { runDynamicFileCJS, runDynamicFileESM } from '../src/dynamic-files' import MultiSuite from '../src/multi-suite' // @ts-expect-error -- untyped virtual file provided by custom plugin @@ -14,7 +13,7 @@ import virtualFile2 from '\0vitest-custom-virtual-file-2' // Browser mode crashes with dynamic files. Enable this when browser mode works. // To keep istanbul report consistent between browser and node, skip dynamic tests when istanbul is used. -const skipDynamicFiles = globalThis.process?.env.COVERAGE_PROVIDER === 'istanbul' || !globalThis.process?.env.COVERAGE_PROVIDER +const skipDynamicFiles = '__vitest_browser__' in globalThis || globalThis.process?.env.COVERAGE_PROVIDER === 'istanbul' || !globalThis.process?.env.COVERAGE_PROVIDER const { pythagoras } = await (() => { if ('__vitest_browser__' in globalThis) @@ -59,10 +58,12 @@ describe('Multiple test suites', () => { }) test.skipIf(skipDynamicFiles)('run dynamic ESM file', async () => { + const { runDynamicFileESM } = await import('../src/dynamic-files') await runDynamicFileESM() }) test.skipIf(skipDynamicFiles)('run dynamic CJS file', async () => { + const { runDynamicFileCJS } = await import('../src/dynamic-files') await runDynamicFileCJS() })