Skip to content

Commit

Permalink
fix(coverage): use project specific vitenode
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jul 7, 2024
1 parent 42bd4a2 commit fbea520
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 5 deletions.
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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) {
const filename = id.split("?")[0]

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

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

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

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

return { code: s.toString(), map: s.generateMap({ hires: 'boundary'})}
}
},
}
}
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!')
})
4 changes: 2 additions & 2 deletions test/coverage-test/test/all.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { readCoverageMap, runVitest, test } from '../utils'
test('{ all: true } includes uncovered files', async () => {
await runVitest({
include: ['fixtures/test/**'],
exclude: ['**/virtual-files-**'],
exclude: ['**/virtual-files-**', '**/custom-syntax**'],
coverage: {
include: ['fixtures/src/**'],
all: true,
Expand All @@ -25,7 +25,7 @@ test('{ all: true } includes uncovered files', async () => {
test('{ all: false } excludes uncovered files', async () => {
await runVitest({
include: ['fixtures/test/**'],
exclude: ['**/virtual-files-**'],
exclude: ['**/virtual-files-**', '**/custom-syntax**'],
coverage: {
include: ['fixtures/src/**'],
all: false,
Expand Down
1 change: 1 addition & 0 deletions test/coverage-test/test/changed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ afterAll(() => {
test('{ changed: "HEAD" }', async () => {
await runVitest({
include: ['fixtures/test/**'],
exclude: ['**/custom-syntax**'],
changed: 'HEAD',
coverage: {
include: ['fixtures/src/**'],
Expand Down
25 changes: 25 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,25 @@
import { expect } from 'vitest'
import { readCoverageMap, runVitest, test } from '../utils'

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

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

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

0 comments on commit fbea520

Please sign in to comment.