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): apply vite-node's wrapper only to executed files #5642

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,54 @@ exports[`istanbul json report 1`] = `
},
},
},
"<process-cwd>/src/load-outside-vite.cjs": {
"b": {},
"branchMap": {},
"f": {
"0": 0,
},
"fnMap": {
"0": {
"decl": {
"end": {
"column": 30,
"line": 1,
},
"start": {
"column": 26,
"line": 1,
},
},
"loc": {
"end": {
"column": 35,
"line": 1,
},
"start": {
"column": 33,
"line": 1,
},
},
"name": "noop",
},
},
"path": "<process-cwd>/src/load-outside-vite.cjs",
"s": {
"0": 0,
},
"statementMap": {
"0": {
"end": {
"column": 35,
"line": 1,
},
"start": {
"column": 0,
"line": 1,
},
},
},
},
"<process-cwd>/src/multi-environment.ts": {
"b": {
"0": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4345,6 +4345,56 @@ exports[`v8 json report 1`] = `
},
},
},
"<process-cwd>/src/load-outside-vite.cjs": {
"all": false,
"b": {},
"branchMap": {},
"f": {
"0": 0,
},
"fnMap": {
"0": {
"decl": {
"end": {
"column": 35,
"line": 1,
},
"start": {
"column": 17,
"line": 1,
},
},
"line": 1,
"loc": {
"end": {
"column": 35,
"line": 1,
},
"start": {
"column": 17,
"line": 1,
},
},
"name": "noop",
},
},
"path": "<process-cwd>/src/load-outside-vite.cjs",
"s": {
"0": 1,
},
"statementMap": {
"0": {
"end": {
"column": 35,
"line": 1,
},
"start": {
"column": 0,
"line": 1,
},
},
},
},
"<process-cwd>/src/multi-environment.ts": {
"all": false,
"b": {
Expand Down
1 change: 1 addition & 0 deletions test/coverage-test/src/load-outside-vite.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = function noop() {}
7 changes: 7 additions & 0 deletions test/coverage-test/test/coverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,10 @@ test.runIf(provider === 'v8' || provider === 'custom')('pre-transpiled code with

transpiled.hello()
})

test.runIf(provider === 'v8' || provider === 'custom')('file loaded outside Vite, #5639', async () => {
const { Module: { createRequire } } = await import('node:module')

const noop = createRequire(import.meta.url)('../src/load-outside-vite.cjs')
expect(noop).toBeTypeOf('function')
})
Loading