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 12, 2024
1 parent e9f9adc commit f5e1827
Show file tree
Hide file tree
Showing 22 changed files with 970 additions and 12 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.
10 changes: 7 additions & 3 deletions packages/coverage-istanbul/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,16 +396,20 @@ 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()

const transform = this.createUncoveredFileTransformer(this.ctx)

// 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}`)
await transform(`${filename}?v=${cacheKey}`)

const lastCoverage = this.instrumenter.lastFileCoverage()
coverageMap.addFileCoverage(lastCoverage)
}
Expand Down
9 changes: 6 additions & 3 deletions packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ export class V8CoverageProvider
const transformResults = normalizeTransformResults(
this.ctx.vitenode.fetchCache,
)
const transform = this.createUncoveredFileTransformer(this.ctx)

const allFiles = await this.testExclude.glob(this.ctx.config.root)
let includedFiles = allFiles.map(file =>
Expand Down Expand Up @@ -396,6 +397,7 @@ export class V8CoverageProvider
const { originalSource, source } = await this.getSources(
filename.href,
transformResults,
transform,
)

// Ignore empty files, e.g. files that contain only typescript types and no runtime code
Expand Down Expand Up @@ -441,6 +443,7 @@ export class V8CoverageProvider
private async getSources(
url: string,
transformResults: TransformResults,
transform: ReturnType<typeof this.createUncoveredFileTransformer>,
functions: Profiler.FunctionCoverage[] = [],
): Promise<{
source: string
Expand All @@ -458,9 +461,7 @@ export class V8CoverageProvider

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

const map = transformResult?.map as EncodedSourceMap | undefined
Expand Down Expand Up @@ -515,6 +516,7 @@ export class V8CoverageProvider
? viteNode.fetchCaches[transformMode]
: viteNode.fetchCache
const transformResults = normalizeTransformResults(fetchCache)
const transform = this.createUncoveredFileTransformer(this.ctx)

const scriptCoverages = coverage.result.filter(result =>
this.testExclude.shouldInstrument(fileURLToPath(result.url)),
Expand All @@ -536,6 +538,7 @@ export class V8CoverageProvider
const sources = await this.getSources(
url,
transformResults,
transform,
functions,
)

Expand Down
33 changes: 32 additions & 1 deletion packages/vitest/src/utils/coverage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { relative } from 'pathe'
import mm from 'micromatch'
import type { CoverageMap } from 'istanbul-lib-coverage'
import type { BaseCoverageOptions, ResolvedCoverageOptions } from '../types'
import type { BaseCoverageOptions, ResolvedCoverageOptions, Vitest } from '../types'

type Threshold = 'lines' | 'functions' | 'statements' | 'branches'

Expand Down Expand Up @@ -293,6 +293,37 @@ export class BaseCoverageProvider {
return chunks
}, [])
}

createUncoveredFileTransformer(ctx: Vitest) {
const servers = [
...ctx.projects.map(project => ({
root: project.config.root,
vitenode: project.vitenode,
})),
// Check core last as it will match all files anyway
{ root: ctx.config.root, vitenode: ctx.vitenode },
]

return async function transformFile(filename: string) {
let lastError

for (const { root, vitenode } of servers) {
if (!filename.startsWith(root)) {
continue
}

try {
return await vitenode.transformRequest(filename)
}
catch (error) {
lastError = error
}
}

// All vite-node servers failed to transform the file
throw lastError
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Plugin, defineWorkspace } from "vitest/config";
import MagicString from "magic-string";
import { readFileSync } from "fs";

export default defineWorkspace([
// Project that uses its own "root" and custom transform plugin
{
test: {
name: "custom-with-root",
root: "fixtures/workspaces/custom-2",
},
plugins: [customFilePlugin("2")],
},

// Project that cannot transform "*.custom-x" files
{
test: {
name: "normal",
include: ["fixtures/test/math.test.ts"],
},
},

// Project that uses default "root" and has custom transform plugin
{
test: {
name: "custom",
include: ["fixtures/test/custom-1-syntax.test.ts"],
},
plugins: [customFilePlugin("1")],
},
]);

/**
* Plugin for transforming `.custom-1` and/or `.custom-2` files to Javascript
*/
function customFilePlugin(postfix: "1" | "2"): Plugin {
function transform(code: MagicString) {
code.replaceAll(
"<function covered>",
`
function covered() {
return "Custom-${postfix} 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()");
}

return {
name: `custom-${postfix}-file-plugin`,
transform(_, id) {
const filename = id.split("?")[0];

if (filename.endsWith(`.custom-${postfix}`)) {
const content = readFileSync(filename, "utf8");

const s = new MagicString(content);
transform(s);

return {
code: s.toString(),
map: s.generateMap({ hires: "boundary" }),
};
}
},
};
}
5 changes: 5 additions & 0 deletions test/coverage-test/fixtures/src/covered.custom-1
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-1
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-1-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-1'

test('custom file loads fine', () => {
expect(output).toMatch('Custom-1 file loaded!')
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<function covered>

<function uncovered>

<default export covered>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<function uncovered>

<default export uncovered>
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-2'

test('custom-2 file loads fine', () => {
expect(output).toMatch('Custom-2 file loaded!')
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"path": "<process-cwd>/fixtures/src/covered.custom-1",
"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 f5e1827

Please sign in to comment.