Skip to content

Commit

Permalink
feat(coverage): support --changed option
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Mar 3, 2024
1 parent dfe16e1 commit ddf24af
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 25 deletions.
14 changes: 10 additions & 4 deletions packages/coverage-istanbul/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,17 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co
}

async getCoverageMapForUncoveredFiles(coveredFiles: string[]) {
// 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)
let includedFiles: string[]

if (this.ctx.config.changed) {
includedFiles = this.ctx.config.related || []
}
else {
const allFiles = await this.testExclude.glob(this.ctx.config.root)
includedFiles = allFiles.map(file => resolve(this.ctx.config.root, file))
}

const uncoveredFiles = includedFiles
.map(file => resolve(this.ctx.config.root, file))
.filter(file => !coveredFiles.includes(file))

const cacheKey = new Date().getTime()
Expand Down
12 changes: 10 additions & 2 deletions packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,18 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage

private async getUntestedFiles(testedFiles: string[]): Promise<RawCoverage> {
const transformResults = normalizeTransformResults(this.ctx.vitenode.fetchCache)
let includedFiles: string[]

if (this.ctx.config.changed) {
includedFiles = this.ctx.config.related || []
}
else {
const allFiles = await this.testExclude.glob(this.ctx.config.root)
includedFiles = allFiles.map(file => resolve(this.ctx.config.root, file))
}

const includedFiles = await this.testExclude.glob(this.ctx.config.root)
const uncoveredFiles = includedFiles
.map(file => pathToFileURL(resolve(this.ctx.config.root, file)))
.map(file => pathToFileURL(file))
.filter(file => !testedFiles.includes(file.pathname))

let merged: RawCoverage = { result: [] }
Expand Down
8 changes: 4 additions & 4 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,6 @@ export class Vitest {
}
}

// all subsequent runs will treat this as a fresh run
this.config.changed = false
this.config.related = undefined

if (files.length) {
// populate once, update cache on watch
await this.cache.stats.populateStats(this.config.root, files)
Expand Down Expand Up @@ -532,6 +528,10 @@ export class Vitest {

this.runningPromise = undefined
this.isFirstRun = false

// all subsequent runs will treat this as a fresh run
this.config.changed = false
this.config.related = undefined
})

return await this.runningPromise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,89 @@ exports[`istanbul json report 1`] = `
},
},
},
"<process-cwd>/src/file-to-change.ts": {
"b": {},
"branchMap": {},
"f": {
"0": 0,
"1": 0,
},
"fnMap": {
"0": {
"decl": {
"end": {
"column": 22,
"line": 1,
},
"start": {
"column": 16,
"line": 1,
},
},
"loc": {
"end": {
"column": null,
"line": 3,
},
"start": {
"column": 22,
"line": 1,
},
},
"name": "run",
},
"1": {
"decl": {
"end": {
"column": 36,
"line": 5,
},
"start": {
"column": 16,
"line": 5,
},
},
"loc": {
"end": {
"column": null,
"line": 7,
},
"start": {
"column": 36,
"line": 5,
},
},
"name": "uncoveredFunction",
},
},
"path": "<process-cwd>/src/file-to-change.ts",
"s": {
"0": 0,
"1": 0,
},
"statementMap": {
"0": {
"end": {
"column": null,
"line": 2,
},
"start": {
"column": 2,
"line": 2,
},
},
"1": {
"end": {
"column": null,
"line": 6,
},
"start": {
"column": 2,
"line": 6,
},
},
},
},
"<process-cwd>/src/function-count.ts": {
"b": {},
"branchMap": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2777,6 +2777,153 @@ exports[`v8 json report 1`] = `
},
},
},
"<process-cwd>/src/file-to-change.ts": {
"all": true,
"b": {
"0": [
0,
],
},
"branchMap": {
"0": {
"line": 1,
"loc": {
"end": {
"column": 1,
"line": 7,
},
"start": {
"column": 0,
"line": 1,
},
},
"locations": [
{
"end": {
"column": 1,
"line": 7,
},
"start": {
"column": 0,
"line": 1,
},
},
],
"type": "branch",
},
},
"f": {
"0": 0,
},
"fnMap": {
"0": {
"decl": {
"end": {
"column": 1,
"line": 7,
},
"start": {
"column": 0,
"line": 1,
},
},
"line": 1,
"loc": {
"end": {
"column": 1,
"line": 7,
},
"start": {
"column": 0,
"line": 1,
},
},
"name": "(empty-report)",
},
},
"path": "<process-cwd>/src/file-to-change.ts",
"s": {
"0": 0,
"1": 0,
"2": 0,
"3": 0,
"4": 0,
"5": 0,
"6": 0,
},
"statementMap": {
"0": {
"end": {
"column": 23,
"line": 1,
},
"start": {
"column": 0,
"line": 1,
},
},
"1": {
"end": {
"column": 51,
"line": 2,
},
"start": {
"column": 0,
"line": 2,
},
},
"2": {
"end": {
"column": 1,
"line": 3,
},
"start": {
"column": 0,
"line": 3,
},
},
"3": {
"end": {
"column": 0,
"line": 4,
},
"start": {
"column": 0,
"line": 4,
},
},
"4": {
"end": {
"column": 37,
"line": 5,
},
"start": {
"column": 0,
"line": 5,
},
},
"5": {
"end": {
"column": 14,
"line": 6,
},
"start": {
"column": 0,
"line": 6,
},
},
"6": {
"end": {
"column": 1,
"line": 7,
},
"start": {
"column": 0,
"line": 7,
},
},
},
},
"<process-cwd>/src/function-count.ts": {
"all": false,
"b": {
Expand Down
22 changes: 22 additions & 0 deletions test/coverage-test/coverage-report-tests/changed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { expect, test } from 'vitest'
import libCoverage from 'istanbul-lib-coverage'

import { readCoverageJson } from './utils'

test('report contains only the changed files', async () => {
const coverageJson = await readCoverageJson('./coverage/coverage-final.json')
const coverageMap = libCoverage.createCoverageMap(coverageJson as any)

expect(coverageMap.files()).toMatchInlineSnapshot(`
[
"<process-cwd>/src/file-to-change.ts",
"<process-cwd>/src/new-uncovered-file.ts",
]
`)

const uncoveredFile = coverageMap.fileCoverageFor('<process-cwd>/src/new-uncovered-file.ts').toSummary()
expect(uncoveredFile.lines.pct).toBe(0)

const changedFile = coverageMap.fileCoverageFor('<process-cwd>/src/file-to-change.ts').toSummary()
expect(changedFile.lines.pct).toBeGreaterThanOrEqual(50)
})
4 changes: 2 additions & 2 deletions test/coverage-test/coverage-report-tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ interface CoverageFinalJson {
* Read JSON coverage report from file system.
* Normalizes paths to keep contents consistent between OS's
*/
export async function readCoverageJson() {
const jsonReport = JSON.parse(readFileSync('./coverage/custom-json-report-name.json', 'utf8')) as CoverageFinalJson
export async function readCoverageJson(name = './coverage/custom-json-report-name.json') {
const jsonReport = JSON.parse(readFileSync(name, 'utf8')) as CoverageFinalJson

const normalizedReport: CoverageFinalJson['default'] = {}

Expand Down
6 changes: 6 additions & 0 deletions test/coverage-test/option-tests/changed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { test } from 'vitest'
import { run } from '../src/file-to-change'

test('test case for changed file', () => {
run()
})
7 changes: 7 additions & 0 deletions test/coverage-test/src/file-to-change.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function run() {
return 'This file will be modified by test cases'
}

export function uncoveredFunction() {
return 1 + 2
}
Loading

0 comments on commit ddf24af

Please sign in to comment.