-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(coverage): add support for coverage reporter options
- Loading branch information
1 parent
aa88829
commit d47b2a1
Showing
10 changed files
with
140 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import { extname, resolve } from 'pathe' | |
import type { RawSourceMap } from 'vite-node' | ||
import { coverageConfigDefaults } from 'vitest/config' | ||
// eslint-disable-next-line no-restricted-imports | ||
import type { CoverageC8Options, CoverageProvider, ReportContext, ResolvedCoverageOptions } from 'vitest' | ||
import type { CoverageC8Options, CoverageOptions, CoverageProvider, ReportContext, ResolvedCoverageOptions } from 'vitest' | ||
import type { Vitest } from 'vitest/node' | ||
import type { Report } from 'c8' | ||
// @ts-expect-error missing types | ||
|
@@ -55,6 +55,18 @@ export class C8CoverageProvider implements CoverageProvider { | |
const options: ConstructorParameters<typeof Report>[0] = { | ||
...this.options, | ||
all: this.options.all && allTestsRun, | ||
reporter: this.options.reporter.map(([reporterName]) => reporterName), | ||
|
||
// TODO: This requires unreleased [email protected] | ||
// @ts-expect-error -- untyped | ||
reporterOptions: this.options.reporter.reduce((all, [name, options]) => ({ | ||
...all, | ||
[name]: { | ||
skipFull: this.options.skipFull, | ||
projectRoot: this.ctx.config.root, | ||
...options, | ||
}, | ||
}), {}), | ||
} | ||
|
||
const report = createReport(options) | ||
|
@@ -159,7 +171,6 @@ export class C8CoverageProvider implements CoverageProvider { | |
|
||
function resolveC8Options(options: CoverageC8Options, root: string): Options { | ||
const reportsDirectory = resolve(root, options.reportsDirectory || coverageConfigDefaults.reportsDirectory) | ||
const reporter = options.reporter || coverageConfigDefaults.reporter | ||
|
||
const resolved: Options = { | ||
...coverageConfigDefaults, | ||
|
@@ -174,7 +185,7 @@ function resolveC8Options(options: CoverageC8Options, root: string): Options { | |
// Resolved fields | ||
provider: 'c8', | ||
tempDirectory: process.env.NODE_V8_COVERAGE || resolve(reportsDirectory, 'tmp'), | ||
reporter: Array.isArray(reporter) ? reporter : [reporter], | ||
reporter: resolveReporters(options.reporter || coverageConfigDefaults.reporter), | ||
reportsDirectory, | ||
} | ||
|
||
|
@@ -187,3 +198,25 @@ function resolveC8Options(options: CoverageC8Options, root: string): Options { | |
|
||
return resolved | ||
} | ||
|
||
// TODO: Move to some generic utility for sharing with istanbul-provider | ||
function resolveReporters(configReporters: NonNullable<CoverageOptions['reporter']>): Options['reporter'] { | ||
// E.g. { reporter: "html" } | ||
if (!Array.isArray(configReporters)) | ||
return [[configReporters, {}]] | ||
|
||
const resolvedReporters: Options['reporter'] = [] | ||
|
||
for (const reporter of configReporters) { | ||
if (Array.isArray(reporter)) { | ||
// E.g. { reporter: [ ["html", { skipEmpty: true }], ["lcov"], ["json", { file: "map.json" }] ]} | ||
resolvedReporters.push([reporter[0], reporter[1] || {}]) | ||
} | ||
else { | ||
// E.g. { reporter: ["html", "json"]} | ||
resolvedReporters.push([reporter, {}]) | ||
} | ||
} | ||
|
||
return resolvedReporters | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters