Skip to content

Commit 8329803

Browse files
authored
fix(coverage): transform untested files with correct project (#11870)
1 parent 1d115dc commit 8329803

8 files changed

Lines changed: 89 additions & 1 deletion

File tree

‎packages/vitest/src/node/coverage.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,9 @@ export class BaseCoverageProvider {
799799
let lastError
800800

801801
for (const project of projects) {
802-
const root = project.config.root
802+
const root = project.config.root.endsWith('/')
803+
? project.config.root
804+
: `${project.config.root}/`
803805

804806
if (
805807
// On Windows root doesn't start with "/" while filenames do
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Plugin, defineConfig } from "vitest/config";
2+
import MagicString from "magic-string";
3+
4+
export default defineConfig({
5+
test: {
6+
projects: [
7+
// Root "app" is a string prefix of "app-utils"
8+
{
9+
test: { name: "app", root: "fixtures/workspaces/prefix-roots/app" },
10+
plugins: [renameFunctionPlugin("transformedByApp")],
11+
},
12+
{
13+
test: { name: "app-utils", root: "fixtures/workspaces/prefix-roots/app-utils" },
14+
plugins: [renameFunctionPlugin("transformedByAppUtils")],
15+
},
16+
],
17+
},
18+
});
19+
20+
function renameFunctionPlugin(name: string): Plugin {
21+
return {
22+
name: "rename-function-plugin",
23+
transform(code, id) {
24+
if (!id.includes("/prefix-roots/") || !code.includes("__project__")) {
25+
return;
26+
}
27+
28+
const s = new MagicString(code);
29+
s.replaceAll("__project__", name);
30+
31+
return { code: s.toString(), map: s.generateMap({ hires: "boundary" }) };
32+
},
33+
};
34+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function __project__() {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expect, test } from 'vitest'
2+
3+
test('basic', () => {
4+
expect(1).toBe(1)
5+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function __project__() {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expect, test } from 'vitest'
2+
3+
test('basic', () => {
4+
expect(1).toBe(1)
5+
})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { expect } from 'vitest'
2+
import { isNativeRunner, readCoverageMap, runVitest, test } from '../utils'
3+
4+
test('uncovered files are transformed by the project whose root contains them', async ({
5+
skip,
6+
}) => {
7+
skip(isNativeRunner(), 'native runner does not support plugins')
8+
9+
await runVitest({
10+
config: './fixtures/configs/vitest.config.prefix-roots.ts',
11+
coverage: {
12+
reporter: 'json',
13+
include: ['**/prefix-roots/**/untested.ts'],
14+
},
15+
})
16+
17+
const coverageMap = await readCoverageMap()
18+
19+
const functionNames = Object.fromEntries(
20+
coverageMap
21+
.files()
22+
.map((file) => [
23+
file,
24+
Object.values(coverageMap.fileCoverageFor(file).fnMap).map((fn) => fn.name),
25+
]),
26+
)
27+
28+
expect(functionNames).toMatchInlineSnapshot(`
29+
{
30+
"<process-cwd>/fixtures/workspaces/prefix-roots/app-utils/src/untested.ts": [
31+
"transformedByAppUtils",
32+
],
33+
"<process-cwd>/fixtures/workspaces/prefix-roots/app/src/untested.ts": [
34+
"transformedByApp",
35+
],
36+
}
37+
`)
38+
})

‎test/coverage-test/vitest.config.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export default defineConfig({
9494
'**/test/mock-autospy.test.ts',
9595
'**/test/mock-importActual.test.ts',
9696
'**/virtual-files.test.ts',
97+
'**/workspace.prefix-roots.test.ts',
9798
],
9899
exclude: [FIXTURES],
99100
},
@@ -129,6 +130,7 @@ export default defineConfig({
129130
'**/test/mock-autospy.test.ts',
130131
'**/test/mock-importActual.test.ts',
131132
'**/virtual-files.test.ts',
133+
'**/workspace.prefix-roots.test.ts',
132134
],
133135
exclude: [FIXTURES],
134136
},

0 commit comments

Comments
 (0)