Skip to content

Commit

Permalink
fix(coverage): isolate: false with browser mode
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Dec 9, 2024
1 parent afb0b35 commit 87c9a56
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
15 changes: 11 additions & 4 deletions packages/coverage-v8/src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import type { CoverageProviderModule } from 'vitest/node'
import type { V8CoverageProvider } from './provider'
import { cdp } from '@vitest/browser/context'
import { loadProvider } from './load-provider'

const session = cdp()
let enabled = false

type ScriptCoverage = Awaited<ReturnType<typeof session.send<'Profiler.takePreciseCoverage'>>>

export default {
export default <CoverageProviderModule>{
async startCoverage() {
if (enabled) {
return
}

enabled = true

await session.send('Profiler.enable')
await session.send('Profiler.startPreciseCoverage', {
callCount: true,
Expand All @@ -32,9 +40,8 @@ export default {
return { result }
},

async stopCoverage() {
await session.send('Profiler.stopPreciseCoverage')
await session.send('Profiler.disable')
stopCoverage() {
// Browser mode should not stop coverage as same V8 instance is shared between tests
},

async getProvider(): Promise<V8CoverageProvider> {
Expand Down
8 changes: 7 additions & 1 deletion test/coverage-test/test/isolation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ for (const isolate of [true, false]) {

coverage: {
all: false,
reporter: 'json',
reporter: ['json', 'html'],
},

// @ts-expect-error -- merged in runVitest
browser: {
isolate,
},
})

Expand All @@ -33,6 +38,7 @@ for (const isolate of [true, false]) {
expect(math.toSummary().branches.pct).toBe(100)
})
}

class Sorter {
sort(files: WorkspaceSpec[]) {
return files.sort((a) => {
Expand Down
1 change: 1 addition & 0 deletions test/coverage-test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export async function runVitest(config: UserConfig, options = { throwOnError: tr
headless: true,
name: 'chromium',
provider: 'playwright',
...config.browser,
},
})

Expand Down
2 changes: 2 additions & 0 deletions test/coverage-test/vitest.workspace.custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export default defineWorkspace([
BROWSER_TESTS,

// Other non-provider-specific tests that should be run on browser mode as well
'**/isolation.test.ts',
'**/include-exclude.test.ts',
'**/allow-external.test.ts',
'**/ignore-hints.test.ts',
Expand All @@ -90,6 +91,7 @@ export default defineWorkspace([
BROWSER_TESTS,

// Other non-provider-specific tests that should be run on browser mode as well
'**/isolation.test.ts',
'**/include-exclude.test.ts',
'**/allow-external.test.ts',
'**/ignore-hints.test.ts',
Expand Down

0 comments on commit 87c9a56

Please sign in to comment.