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): v8 to ignore type-only files #5328

Merged
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
1 change: 1 addition & 0 deletions packages/coverage-v8/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"magicast": "^0.3.3",
"picocolors": "^1.0.0",
"std-env": "^3.5.0",
"strip-literal": "^2.0.0",
"test-exclude": "^6.0.0",
"v8-to-istanbul": "^9.2.0"
},
Expand Down
26 changes: 20 additions & 6 deletions packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import remapping from '@ampproject/remapping'
import { normalize, resolve } from 'pathe'
import c from 'picocolors'
import { provider } from 'std-env'
import { stripLiteral } from 'strip-literal'
import createDebug from 'debug'
import { cleanUrl } from 'vite-node/utils'
import type { EncodedSourceMap, FetchResult } from 'vite-node'
Expand Down Expand Up @@ -260,7 +261,13 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
}

const coverages = await Promise.all(chunk.map(async (filename) => {
const { source } = await this.getSources(filename.href, transformResults)
const transformResult = await this.ctx.vitenode.transformRequest(filename.pathname).catch(() => {})

// Ignore empty files, e.g. files that contain only typescript types and no runtime code
if (transformResult && stripLiteral(transformResult.code).trim() === '')
return null

const { originalSource } = await this.getSources(filename.href, transformResults)

const coverage = {
url: filename.href,
Expand All @@ -269,7 +276,7 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
functions: [{
ranges: [{
startOffset: 0,
endOffset: source.length,
endOffset: originalSource.length,
count: 0,
}],
isBlockCoverage: true,
Expand All @@ -281,15 +288,18 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
return { result: [coverage] }
}))

merged = mergeProcessCovs([merged, ...coverages])
merged = mergeProcessCovs([
merged,
...coverages.filter((cov): cov is NonNullable<typeof cov> => cov != null),
])
}

return merged
}

private async getSources(url: string, transformResults: TransformResults, functions: Profiler.FunctionCoverage[] = []): Promise<{
source: string
originalSource?: string
originalSource: string
sourceMap?: { sourcemap: EncodedSourceMap }
}> {
const filePath = normalize(fileURLToPath(url))
Expand All @@ -306,8 +316,12 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
})

// These can be uncovered files included by "all: true" or files that are loaded outside vite-node
if (!map)
return { source: code || sourcesContent }
if (!map) {
return {
source: code || sourcesContent,
originalSource: sourcesContent,
}
}

return {
originalSource: sourcesContent,
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/coverage-test/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type First = 'This' | 'file' | 'should'

export type Second = 'be' | 'excluded' | 'from' | 'report'
Loading