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
6 changes: 4 additions & 2 deletions packages/browser/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,13 @@ export default (parentServer: ParentBrowserProject, base = '/'): Plugin[] => {
{
name: 'vitest:browser:in-source-tests',
transform(code, id) {
const filename = cleanUrl(id)
const project = parentServer.vitest.getProjectByName(parentServer.config.name)
if (!project._isCachedTestFile(id) || !code.includes('import.meta.vitest')) {

if (!project._isCachedTestFile(filename) || !code.includes('import.meta.vitest')) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This change is needed so that /Users/x/vitest/test/coverage-test/fixtures/src/in-source.ts?import&browserv=1747838764408 is recognized as cached test file.

return
}
const s = new MagicString(code, { filename: cleanUrl(id) })
const s = new MagicString(code, { filename })
s.prepend(
`import.meta.vitest = __vitest_index__;\n`,
)
Expand Down
4 changes: 0 additions & 4 deletions packages/coverage-v8/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,5 @@ function filterResult(coverage: ScriptCoverage['result'][number]): boolean {
return false
}

if (coverage.url.includes('?browserv=') || coverage.url.includes('&browserv=')) {
return false
}

Comment on lines -74 to -77
Copy link
Member Author

Choose a reason for hiding this comment

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

Need to check with Storybook folks to see that this doesn't break coverage reports there. This was used to exclude *.stories.* from appearing in coverage report.

Copy link
Member Author

Choose a reason for hiding this comment

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

*.stories.* are now visible on coverage report 😢

Copy link
Member

Choose a reason for hiding this comment

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

Storybook can now exclude story files inside the configureVitest hook

Copy link
Member Author

@AriPerkkio AriPerkkio May 21, 2025

Choose a reason for hiding this comment

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

Yep, that works! Using this in fresh Storybook project fixes the issue:

      {
        name: "exclude-stories",
        configureVitest(context) {
          if (context.vitest.config.coverage.enabled) {
            context.vitest.config.coverage.exclude.push("**/*.stories.**");
          }
        },

Related tests in #8009

return true
}
3 changes: 3 additions & 0 deletions packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ export class V8CoverageProvider extends BaseCoverageProvider<ResolvedCoverageOpt
if (result.url.startsWith('/@fs')) {
result.url = `${FILE_PROTOCOL}${removeStartsWith(result.url, '/@fs')}`
}
else if (result.url.startsWith(project.config.root)) {
result.url = `${FILE_PROTOCOL}${result.url}`
}
else {
result.url = `${FILE_PROTOCOL}${project.config.root}${result.url}`
}
Expand Down
17 changes: 17 additions & 0 deletions test/coverage-test/fixtures/src/in-source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference types="vitest/importMeta" />

export function add(a: number, b: number) {
if(a === 5 && b === 7) {
return 12;
}

return a + b
}

if (import.meta.vitest) {
const { test, expect } = import.meta.vitest

test('in source test running add function', () => {
expect(add(10, 19)).toBe(29)
})
}
47 changes: 47 additions & 0 deletions test/coverage-test/test/in-source.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { expect } from 'vitest'
import { isV8Provider, readCoverageMap, runVitest, test } from '../utils'

test('in-source tests work', async () => {
const { stdout } = await runVitest({
include: [],
includeSource: ['fixtures/src/in-source.ts'],
coverage: { all: false, reporter: 'json' },
})

expect(stdout).toContain('in source test running add function')

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

expect(files).toMatchInlineSnapshot(`
[
"<process-cwd>/fixtures/src/in-source.ts",
]
`)

const fileCoverage = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/in-source.ts')

// If-branch is not taken - makes sure source maps are correct in in-source testing too
expect(fileCoverage.getUncoveredLines()).toContain('5')

if (isV8Provider()) {
expect(fileCoverage).toMatchInlineSnapshot(`
{
"branches": "2/4 (50%)",
"functions": "1/1 (100%)",
"lines": "10/12 (83.33%)",
"statements": "10/12 (83.33%)",
}
`)
}
else {
expect(fileCoverage).toMatchInlineSnapshot(`
{
"branches": "3/6 (50%)",
"functions": "2/2 (100%)",
"lines": "6/7 (85.71%)",
"statements": "6/7 (85.71%)",
}
`)
}
})
2 changes: 2 additions & 0 deletions test/coverage-test/vitest.workspace.custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export default defineWorkspace([
'**/temporary-files.test.ts',
'**/test-reporter-conflicts.test.ts',
'**/vue.test.ts',
'**/in-source.test.ts',
],
},
},
Expand All @@ -126,6 +127,7 @@ export default defineWorkspace([
'**/temporary-files.test.ts',
'**/test-reporter-conflicts.test.ts',
'**/vue.test.ts',
'**/in-source.test.ts',
],
},
},
Expand Down
Loading