From 87c9a56d0f0bd2e964e5ae3faf1ad389089ae74e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Mon, 9 Dec 2024 10:40:12 +0200 Subject: [PATCH] fix(coverage): `isolate: false` with browser mode --- packages/coverage-v8/src/browser.ts | 15 +++++++++++---- test/coverage-test/test/isolation.test.ts | 8 +++++++- test/coverage-test/utils.ts | 1 + test/coverage-test/vitest.workspace.custom.ts | 2 ++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/coverage-v8/src/browser.ts b/packages/coverage-v8/src/browser.ts index 3f3940892129..3ae9f51f463a 100644 --- a/packages/coverage-v8/src/browser.ts +++ b/packages/coverage-v8/src/browser.ts @@ -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>> -export default { +export default { async startCoverage() { + if (enabled) { + return + } + + enabled = true + await session.send('Profiler.enable') await session.send('Profiler.startPreciseCoverage', { callCount: true, @@ -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 { diff --git a/test/coverage-test/test/isolation.test.ts b/test/coverage-test/test/isolation.test.ts index 9d32038fecb3..02414bf6008b 100644 --- a/test/coverage-test/test/isolation.test.ts +++ b/test/coverage-test/test/isolation.test.ts @@ -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, }, }) @@ -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) => { diff --git a/test/coverage-test/utils.ts b/test/coverage-test/utils.ts index 5efe5731bd07..0486120b93fa 100644 --- a/test/coverage-test/utils.ts +++ b/test/coverage-test/utils.ts @@ -51,6 +51,7 @@ export async function runVitest(config: UserConfig, options = { throwOnError: tr headless: true, name: 'chromium', provider: 'playwright', + ...config.browser, }, }) diff --git a/test/coverage-test/vitest.workspace.custom.ts b/test/coverage-test/vitest.workspace.custom.ts index 6974b93ca907..361bce3f3414 100644 --- a/test/coverage-test/vitest.workspace.custom.ts +++ b/test/coverage-test/vitest.workspace.custom.ts @@ -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', @@ -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',