Skip to content

Commit

Permalink
fix(coverage): isolate: false with v8 provider
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Nov 24, 2024
1 parent 021944c commit f563f7e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/browser/src/client/tester/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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__', {
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 15 additions & 3 deletions packages/coverage-v8/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 <CoverageProviderModule>{
startCoverage({ isolate }) {
if (isolate === false && enabled) {
return
}

enabled = true

export default {
startCoverage(): void {
session.connect()
session.post('Profiler.enable')
session.post('Profiler.startPreciseCoverage', {
Expand Down Expand Up @@ -34,7 +42,11 @@ export default {
})
},

stopCoverage(): void {
stopCoverage({ isolate }) {
if (isolate === false) {
return
}

session.post('Profiler.stopPreciseCoverage')
session.post('Profiler.disable')
session.disconnect()
Expand Down
6 changes: 4 additions & 2 deletions packages/vitest/src/integrations/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/types/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface CoverageProviderModule {
/**
* Executed before tests are run in the worker thread.
*/
startCoverage?: () => unknown | Promise<unknown>
startCoverage?: (runtimeOptions: { isolate: boolean }) => unknown | Promise<unknown>

/**
* Executed on after each run in the worker thread. Possible to return a payload passed to the provider
Expand All @@ -76,7 +76,7 @@ export interface CoverageProviderModule {
/**
* Executed after all tests have been run in the worker thread.
*/
stopCoverage?: () => unknown | Promise<unknown>
stopCoverage?: (runtimeOptions: { isolate: boolean }) => unknown | Promise<unknown>
}

export type CoverageReporter = keyof ReportOptions | (string & {})
Expand Down
17 changes: 7 additions & 10 deletions packages/vitest/src/runtime/runBaseTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ export async function run(
): Promise<void> {
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)
Expand All @@ -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)
}
Expand All @@ -76,7 +73,7 @@ export async function run(
vi.restoreAllMocks()
}

await stopCoverageInsideWorker(config.coverage, executor)
await stopCoverageInsideWorker(config.coverage, executor, { isolate })
},
)

Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/runtime/runVmTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit f563f7e

Please sign in to comment.