Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(coverage): validate extension, when reporting c8 coverage #2626

Merged
merged 2 commits into from Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/coverage-c8/package.json
Expand Up @@ -46,6 +46,7 @@
"vitest": "workspace:*"
},
"devDependencies": {
"pathe": "^0.2.0",
"vite-node": "workspace:*"
}
}
12 changes: 11 additions & 1 deletion packages/coverage-c8/src/provider.ts
Expand Up @@ -2,7 +2,7 @@ import { existsSync, promises as fs } from 'fs'
import _url from 'url'
import type { Profiler } from 'inspector'
import { takeCoverage } from 'v8'
import { resolve } from 'pathe'
import { extname, resolve } from 'pathe'
import type { RawSourceMap } from 'vite-node'
import { configDefaults } from 'vitest/config'
// eslint-disable-next-line no-restricted-imports
Expand Down Expand Up @@ -53,6 +53,7 @@ export class C8CoverageProvider implements CoverageProvider {

// add source maps
const sourceMapMeta: Record<SourceMapMeta['url'], MapAndSource> = {}
const extensions = Array.isArray(this.options.extension) ? this.options.extension : [this.options.extension]

const entries = Array
.from(this.ctx.vitenode.fetchCache.entries())
Expand All @@ -66,6 +67,7 @@ export class C8CoverageProvider implements CoverageProvider {
return {
filepath,
url: _url.pathToFileURL(filepath).href,
id: file,
map: result.map,
source: result.code,
}
Expand All @@ -74,6 +76,11 @@ export class C8CoverageProvider implements CoverageProvider {
if (!entry)
return false

const extension = extname(entry.id) || extname(entry.url)

if (!extensions.includes(extension))
return false

// Mappings and sourcesContent are needed for C8 to work
return (
entry.map.mappings.length > 0
Expand All @@ -84,6 +91,9 @@ export class C8CoverageProvider implements CoverageProvider {
}) as SourceMapMeta[]

await Promise.all(entries.map(async ({ url, source, map, filepath }) => {
if (url in sourceMapMeta)
return

let code: string | undefined
try {
code = (await fs.readFile(filepath)).toString()
Expand Down