Skip to content

Commit 9c8f7e3

Browse files
authoredNov 13, 2024··
feat(vitest): include coverageMap in json report (#6606)
1 parent ff66206 commit 9c8f7e3

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed
 

‎docs/guide/reporters.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ export default defineConfig({
263263

264264
### JSON Reporter
265265

266-
Outputs a report of the test results in JSON format. Can either be printed to the terminal or written to a file using the [`outputFile`](/config/#outputfile) configuration option.
266+
Generates a report of the test results in a JSON format compatible with Jest's `--json` option. Can either be printed to the terminal or written to a file using the [`outputFile`](/config/#outputfile) configuration option.
267267

268268
:::code-group
269269
```bash [CLI]
@@ -322,10 +322,15 @@ Example of a JSON report:
322322
"message": "",
323323
"name": "/root-directory/__tests__/test-file-1.test.ts"
324324
}
325-
]
325+
],
326+
"coverageMap": {}
326327
}
327328
```
328329

330+
::: info
331+
Since Vitest 2.2, the JSON reporter includes coverage information in `coverageMap` if coverage is enabled.
332+
:::
333+
329334
### HTML Reporter
330335

331336
Generates an HTML file to view test results through an interactive [GUI](/guide/ui). After the file has been generated, Vitest will keep a local development server running and provide a link to view the report in a browser.

‎packages/vitest/src/node/reporters/json.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { File, Suite, TaskMeta, TaskState } from '@vitest/runner'
22
import type { SnapshotSummary } from '@vitest/snapshot'
3+
import type { CoverageMap } from 'istanbul-lib-coverage'
34
import type { Vitest } from '../core'
45
import type { Reporter } from '../types/reporter'
56
import { existsSync, promises as fs } from 'node:fs'
@@ -63,7 +64,7 @@ export interface JsonTestResults {
6364
success: boolean
6465
testResults: Array<JsonTestResult>
6566
snapshot: SnapshotSummary
66-
// coverageMap?: CoverageMap | null | undefined
67+
coverageMap?: CoverageMap | null | undefined
6768
// numRuntimeErrorTestSuites: number
6869
// wasInterrupted: boolean
6970
}
@@ -86,7 +87,7 @@ export class JsonReporter implements Reporter {
8687
this.start = Date.now()
8788
}
8889

89-
protected async logTasks(files: File[]) {
90+
protected async logTasks(files: File[], coverageMap?: CoverageMap | null) {
9091
const suites = getSuites(files)
9192
const numTotalTestSuites = suites.length
9293
const tests = getTests(files)
@@ -188,13 +189,14 @@ export class JsonReporter implements Reporter {
188189
startTime: this.start,
189190
success,
190191
testResults,
192+
coverageMap,
191193
}
192194

193195
await this.writeReport(JSON.stringify(result))
194196
}
195197

196-
async onFinished(files = this.ctx.state.getFiles()) {
197-
await this.logTasks(files)
198+
async onFinished(files = this.ctx.state.getFiles(), _errors: unknown[] = [], coverageMap?: unknown) {
199+
await this.logTasks(files, coverageMap as CoverageMap)
198200
}
199201

200202
/**

‎test/workspaces/globalTest.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export async function teardown() {
3737
assert.equal(results.numTotalTestSuites, 28)
3838
assert.equal(results.numTotalTests, 31)
3939
assert.equal(results.numPassedTests, 31)
40+
assert.ok(results.coverageMap)
4041

4142
const shared = results.testResults.filter((r: any) => r.name.includes('space_shared/test.spec.ts'))
4243

0 commit comments

Comments
 (0)
Please sign in to comment.