Skip to content

Commit

Permalink
feat(coverage-istanbul): add "all" option
Browse files Browse the repository at this point in the history
- Similar as nyc and c8 has
  • Loading branch information
AriPerkkio committed Sep 3, 2022
1 parent 03f7450 commit d85d878
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
7 changes: 7 additions & 0 deletions docs/config/index.md
Expand Up @@ -532,6 +532,13 @@ Set to array of class method names to ignore for coverage.

Watermarks for statements, lines, branches and functions.

##### all

- **Type:** `boolean`
- **Default:** false

Whether to include all files, including the untested ones into report.

### testNamePattern

- **Type** `string | RegExp`
Expand Down
43 changes: 41 additions & 2 deletions packages/coverage-istanbul/src/provider.ts
Expand Up @@ -23,7 +23,10 @@ interface TestExclude {
exclude?: string | string[]
extension?: string | string[]
excludeNodeModules?: boolean
}): { shouldInstrument(filePath: string): boolean }
}): {
shouldInstrument(filePath: string): boolean
glob(cwd: string): Promise<string[]>
}
}

export class IstanbulCoverageProvider implements CoverageProvider {
Expand Down Expand Up @@ -95,12 +98,15 @@ export class IstanbulCoverageProvider implements CoverageProvider {
}

async reportCoverage() {
const mergedCoverage = this.coverages.reduce((coverage, previousCoverageMap) => {
const mergedCoverage: CoverageMap = this.coverages.reduce((coverage, previousCoverageMap) => {
const map = libCoverage.createCoverageMap(coverage)
map.merge(previousCoverageMap)
return map
}, {})

if (this.options.all)
await this.includeUntestedFiles(mergedCoverage)

const sourceMapStore = libSourceMaps.createSourceMapStore()
const coverageMap: CoverageMap = await sourceMapStore.transformCoverage(mergedCoverage)

Expand Down Expand Up @@ -175,6 +181,39 @@ export class IstanbulCoverageProvider implements CoverageProvider {
}
}
}

async includeUntestedFiles(coverageMap: CoverageMap) {
// Load, instrument and collect empty coverages from all files which
// are not already in the coverage map
const includedFiles = await this.testExclude.glob(this.ctx.config.root)
const uncoveredFiles = includedFiles
.map(file => resolve(this.ctx.config.root, file))
.filter(file => !coverageMap.data[file])

const transformResults = await Promise.all(uncoveredFiles.map(async (filename) => {
const transformResult = await this.ctx.vitenode.transformRequest(filename)
return { transformResult, filename }
}))

for (const { transformResult, filename } of transformResults) {
const sourceMap = transformResult?.map

if (sourceMap) {
this.instrumenter.instrumentSync(
transformResult.code,
filename,
{
...sourceMap,
version: sourceMap.version.toString(),
},
)

const lastCoverage = this.instrumenter.lastFileCoverage()
if (lastCoverage)
coverageMap.data[lastCoverage.path] = lastCoverage
}
}
}
}

function resolveIstanbulOptions(options: CoverageIstanbulOptions, root: string) {
Expand Down
6 changes: 5 additions & 1 deletion packages/vitest/src/types/coverage.ts
Expand Up @@ -137,6 +137,11 @@ export interface BaseCoverageOptions {
* Extensions for files to be included in coverage
*/
extension?: string | string[]

/**
* Whether to include all files, including the untested ones into report
*/
all?: boolean
}

export interface CoverageIstanbulOptions extends BaseCoverageOptions {
Expand Down Expand Up @@ -166,7 +171,6 @@ export interface CoverageC8Options extends BaseCoverageOptions {
*/
excludeNodeModules?: boolean

all?: boolean
src?: string[]

100?: boolean
Expand Down
9 changes: 8 additions & 1 deletion test/coverage-test/coverage-test/coverage.istanbul.test.ts
Expand Up @@ -3,7 +3,7 @@ import { resolve } from 'pathe'
import { expect, test } from 'vitest'

test('istanbul html report', async () => {
const coveragePath = resolve('./coverage')
const coveragePath = resolve('./coverage/coverage-test/src')
const files = fs.readdirSync(coveragePath)

expect(files).toContain('index.html')
Expand All @@ -22,3 +22,10 @@ test('istanbul lcov report', async () => {

expect(lcovReportFiles).toContain('index.html')
})

test('all includes untested files', () => {
const coveragePath = resolve('./coverage/coverage-test/src')
const files = fs.readdirSync(coveragePath)

expect(files).toContain('untested-file.ts.html')
})
3 changes: 3 additions & 0 deletions test/coverage-test/src/untested-file.ts
@@ -0,0 +1,3 @@
export default function untestedFile() {
return 'This file should end up in report when {"all": true} is given'
}
1 change: 1 addition & 0 deletions test/coverage-test/vitest.config.ts
Expand Up @@ -19,6 +19,7 @@ export default defineConfig({
coverage: {
enabled: true,
clean: true,
all: true,
reporter: ['html', 'text', 'lcov'],
},
},
Expand Down

0 comments on commit d85d878

Please sign in to comment.