Skip to content

Commit c5e4c24

Browse files
committed
fix(browser): source maps of in-source tests
1 parent 9defb9c commit c5e4c24

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
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
)

test/coverage-test/test/in-source.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from 'vitest'
2-
import { readCoverageMap, runVitest, test } from '../utils'
2+
import { isV8Provider, readCoverageMap, runVitest, test } from '../utils'
33

44
test('in-source tests work', async () => {
55
const { stdout } = await runVitest({
@@ -18,4 +18,30 @@ test('in-source tests work', async () => {
1818
"<process-cwd>/fixtures/src/in-source.ts",
1919
]
2020
`)
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+
}
2147
})

0 commit comments

Comments
 (0)