Skip to content

Commit

Permalink
fix(coverage): workspaces c8 source maps (#3226)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Apr 27, 2023
1 parent 8d4606e commit efce3b4
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/coverage-c8/src/provider.ts
Expand Up @@ -100,8 +100,12 @@ export class C8CoverageProvider extends BaseCoverageProvider implements Coverage
const sourceMapMeta: Record<SourceMapMeta['url'], MapAndSource> = {}
const extensions = Array.isArray(this.options.extension) ? this.options.extension : [this.options.extension]

const fetchCache = this.ctx.projects.map(project =>
Array.from(project.vitenode.fetchCache.entries()),
).flat()

const entries = Array
.from(this.ctx.vitenode.fetchCache.entries())
.from(fetchCache)
.filter(entry => report._shouldInstrument(entry[0]))
.map(([file, { result }]) => {
if (!result.map)
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions test/workspaces/coverage-report-tests/check-coverage.test.ts
@@ -0,0 +1,22 @@
import { existsSync, readFileSync } from 'node:fs'
import { normalize } from 'node:path'
import { expect, test } from 'vitest'
import libCoverage from 'istanbul-lib-coverage'
import { resolve } from 'pathe'

test('coverage exists', () => {
expect(existsSync('./coverage')).toBe(true)
expect(existsSync('./coverage/index.html')).toBe(true)
})

test('file coverage summary matches', () => {
const coverageJson = JSON.parse(readFileSync('./coverage/coverage-final.json', 'utf-8'))
const coverageMap = libCoverage.createCoverageMap(coverageJson)
const fileCoverage = coverageMap.fileCoverageFor(normalize(resolve('./src/math.ts')))

// There should be 1 uncovered branch and 1 uncovered function. See math.ts.
const { branches, functions } = fileCoverage.toSummary()

expect(branches.total - branches.covered).toBe(1)
expect(functions.total - functions.covered).toBe(1)
})
7 changes: 7 additions & 0 deletions test/workspaces/coverage-report-tests/vitest.config.ts
@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
include: ['./check-coverage.test.ts'],
},
})
7 changes: 5 additions & 2 deletions test/workspaces/package.json
Expand Up @@ -3,10 +3,13 @@
"type": "module",
"private": true,
"scripts": {
"test": "vitest",
"coverage": "vitest run --coverage"
"test": "pnpm test:workspace && pnpm test:coverage",
"test:workspace": "vitest run",
"test:coverage": "vitest run --root coverage-report-tests"
},
"devDependencies": {
"@types/istanbul-lib-coverage": "^2.0.4",
"istanbul-lib-coverage": "^3.2.0",
"jsdom": "latest",
"vitest": "workspace:*"
}
Expand Down
11 changes: 11 additions & 0 deletions test/workspaces/src/math.ts
@@ -1,3 +1,14 @@
/* eslint-disable unused-imports/no-unused-vars */
export function sum(a: number, b: number) {
if (a === 3 && b === 4) {
// This should be uncovered
return 7
}

return a + b
}

function uncoveredFunction() {
// This should be uncovered
return 1
}
1 change: 1 addition & 0 deletions test/workspaces/vitest.config.ts
Expand Up @@ -9,6 +9,7 @@ if (process.env.TEST_WATCH) {
export default defineConfig({
test: {
coverage: {
enabled: true,
all: true,
},
reporters: ['default', 'json'],
Expand Down

0 comments on commit efce3b4

Please sign in to comment.