Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vitest): include coverageMap in json report #6606

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions docs/guide/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export default defineConfig({

### JSON Reporter

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.
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.

:::code-group
```bash [CLI]
Expand Down Expand Up @@ -322,10 +322,15 @@ Example of a JSON report:
"message": "",
"name": "/root-directory/__tests__/test-file-1.test.ts"
}
]
],
"coverageMap": {}
}
```

::: info
Since Vitest 2.2, the JSON reporter includes coverage information in `coverageMap` if coverage is enabled.
:::

### HTML Reporter

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.
Expand Down
10 changes: 6 additions & 4 deletions packages/vitest/src/node/reporters/json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { File, Suite, TaskMeta, TaskState } from '@vitest/runner'
import type { SnapshotSummary } from '@vitest/snapshot'
import type { CoverageMap } from 'istanbul-lib-coverage'
import type { Vitest } from '../core'
import type { Reporter } from '../types/reporter'
import { existsSync, promises as fs } from 'node:fs'
Expand Down Expand Up @@ -63,7 +64,7 @@ export interface JsonTestResults {
success: boolean
testResults: Array<JsonTestResult>
snapshot: SnapshotSummary
// coverageMap?: CoverageMap | null | undefined
coverageMap?: CoverageMap | null | undefined
// numRuntimeErrorTestSuites: number
// wasInterrupted: boolean
}
Expand All @@ -86,7 +87,7 @@ export class JsonReporter implements Reporter {
this.start = Date.now()
}

protected async logTasks(files: File[]) {
protected async logTasks(files: File[], coverageMap?: CoverageMap | null) {
const suites = getSuites(files)
const numTotalTestSuites = suites.length
const tests = getTests(files)
Expand Down Expand Up @@ -188,13 +189,14 @@ export class JsonReporter implements Reporter {
startTime: this.start,
success,
testResults,
coverageMap,
}

await this.writeReport(JSON.stringify(result))
}

async onFinished(files = this.ctx.state.getFiles()) {
await this.logTasks(files)
async onFinished(files = this.ctx.state.getFiles(), _errors: unknown[] = [], coverageMap?: unknown) {
await this.logTasks(files, coverageMap as CoverageMap)
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/workspaces/globalTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export async function teardown() {
assert.equal(results.numTotalTestSuites, 28)
assert.equal(results.numTotalTests, 31)
assert.equal(results.numPassedTests, 31)
assert.ok(results.coverageMap)

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

Expand Down
Loading