Skip to content

Commit

Permalink
fix(coverage): apply vite-node's wrapper only to executed files
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Apr 30, 2024
1 parent bfe8ad9 commit d1b4dd4
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,17 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
source: string
originalSource: string
sourceMap?: { sourcemap: EncodedSourceMap }
isExecuted: boolean
}> {
const filePath = normalize(fileURLToPath(url))

const transformResult = transformResults.get(filePath) || await this.ctx.vitenode.transformRequest(filePath).catch(() => {})
let isExecuted = true
let transformResult: FetchResult | Awaited<ReturnType<typeof this.ctx.vitenode.transformRequest>> = transformResults.get(filePath)

if (!transformResult) {
isExecuted = false
transformResult = await this.ctx.vitenode.transformRequest(filePath).catch(() => null)
}

const map = transformResult?.map as (EncodedSourceMap | undefined)
const code = transformResult?.code
Expand All @@ -327,6 +334,7 @@ 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 {
isExecuted,
source: code || sourcesContent,
originalSource: sourcesContent,
}
Expand All @@ -337,6 +345,7 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
sources[0] = new URL(map.sources[0], url).href

return {
isExecuted,
originalSource: sourcesContent,
source: code || sourcesContent,
sourceMap: {
Expand Down Expand Up @@ -368,8 +377,8 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
await Promise.all(chunk.map(async ({ url, functions }) => {
const sources = await this.getSources(url, transformResults, functions)

// If no source map was found from vite-node we can assume this file was not run in the wrapper
const wrapperLength = sources.sourceMap ? WRAPPER_LENGTH : 0
// If file was executed by vite-node we'll need to add its wrapper
const wrapperLength = sources.isExecuted ? WRAPPER_LENGTH : 0

const converter = v8ToIstanbul(url, wrapperLength, sources, undefined, this.options.ignoreEmptyLines)
await converter.load()
Expand Down

0 comments on commit d1b4dd4

Please sign in to comment.