Skip to content

Commit

Permalink
fix(coverage): use project specific vitenode (vitest-dev#5856)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jul 6, 2024
1 parent 42bd4a2 commit e7ad735
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 10 deletions.
30 changes: 21 additions & 9 deletions packages/coverage-istanbul/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,7 @@ export class IstanbulCoverageProvider
},
})

this.testExclude = new _TestExclude({
cwd: ctx.config.root,
include: this.options.include,
exclude: this.options.exclude,
excludeNodeModules: true,
extension: this.options.extension,
relativePath: !this.options.allowExternal,
})
this.testExclude = this.createTestExclude(ctx.config.root)

const shard = this.ctx.config.shard
const tempDirectory = `.tmp${
Expand Down Expand Up @@ -386,6 +379,11 @@ export class IstanbulCoverageProvider
resolve(this.ctx.config.root, file),
)

const projects = this.ctx.projects.map(project => ({
testExclude: this.createTestExclude(project.config.root),
vitenode: project.vitenode,
}))

if (this.ctx.config.changed) {
includedFiles = (this.ctx.config.related || []).filter(file =>
includedFiles.includes(file),
Expand All @@ -404,14 +402,28 @@ export class IstanbulCoverageProvider
for (const [index, filename] of uncoveredFiles.entries()) {
debug('Uncovered file %s %d/%d', filename, index, uncoveredFiles.length)

const project = projects.find(p => p.testExclude.shouldInstrument(filename))
const vitenode = project?.vitenode || this.ctx.vitenode

// 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 vitenode.transformRequest(`${filename}?v=${cacheKey}`)
const lastCoverage = this.instrumenter.lastFileCoverage()
coverageMap.addFileCoverage(lastCoverage)
}

return coverageMap
}

private createTestExclude(cwd: string) {
return new _TestExclude({
cwd,
include: this.options.include,
exclude: this.options.exclude,
excludeNodeModules: true,
extension: this.options.extension,
relativePath: !this.options.allowExternal,
}) as InstanceType<TestExclude>
}
}

async function transformCoverage(coverageMap: CoverageMap) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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()]
}
])

function customFilePlugin(): Plugin {
return {
name: 'load-custom-files',
load(id) {
if(id.endsWith(".custom")) {
const content = readFileSync(id, 'utf8')

const s = new MagicString(content)
s.replace('<function covered>', `
function covered() {
return "Custom file loaded!"
}`.trim());

s.replace('<function uncovered>', `
function uncovered() {
return "This should be uncovered!"
}`.trim());

s.replace('<default export covered>', 'export default covered()');

return { code: s.toString(), map: s.generateMap({ hires: 'boundary'})}
}
},
}
}
5 changes: 5 additions & 0 deletions test/coverage-test/fixtures/src/file.custom
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<function covered>

<function uncovered>

<default export covered>
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/file.custom'

test('custom file loads fine', () => {
expect(output).toMatch('Custom file loaded!')
})
3 changes: 2 additions & 1 deletion test/coverage-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"type": "module",
"private": true,
"scripts": {
"test": "vitest --workspace=vitest.workspace.custom.ts"
"test": "vitest --workspace=vitest.workspace.custom.ts",
"vitest": "vitest"
},
"devDependencies": {
"@ampproject/remapping": "^2.2.1",
Expand Down
23 changes: 23 additions & 0 deletions test/coverage-test/test/workspace.multi-transform.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expect } from 'vitest'
import { readCoverageMap, runVitest, test } from '../utils'

test('{ all: true } includes uncovered files', async () => {
await runVitest({
workspace: 'fixtures/configs/vitest.workspace.multi-transforms.ts',
coverage: {
all: false,
extension: ['.ts', '.custom'],
reporter: ['json', 'html'],
},
})

const coverageMap = await readCoverageMap()
const files = coverageMap.files()

expect(files).toMatchInlineSnapshot(`
[
"<process-cwd>/fixtures/src/file.custom",
"<process-cwd>/fixtures/src/math.ts",
]
`)
})

0 comments on commit e7ad735

Please sign in to comment.