Skip to content

Commit 51cd814

Browse files
authored
fix(coverage): browser + v8 in source tests missing (#7946)
1 parent 1b29ff6 commit 51cd814

File tree

6 files changed

+73
-6
lines changed

6 files changed

+73
-6
lines changed

packages/browser/src/node/plugin.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,13 @@ export default (parentServer: ParentBrowserProject, base = '/'): Plugin[] => {
382382
{
383383
name: 'vitest:browser:in-source-tests',
384384
transform(code, id) {
385+
const filename = cleanUrl(id)
385386
const project = parentServer.vitest.getProjectByName(parentServer.config.name)
386-
if (!project._isCachedTestFile(id) || !code.includes('import.meta.vitest')) {
387+
388+
if (!project._isCachedTestFile(filename) || !code.includes('import.meta.vitest')) {
387389
return
388390
}
389-
const s = new MagicString(code, { filename: cleanUrl(id) })
391+
const s = new MagicString(code, { filename })
390392
s.prepend(
391393
`import.meta.vitest = __vitest_index__;\n`,
392394
)

packages/coverage-v8/src/browser.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,5 @@ function filterResult(coverage: ScriptCoverage['result'][number]): boolean {
7171
return false
7272
}
7373

74-
if (coverage.url.includes('?browserv=') || coverage.url.includes('&browserv=')) {
75-
return false
76-
}
77-
7874
return true
7975
}

packages/coverage-v8/src/provider.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ export class V8CoverageProvider extends BaseCoverageProvider<ResolvedCoverageOpt
399399
if (result.url.startsWith('/@fs')) {
400400
result.url = `${FILE_PROTOCOL}${removeStartsWith(result.url, '/@fs')}`
401401
}
402+
else if (result.url.startsWith(project.config.root)) {
403+
result.url = `${FILE_PROTOCOL}${result.url}`
404+
}
402405
else {
403406
result.url = `${FILE_PROTOCOL}${project.config.root}${result.url}`
404407
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference types="vitest/importMeta" />
2+
3+
export function add(a: number, b: number) {
4+
if(a === 5 && b === 7) {
5+
return 12;
6+
}
7+
8+
return a + b
9+
}
10+
11+
if (import.meta.vitest) {
12+
const { test, expect } = import.meta.vitest
13+
14+
test('in source test running add function', () => {
15+
expect(add(10, 19)).toBe(29)
16+
})
17+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { expect } from 'vitest'
2+
import { isV8Provider, readCoverageMap, runVitest, test } from '../utils'
3+
4+
test('in-source tests work', async () => {
5+
const { stdout } = await runVitest({
6+
include: [],
7+
includeSource: ['fixtures/src/in-source.ts'],
8+
coverage: { all: false, reporter: 'json' },
9+
})
10+
11+
expect(stdout).toContain('in source test running add function')
12+
13+
const coverageMap = await readCoverageMap()
14+
const files = coverageMap.files()
15+
16+
expect(files).toMatchInlineSnapshot(`
17+
[
18+
"<process-cwd>/fixtures/src/in-source.ts",
19+
]
20+
`)
21+
22+
const fileCoverage = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/in-source.ts')
23+
24+
// If-branch is not taken - makes sure source maps are correct in in-source testing too
25+
expect(fileCoverage.getUncoveredLines()).toContain('5')
26+
27+
if (isV8Provider()) {
28+
expect(fileCoverage).toMatchInlineSnapshot(`
29+
{
30+
"branches": "2/4 (50%)",
31+
"functions": "1/1 (100%)",
32+
"lines": "10/12 (83.33%)",
33+
"statements": "10/12 (83.33%)",
34+
}
35+
`)
36+
}
37+
else {
38+
expect(fileCoverage).toMatchInlineSnapshot(`
39+
{
40+
"branches": "3/6 (50%)",
41+
"functions": "2/2 (100%)",
42+
"lines": "6/7 (85.71%)",
43+
"statements": "6/7 (85.71%)",
44+
}
45+
`)
46+
}
47+
})

test/coverage-test/vitest.workspace.custom.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export default defineWorkspace([
100100
'**/temporary-files.test.ts',
101101
'**/test-reporter-conflicts.test.ts',
102102
'**/vue.test.ts',
103+
'**/in-source.test.ts',
103104
],
104105
},
105106
},
@@ -126,6 +127,7 @@ export default defineWorkspace([
126127
'**/temporary-files.test.ts',
127128
'**/test-reporter-conflicts.test.ts',
128129
'**/vue.test.ts',
130+
'**/in-source.test.ts',
129131
],
130132
},
131133
},

0 commit comments

Comments
 (0)