Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/vitest/src/node/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,15 +634,16 @@ export class BaseCoverageProvider<Options extends ResolvedCoverageOptions<'istan
root: project.config.root,
isBrowserEnabled: project.isBrowserEnabled(),
vite: project.vite,
environment: project.config.environment,
})),
// Check core last as it will match all files anyway
{ root: ctx.config.root, vite: ctx.vite, isBrowserEnabled: ctx.getRootProject().isBrowserEnabled() },
{ root: ctx.config.root, vite: ctx.vite, isBrowserEnabled: ctx.getRootProject().isBrowserEnabled(), environment: ctx.config.environment },
]

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

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

try {
if (environment === 'jsdom' || environment === 'happy-dom') {
return await vite.environments.client.transformRequest(filename)
}

return await vite.environments.ssr.transformRequest(filename)
}
catch (error) {
lastError = error
}
}

// All vite-node servers failed to transform the file
// All vite servers failed to transform the file
throw lastError
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join, resolve } from 'node:path'
import { resolve } from 'node:path'
import { defineConfig } from 'vitest/config'

export default defineConfig({
Expand All @@ -9,9 +9,9 @@ export default defineConfig({
replacement: "$1",
customResolver(_, __, options) {
if ('ssr' in options && options.ssr) {
return { id: resolve('fixtures/src/conditional/node.ts') }
return { id: resolve('fixtures/src/conditional/ssr.ts') }
}
return { id: resolve('fixtures/src/conditional/browser.ts') }
return { id: resolve('fixtures/src/conditional/web.ts') }
},
},
],
Expand Down
3 changes: 0 additions & 3 deletions test/coverage-test/fixtures/src/conditional/browser.ts

This file was deleted.

3 changes: 0 additions & 3 deletions test/coverage-test/fixtures/src/conditional/node.ts

This file was deleted.

3 changes: 3 additions & 0 deletions test/coverage-test/fixtures/src/conditional/ssr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function ssr() {
return "This is for ssr transform"
}
3 changes: 3 additions & 0 deletions test/coverage-test/fixtures/src/conditional/web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function web() {
return "This is for web transform"
}
30 changes: 27 additions & 3 deletions test/coverage-test/test/include-exclude.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ test('overridden exclude should still apply defaults', async () => {
expect(coverageMap.files()).toMatchInlineSnapshot(`{}`)
})

test('uncovered files are transformed correctly', async () => {
test('uncovered files are transformed correctly (node and browser)', async () => {
await runVitest({
config: 'fixtures/configs/vitest.config.conditional.ts',
include: ['fixtures/test/math.test.ts'],
Expand All @@ -242,20 +242,44 @@ test('uncovered files are transformed correctly', async () => {
expect(files).toMatchInlineSnapshot(`
[
"<process-cwd>/fixtures/src/math.ts",
"<process-cwd>/fixtures/src/conditional/browser.ts",
"<process-cwd>/fixtures/src/conditional/web.ts",
]
`)
}
else {
expect(files).toMatchInlineSnapshot(`
[
"<process-cwd>/fixtures/src/math.ts",
"<process-cwd>/fixtures/src/conditional/node.ts",
"<process-cwd>/fixtures/src/conditional/ssr.ts",
]
`)
}
})

test('uncovered files are transformed correctly (jsdom)', async ({ skip }) => {
skip(isBrowser(), 'node relevant test')

await runVitest({
config: 'fixtures/configs/vitest.config.conditional.ts',
include: ['fixtures/test/math.test.ts'],
environment: 'jsdom',
coverage: {
include: ['fixtures/src/math.ts', 'fixtures/src/conditional/*'],
reporter: 'json',
},
})

const coverageMap = await readCoverageMap()
const files = coverageMap.files()

expect(files).toMatchInlineSnapshot(`
[
"<process-cwd>/fixtures/src/math.ts",
"<process-cwd>/fixtures/src/conditional/web.ts",
]
`)
Comment on lines +275 to +280
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test failure without the fix. Demonstrates the minimal repro's issue.

})

test('files included and excluded in plugin\'s configureVitest are excluded', async () => {
await runVitest({
config: 'fixtures/configs/vitest.config.configure-vitest-hook.ts',
Expand Down
Loading