Skip to content

Commit c9883f3

Browse files
authoredMay 2, 2024··
fix(coverage): apply vite-node's wrapper only to executed files (#5642)
1 parent 40c299f commit c9883f3

File tree

5 files changed

+118
-3
lines changed

5 files changed

+118
-3
lines changed
 

‎packages/coverage-v8/src/provider.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,17 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
310310
source: string
311311
originalSource: string
312312
sourceMap?: { sourcemap: EncodedSourceMap }
313+
isExecuted: boolean
313314
}> {
314315
const filePath = normalize(fileURLToPath(url))
315316

316-
const transformResult = transformResults.get(filePath) || await this.ctx.vitenode.transformRequest(filePath).catch(() => {})
317+
let isExecuted = true
318+
let transformResult: FetchResult | Awaited<ReturnType<typeof this.ctx.vitenode.transformRequest>> = transformResults.get(filePath)
319+
320+
if (!transformResult) {
321+
isExecuted = false
322+
transformResult = await this.ctx.vitenode.transformRequest(filePath).catch(() => null)
323+
}
317324

318325
const map = transformResult?.map as (EncodedSourceMap | undefined)
319326
const code = transformResult?.code
@@ -327,6 +334,7 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
327334
// These can be uncovered files included by "all: true" or files that are loaded outside vite-node
328335
if (!map) {
329336
return {
337+
isExecuted,
330338
source: code || sourcesContent,
331339
originalSource: sourcesContent,
332340
}
@@ -337,6 +345,7 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
337345
sources[0] = new URL(map.sources[0], url).href
338346

339347
return {
348+
isExecuted,
340349
originalSource: sourcesContent,
341350
source: code || sourcesContent,
342351
sourceMap: {
@@ -368,8 +377,8 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
368377
await Promise.all(chunk.map(async ({ url, functions }) => {
369378
const sources = await this.getSources(url, transformResults, functions)
370379

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

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

‎test/coverage-test/coverage-report-tests/__snapshots__/istanbul.report.test.ts.snap

+48
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,54 @@ exports[`istanbul json report 1`] = `
18951895
},
18961896
},
18971897
},
1898+
"<process-cwd>/src/load-outside-vite.cjs": {
1899+
"b": {},
1900+
"branchMap": {},
1901+
"f": {
1902+
"0": 0,
1903+
},
1904+
"fnMap": {
1905+
"0": {
1906+
"decl": {
1907+
"end": {
1908+
"column": 30,
1909+
"line": 1,
1910+
},
1911+
"start": {
1912+
"column": 26,
1913+
"line": 1,
1914+
},
1915+
},
1916+
"loc": {
1917+
"end": {
1918+
"column": 35,
1919+
"line": 1,
1920+
},
1921+
"start": {
1922+
"column": 33,
1923+
"line": 1,
1924+
},
1925+
},
1926+
"name": "noop",
1927+
},
1928+
},
1929+
"path": "<process-cwd>/src/load-outside-vite.cjs",
1930+
"s": {
1931+
"0": 0,
1932+
},
1933+
"statementMap": {
1934+
"0": {
1935+
"end": {
1936+
"column": 35,
1937+
"line": 1,
1938+
},
1939+
"start": {
1940+
"column": 0,
1941+
"line": 1,
1942+
},
1943+
},
1944+
},
1945+
},
18981946
"<process-cwd>/src/multi-environment.ts": {
18991947
"b": {
19001948
"0": [

‎test/coverage-test/coverage-report-tests/__snapshots__/v8.report.test.ts.snap

+50
Original file line numberDiff line numberDiff line change
@@ -4345,6 +4345,56 @@ exports[`v8 json report 1`] = `
43454345
},
43464346
},
43474347
},
4348+
"<process-cwd>/src/load-outside-vite.cjs": {
4349+
"all": false,
4350+
"b": {},
4351+
"branchMap": {},
4352+
"f": {
4353+
"0": 0,
4354+
},
4355+
"fnMap": {
4356+
"0": {
4357+
"decl": {
4358+
"end": {
4359+
"column": 35,
4360+
"line": 1,
4361+
},
4362+
"start": {
4363+
"column": 17,
4364+
"line": 1,
4365+
},
4366+
},
4367+
"line": 1,
4368+
"loc": {
4369+
"end": {
4370+
"column": 35,
4371+
"line": 1,
4372+
},
4373+
"start": {
4374+
"column": 17,
4375+
"line": 1,
4376+
},
4377+
},
4378+
"name": "noop",
4379+
},
4380+
},
4381+
"path": "<process-cwd>/src/load-outside-vite.cjs",
4382+
"s": {
4383+
"0": 1,
4384+
},
4385+
"statementMap": {
4386+
"0": {
4387+
"end": {
4388+
"column": 35,
4389+
"line": 1,
4390+
},
4391+
"start": {
4392+
"column": 0,
4393+
"line": 1,
4394+
},
4395+
},
4396+
},
4397+
},
43484398
"<process-cwd>/src/multi-environment.ts": {
43494399
"all": false,
43504400
"b": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = function noop() {}

‎test/coverage-test/test/coverage.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,10 @@ test.runIf(provider === 'v8' || provider === 'custom')('pre-transpiled code with
8484

8585
transpiled.hello()
8686
})
87+
88+
test.runIf(provider === 'v8' || provider === 'custom')('file loaded outside Vite, #5639', async () => {
89+
const { Module: { createRequire } } = await import('node:module')
90+
91+
const noop = createRequire(import.meta.url)('../src/load-outside-vite.cjs')
92+
expect(noop).toBeTypeOf('function')
93+
})

0 commit comments

Comments
 (0)
Please sign in to comment.