Skip to content

Commit f396792

Browse files
authored
fix(coverage): infer transform mode for uncovered files (#9435)
1 parent e977f3d commit f396792

File tree

7 files changed

+44
-15
lines changed

7 files changed

+44
-15
lines changed

packages/vitest/src/node/coverage.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,15 +634,16 @@ export class BaseCoverageProvider<Options extends ResolvedCoverageOptions<'istan
634634
root: project.config.root,
635635
isBrowserEnabled: project.isBrowserEnabled(),
636636
vite: project.vite,
637+
environment: project.config.environment,
637638
})),
638639
// Check core last as it will match all files anyway
639-
{ root: ctx.config.root, vite: ctx.vite, isBrowserEnabled: ctx.getRootProject().isBrowserEnabled() },
640+
{ root: ctx.config.root, vite: ctx.vite, isBrowserEnabled: ctx.getRootProject().isBrowserEnabled(), environment: ctx.config.environment },
640641
]
641642

642643
return async function transformFile(filename: string): Promise<TransformResult | null | undefined> {
643644
let lastError
644645

645-
for (const { root, vite, isBrowserEnabled } of servers) {
646+
for (const { root, vite, isBrowserEnabled, environment } of servers) {
646647
// On Windows root doesn't start with "/" while filenames do
647648
if (!filename.startsWith(root) && !filename.startsWith(`/${root}`)) {
648649
continue
@@ -657,14 +658,18 @@ export class BaseCoverageProvider<Options extends ResolvedCoverageOptions<'istan
657658
}
658659

659660
try {
661+
if (environment === 'jsdom' || environment === 'happy-dom') {
662+
return await vite.environments.client.transformRequest(filename)
663+
}
664+
660665
return await vite.environments.ssr.transformRequest(filename)
661666
}
662667
catch (error) {
663668
lastError = error
664669
}
665670
}
666671

667-
// All vite-node servers failed to transform the file
672+
// All vite servers failed to transform the file
668673
throw lastError
669674
}
670675
}

test/coverage-test/fixtures/configs/vitest.config.conditional.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join, resolve } from 'node:path'
1+
import { resolve } from 'node:path'
22
import { defineConfig } from 'vitest/config'
33

44
export default defineConfig({
@@ -9,9 +9,9 @@ export default defineConfig({
99
replacement: "$1",
1010
customResolver(_, __, options) {
1111
if ('ssr' in options && options.ssr) {
12-
return { id: resolve('fixtures/src/conditional/node.ts') }
12+
return { id: resolve('fixtures/src/conditional/ssr.ts') }
1313
}
14-
return { id: resolve('fixtures/src/conditional/browser.ts') }
14+
return { id: resolve('fixtures/src/conditional/web.ts') }
1515
},
1616
},
1717
],

test/coverage-test/fixtures/src/conditional/browser.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/coverage-test/fixtures/src/conditional/node.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function ssr() {
2+
return "This is for ssr transform"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function web() {
2+
return "This is for web transform"
3+
}

test/coverage-test/test/include-exclude.test.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ test('overridden exclude should still apply defaults', async () => {
225225
expect(coverageMap.files()).toMatchInlineSnapshot(`{}`)
226226
})
227227

228-
test('uncovered files are transformed correctly', async () => {
228+
test('uncovered files are transformed correctly (node and browser)', async () => {
229229
await runVitest({
230230
config: 'fixtures/configs/vitest.config.conditional.ts',
231231
include: ['fixtures/test/math.test.ts'],
@@ -242,20 +242,44 @@ test('uncovered files are transformed correctly', async () => {
242242
expect(files).toMatchInlineSnapshot(`
243243
[
244244
"<process-cwd>/fixtures/src/math.ts",
245-
"<process-cwd>/fixtures/src/conditional/browser.ts",
245+
"<process-cwd>/fixtures/src/conditional/web.ts",
246246
]
247247
`)
248248
}
249249
else {
250250
expect(files).toMatchInlineSnapshot(`
251251
[
252252
"<process-cwd>/fixtures/src/math.ts",
253-
"<process-cwd>/fixtures/src/conditional/node.ts",
253+
"<process-cwd>/fixtures/src/conditional/ssr.ts",
254254
]
255255
`)
256256
}
257257
})
258258

259+
test('uncovered files are transformed correctly (jsdom)', async ({ skip }) => {
260+
skip(isBrowser(), 'node relevant test')
261+
262+
await runVitest({
263+
config: 'fixtures/configs/vitest.config.conditional.ts',
264+
include: ['fixtures/test/math.test.ts'],
265+
environment: 'jsdom',
266+
coverage: {
267+
include: ['fixtures/src/math.ts', 'fixtures/src/conditional/*'],
268+
reporter: 'json',
269+
},
270+
})
271+
272+
const coverageMap = await readCoverageMap()
273+
const files = coverageMap.files()
274+
275+
expect(files).toMatchInlineSnapshot(`
276+
[
277+
"<process-cwd>/fixtures/src/math.ts",
278+
"<process-cwd>/fixtures/src/conditional/web.ts",
279+
]
280+
`)
281+
})
282+
259283
test('files included and excluded in plugin\'s configureVitest are excluded', async () => {
260284
await runVitest({
261285
config: 'fixtures/configs/vitest.config.configure-vitest-hook.ts',

0 commit comments

Comments
 (0)