Skip to content

Commit

Permalink
fix(coverage): use project specific vitenode for uncovered files
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jul 11, 2024
1 parent 99a12ae commit ae226bd
Show file tree
Hide file tree
Showing 14 changed files with 546 additions and 11 deletions.
4 changes: 1 addition & 3 deletions docs/guide/workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,4 @@ All configuration options that are not supported inside a project config have <N

## Coverage

Coverage for workspace projects works out of the box. But if you have [`all`](/config/#coverage-all) option enabled and use non-conventional extensions in some of your projects, you will need to have a plugin that handles this extension in your root configuration file.

For example, if you have a package that uses Vue files and it has its own config file, but some of the files are not imported in your tests, coverage will fail trying to analyze the usage of unused files, because it relies on the root configuration rather than project configuration.
Coverage for workspace projects works out of the box.
24 changes: 21 additions & 3 deletions packages/coverage-istanbul/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ export class IstanbulCoverageProvider
resolve(this.ctx.config.root, file),
)

const viteNodeServers = [
this.ctx.vitenode,
...this.ctx.projects.map(project => project.vitenode),
]

if (this.ctx.config.changed) {
includedFiles = (this.ctx.config.related || []).filter(file =>
includedFiles.includes(file),
Expand All @@ -396,16 +401,29 @@ export class IstanbulCoverageProvider
.filter(file => !coveredFiles.includes(file))
.sort()

const cacheKey = new Date().getTime()
const coverageMap = libCoverage.createCoverageMap({})

// Make sure file is not served from cache so that instrumenter loads up requested file coverage
const cacheKey = new Date().getTime()

// Note that these cannot be run parallel as synchronous instrumenter.lastFileCoverage
// returns the coverage of the last transformed file
for (const [index, filename] of uncoveredFiles.entries()) {
debug('Uncovered file %s %d/%d', filename, index, uncoveredFiles.length)

// Make sure file is not served from cache so that instrumenter loads up requested file coverage
await this.ctx.vitenode.transformRequest(`${filename}?v=${cacheKey}`)
for (const vitenode of viteNodeServers) {
try {
await vitenode.transformRequest(`${filename}?v=${cacheKey}`)
continue
}
catch (error) {
// Try transforming with next ViteNode server, unless last
if (viteNodeServers.indexOf(vitenode) === viteNodeServers.length - 1) {
throw error
}
}
}

const lastCoverage = this.instrumenter.lastFileCoverage()
coverageMap.addFileCoverage(lastCoverage)
}
Expand Down
20 changes: 17 additions & 3 deletions packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,11 @@ export class V8CoverageProvider
}> {
const filePath = normalize(fileURLToPath(url))

const viteNodeServers = [
this.ctx.vitenode,
...this.ctx.projects.map(project => project.vitenode),
]

let isExecuted = true
let transformResult:
| FetchResult
Expand All @@ -458,9 +463,18 @@ export class V8CoverageProvider

if (!transformResult) {
isExecuted = false
transformResult = await this.ctx.vitenode
.transformRequest(filePath)
.catch(() => null)
for (const vitenode of viteNodeServers) {
try {
transformResult = await vitenode.transformRequest(filePath)
continue
}
catch (error) {
// Try transforming with next ViteNode server, unless last
if (viteNodeServers.indexOf(vitenode) === viteNodeServers.length - 1) {
throw error
}
}
}
}

const map = transformResult?.map as EncodedSourceMap | undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Plugin, defineWorkspace } from "vitest/config"
import MagicString from "magic-string"
import { readFileSync } from "fs"

export default defineWorkspace([
{
test: {
name: "normal",
include: ["fixtures/test/math.test.ts"],
},
},
{
test: {
name: "special",
include: ["fixtures/test/custom-syntax.test.ts"],
},
plugins: [customFilePlugin()],
},
])

/**
* Plugin for transforming `.custom` files to Javascript
*/
function customFilePlugin(): Plugin {
return {
name: "custom-file-plugin",
transform(_, id) {
const filename = id.split("?")[0]

if (filename.endsWith(".custom")) {
const content = readFileSync(filename, "utf8")

const s = new MagicString(content)
transformCustomLang(s)

return {
code: s.toString(),
map: s.generateMap({ hires: "boundary" }),
}
}
},
}
}

// Syntax of custom-language
function transformCustomLang(code: MagicString) {
code.replaceAll(
"<function covered>",
`
function covered() {
return "Custom file loaded!"
}
`.trim()
)

code.replaceAll(
"<function uncovered>",
`
function uncovered() {
return "This should be uncovered!"
}
`.trim()
)

code.replaceAll("<default export covered>", "export default covered()")
code.replaceAll("<default export uncovered>", "export default uncovered()")
}
5 changes: 5 additions & 0 deletions test/coverage-test/fixtures/src/covered.custom
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<function covered>

<function uncovered>

<default export covered>
3 changes: 3 additions & 0 deletions test/coverage-test/fixtures/src/uncovered.custom
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<function uncovered>

<default export uncovered>
8 changes: 8 additions & 0 deletions test/coverage-test/fixtures/test/custom-syntax.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { expect, test } from 'vitest'

// @ts-expect-error -- untyped
import output from '../src/covered.custom'

test('custom file loads fine', () => {
expect(output).toMatch('Custom file loaded!')
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"path": "<process-cwd>/fixtures/src/covered.custom",
"statementMap": {
"0": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 18
}
},
"1": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 20
}
}
},
"fnMap": {
"0": {
"name": "covered",
"decl": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 18
}
},
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 18
}
}
},
"1": {
"name": "uncovered",
"decl": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 20
}
},
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 20
}
}
}
},
"branchMap": {},
"s": {
"0": 1,
"1": 0
},
"f": {
"0": 1,
"1": 0
},
"b": {}
}
Loading

0 comments on commit ae226bd

Please sign in to comment.