From e04a13689a56819f6b2406ab103de828d783c1d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Mon, 18 Nov 2024 16:07:31 +0200 Subject: [PATCH] fix(coverage): exclude browser mode iframe results (#6905) --- packages/coverage-v8/src/browser.ts | 8 ++++++++ test/coverage-test/test/bundled-sources.test.ts | 2 +- test/coverage-test/test/include-exclude.test.ts | 10 +++------- test/coverage-test/test/pre-transpiled-source.test.ts | 2 +- test/coverage-test/test/results-snapshot.test.ts | 2 +- test/coverage-test/vitest.workspace.custom.ts | 2 ++ 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/packages/coverage-v8/src/browser.ts b/packages/coverage-v8/src/browser.ts index d4b6f68554d0..3f3940892129 100644 --- a/packages/coverage-v8/src/browser.ts +++ b/packages/coverage-v8/src/browser.ts @@ -59,5 +59,13 @@ function filterResult(coverage: ScriptCoverage['result'][number]): boolean { return false } + if (coverage.url === window.location.href) { + return false + } + + if (coverage.url.includes('?browserv=') || coverage.url.includes('&browserv=')) { + return false + } + return true } diff --git a/test/coverage-test/test/bundled-sources.test.ts b/test/coverage-test/test/bundled-sources.test.ts index ba0b4a24d5bd..cd025c2e39e1 100644 --- a/test/coverage-test/test/bundled-sources.test.ts +++ b/test/coverage-test/test/bundled-sources.test.ts @@ -24,7 +24,7 @@ test('bundled code with source maps to originals', async () => { expect(files.find(file => file.includes('bundle.ts'))).toBeFalsy() expect(files.find(file => file.includes('bundle.d.ts'))).toBeFalsy() - expect(JSON.stringify(coverageJson, null, 2)).toMatchFileSnapshot(`__snapshots__/bundled-${isV8Provider() ? 'v8' : 'istanbul'}.snapshot.json`) + await expect(JSON.stringify(coverageJson, null, 2)).toMatchFileSnapshot(`__snapshots__/bundled-${isV8Provider() ? 'v8' : 'istanbul'}.snapshot.json`) }) coverageTest('run bundled sources', () => { diff --git a/test/coverage-test/test/include-exclude.test.ts b/test/coverage-test/test/include-exclude.test.ts index fa465184a6b3..9095c1367208 100644 --- a/test/coverage-test/test/include-exclude.test.ts +++ b/test/coverage-test/test/include-exclude.test.ts @@ -1,10 +1,10 @@ import { expect } from 'vitest' import { coverageConfigDefaults } from 'vitest/config' -import { coverageTest, normalizeURL, readCoverageMap, runVitest, test } from '../utils' +import { readCoverageMap, runVitest, test } from '../utils' test('default exclude should ignore test files', async () => { await runVitest({ - include: [normalizeURL(import.meta.url)], + include: ['fixtures/test/math.test.ts'], coverage: { all: true, reporter: 'json', @@ -18,7 +18,7 @@ test('default exclude should ignore test files', async () => { test('overriden exclude should not apply defaults', async () => { await runVitest({ - include: [normalizeURL(import.meta.url)], + include: ['fixtures/test/math.test.ts'], coverage: { all: true, reporter: 'json', @@ -63,7 +63,3 @@ test('test files are not automatically excluded from report when excludes is set const files = coverageMap.files() expect(files).toContain('/fixtures/src/test-that-looks-like-source-file.ts') }) - -coverageTest('dummy', () => { - expect(1 + 1).toBe(2) -}) diff --git a/test/coverage-test/test/pre-transpiled-source.test.ts b/test/coverage-test/test/pre-transpiled-source.test.ts index 35ab9d7dc5d1..57c91c493f8c 100644 --- a/test/coverage-test/test/pre-transpiled-source.test.ts +++ b/test/coverage-test/test/pre-transpiled-source.test.ts @@ -22,5 +22,5 @@ test('pre-transpiled code with source maps to original (#5341)', async () => { expect(files.find(file => file.includes('transpiled.ts'))).toBeFalsy() expect(files.find(file => file.includes('transpiled.d.ts'))).toBeFalsy() - expect(JSON.stringify(coverageJson, null, 2)).toMatchFileSnapshot(`__snapshots__/pre-transpiled-${isV8Provider() ? 'v8' : 'istanbul'}.snapshot.json`) + await expect(JSON.stringify(coverageJson, null, 2)).toMatchFileSnapshot(`__snapshots__/pre-transpiled-${isV8Provider() ? 'v8' : 'istanbul'}.snapshot.json`) }) diff --git a/test/coverage-test/test/results-snapshot.test.ts b/test/coverage-test/test/results-snapshot.test.ts index 6f005163ff3c..e1078df0a908 100644 --- a/test/coverage-test/test/results-snapshot.test.ts +++ b/test/coverage-test/test/results-snapshot.test.ts @@ -16,5 +16,5 @@ test('coverage results matches snapshot', async () => { const coverageJson = await readCoverageJson() - expect(JSON.stringify(coverageJson, null, 2)).toMatchFileSnapshot(`__snapshots__/results-${isV8Provider() ? 'v8' : 'istanbul'}.snapshot.json`) + await expect(JSON.stringify(coverageJson, null, 2)).toMatchFileSnapshot(`__snapshots__/results-${isV8Provider() ? 'v8' : 'istanbul'}.snapshot.json`) }) diff --git a/test/coverage-test/vitest.workspace.custom.ts b/test/coverage-test/vitest.workspace.custom.ts index 5d40bec498e8..6974b93ca907 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 + '**/include-exclude.test.ts', '**/allow-external.test.ts', '**/ignore-hints.test.ts', '**/import-attributes.test.ts', @@ -89,6 +90,7 @@ export default defineWorkspace([ BROWSER_TESTS, // Other non-provider-specific tests that should be run on browser mode as well + '**/include-exclude.test.ts', '**/allow-external.test.ts', '**/ignore-hints.test.ts', '**/import-attributes.test.ts',