From 9c8f7e3e687a775dbe998bfdac5cb33baf01f086 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Wed, 13 Nov 2024 17:01:17 +0100 Subject: [PATCH] feat(vitest): include `coverageMap` in json report (#6606) --- docs/guide/reporters.md | 9 +++++++-- packages/vitest/src/node/reporters/json.ts | 10 ++++++---- test/workspaces/globalTest.ts | 1 + 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/docs/guide/reporters.md b/docs/guide/reporters.md index 03050696eaff..4d3785fd1e73 100644 --- a/docs/guide/reporters.md +++ b/docs/guide/reporters.md @@ -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] @@ -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. diff --git a/packages/vitest/src/node/reporters/json.ts b/packages/vitest/src/node/reporters/json.ts index 4b292344cb75..1b46c62bd2ab 100644 --- a/packages/vitest/src/node/reporters/json.ts +++ b/packages/vitest/src/node/reporters/json.ts @@ -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' @@ -63,7 +64,7 @@ export interface JsonTestResults { success: boolean testResults: Array snapshot: SnapshotSummary - // coverageMap?: CoverageMap | null | undefined + coverageMap?: CoverageMap | null | undefined // numRuntimeErrorTestSuites: number // wasInterrupted: boolean } @@ -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) @@ -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) } /** diff --git a/test/workspaces/globalTest.ts b/test/workspaces/globalTest.ts index d642baa0c546..0069891a748d 100644 --- a/test/workspaces/globalTest.ts +++ b/test/workspaces/globalTest.ts @@ -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'))