Skip to content

Commit

Permalink
fix(coverage): v8 to ignore type-only files (#5328)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Mar 4, 2024
1 parent ab60bf8 commit c3eb8de
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
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'

0 comments on commit c3eb8de

Please sign in to comment.