From f563f7e70eab6f92a10dee31bb746d2552eeedc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Sun, 24 Nov 2024 10:44:58 +0200 Subject: [PATCH] fix(coverage): `isolate: false` with v8 provider --- packages/browser/src/client/tester/tester.ts | 4 ++-- packages/coverage-v8/src/index.ts | 18 +++++++++++++++--- packages/vitest/src/integrations/coverage.ts | 6 ++++-- packages/vitest/src/node/types/coverage.ts | 4 ++-- packages/vitest/src/runtime/runBaseTests.ts | 17 +++++++---------- packages/vitest/src/runtime/runVmTests.ts | 4 ++-- 6 files changed, 32 insertions(+), 21 deletions(-) diff --git a/packages/browser/src/client/tester/tester.ts b/packages/browser/src/client/tester/tester.ts index aa86e0f31cb9..424e22e274b7 100644 --- a/packages/browser/src/client/tester/tester.ts +++ b/packages/browser/src/client/tester/tester.ts @@ -122,7 +122,7 @@ async function executeTests(method: 'run' | 'collect', files: string[]) { try { await Promise.all([ setupCommonEnv(config), - startCoverageInsideWorker(config.coverage, executor), + startCoverageInsideWorker(config.coverage, executor, { isolate: config.browser.isolate }), (async () => { const VitestIndex = await import('vitest') Object.defineProperty(window, '__vitest_index__', { @@ -160,7 +160,7 @@ async function executeTests(method: 'run' | 'collect', files: string[]) { }, 'Cleanup Error') } state.environmentTeardownRun = true - await stopCoverageInsideWorker(config.coverage, executor).catch((error) => { + await stopCoverageInsideWorker(config.coverage, executor, { isolate: config.browser.isolate }).catch((error) => { client.rpc.onUnhandledError({ name: error.name, message: error.message, diff --git a/packages/coverage-v8/src/index.ts b/packages/coverage-v8/src/index.ts index 4001d627c81a..8fdc43640d08 100644 --- a/packages/coverage-v8/src/index.ts +++ b/packages/coverage-v8/src/index.ts @@ -1,12 +1,20 @@ +import type { CoverageProviderModule } from 'vitest/node' import type { V8CoverageProvider } from './provider' import inspector, { type Profiler } from 'node:inspector' import { provider } from 'std-env' import { loadProvider } from './load-provider' const session = new inspector.Session() +let enabled = false + +export default { + startCoverage({ isolate }) { + if (isolate === false && enabled) { + return + } + + enabled = true -export default { - startCoverage(): void { session.connect() session.post('Profiler.enable') session.post('Profiler.startPreciseCoverage', { @@ -34,7 +42,11 @@ export default { }) }, - stopCoverage(): void { + stopCoverage({ isolate }) { + if (isolate === false) { + return + } + session.post('Profiler.stopPreciseCoverage') session.post('Profiler.disable') session.disconnect() diff --git a/packages/vitest/src/integrations/coverage.ts b/packages/vitest/src/integrations/coverage.ts index db3e8090cff7..a4244e21ecb1 100644 --- a/packages/vitest/src/integrations/coverage.ts +++ b/packages/vitest/src/integrations/coverage.ts @@ -79,11 +79,12 @@ export async function getCoverageProvider( export async function startCoverageInsideWorker( options: SerializedCoverageConfig | undefined, loader: Loader, + runtimeOptions: { isolate: boolean }, ) { const coverageModule = await resolveCoverageProviderModule(options, loader) if (coverageModule) { - return coverageModule.startCoverage?.() + return coverageModule.startCoverage?.(runtimeOptions) } return null @@ -105,11 +106,12 @@ export async function takeCoverageInsideWorker( export async function stopCoverageInsideWorker( options: SerializedCoverageConfig | undefined, loader: Loader, + runtimeOptions: { isolate: boolean }, ) { const coverageModule = await resolveCoverageProviderModule(options, loader) if (coverageModule) { - return coverageModule.stopCoverage?.() + return coverageModule.stopCoverage?.(runtimeOptions) } return null diff --git a/packages/vitest/src/node/types/coverage.ts b/packages/vitest/src/node/types/coverage.ts index 3984c7b250f8..c2f3f408bbeb 100644 --- a/packages/vitest/src/node/types/coverage.ts +++ b/packages/vitest/src/node/types/coverage.ts @@ -66,7 +66,7 @@ export interface CoverageProviderModule { /** * Executed before tests are run in the worker thread. */ - startCoverage?: () => unknown | Promise + startCoverage?: (runtimeOptions: { isolate: boolean }) => unknown | Promise /** * Executed on after each run in the worker thread. Possible to return a payload passed to the provider @@ -76,7 +76,7 @@ export interface CoverageProviderModule { /** * Executed after all tests have been run in the worker thread. */ - stopCoverage?: () => unknown | Promise + stopCoverage?: (runtimeOptions: { isolate: boolean }) => unknown | Promise } export type CoverageReporter = keyof ReportOptions | (string & {}) diff --git a/packages/vitest/src/runtime/runBaseTests.ts b/packages/vitest/src/runtime/runBaseTests.ts index e8dcd4ee1009..70ebaa617dea 100644 --- a/packages/vitest/src/runtime/runBaseTests.ts +++ b/packages/vitest/src/runtime/runBaseTests.ts @@ -24,8 +24,12 @@ export async function run( ): Promise { const workerState = getWorkerState() + const isIsolatedThreads = config.pool === 'threads' && (config.poolOptions?.threads?.isolate ?? true) + const isIsolatedForks = config.pool === 'forks' && (config.poolOptions?.forks?.isolate ?? true) + const isolate = isIsolatedThreads || isIsolatedForks + await setupGlobalEnv(config, environment, executor) - await startCoverageInsideWorker(config.coverage, executor) + await startCoverageInsideWorker(config.coverage, executor, { isolate }) if (config.chaiConfig) { setupChaiConfig(config.chaiConfig) @@ -49,14 +53,7 @@ export async function run( = performance.now() - workerState.durations.environment for (const file of files) { - const isIsolatedThreads - = config.pool === 'threads' - && (config.poolOptions?.threads?.isolate ?? true) - const isIsolatedForks - = config.pool === 'forks' - && (config.poolOptions?.forks?.isolate ?? true) - - if (isIsolatedThreads || isIsolatedForks) { + if (isolate) { executor.mocker.reset() resetModules(workerState.moduleCache, true) } @@ -76,7 +73,7 @@ export async function run( vi.restoreAllMocks() } - await stopCoverageInsideWorker(config.coverage, executor) + await stopCoverageInsideWorker(config.coverage, executor, { isolate }) }, ) diff --git a/packages/vitest/src/runtime/runVmTests.ts b/packages/vitest/src/runtime/runVmTests.ts index f195ca90d716..8091821161ef 100644 --- a/packages/vitest/src/runtime/runVmTests.ts +++ b/packages/vitest/src/runtime/runVmTests.ts @@ -61,7 +61,7 @@ export async function run( getSourceMap: source => workerState.moduleCache.getSourceMap(source), }) - await startCoverageInsideWorker(config.coverage, executor) + await startCoverageInsideWorker(config.coverage, executor, { isolate: false }) if (config.chaiConfig) { setupChaiConfig(config.chaiConfig) @@ -100,7 +100,7 @@ export async function run( vi.restoreAllMocks() } - await stopCoverageInsideWorker(config.coverage, executor) + await stopCoverageInsideWorker(config.coverage, executor, { isolate: false }) } function resolveCss(mod: NodeJS.Module) {