From f9c29d138f69bb9e11ffc410e1f8e04b820146aa Mon Sep 17 00:00:00 2001 From: AriPerkkio Date: Fri, 26 May 2023 16:07:49 +0300 Subject: [PATCH] feat(coverage): add `reportOnFailure` option --- docs/config/index.md | 9 +++++++++ packages/vitest/src/defaults.ts | 1 + packages/vitest/src/node/core.ts | 3 +++ packages/vitest/src/types/coverage.ts | 8 ++++++++ test/coverage-test/test/configuration-options.test-d.ts | 1 + 5 files changed, 22 insertions(+) diff --git a/docs/config/index.md b/docs/config/index.md index 37ba61e626e6..672d34395b67 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -778,6 +778,15 @@ The reporter has three different types: Since Vitest 0.31.0, you can check your coverage report in Vitest UI: check [Vitest UI Coverage](/guide/coverage#vitest-ui) for more details. +#### coverage.reportOnFailure + +- **Type:** `boolean` +- **Default:** `true` +- **Available for providers:** `'c8' | 'istanbul'` +- **CLI:** `--coverage.reportOnFailure`, `--coverage.reportOnFailure=false` + +Generate coverage report even when tests fail. + #### coverage.skipFull - **Type:** `boolean` diff --git a/packages/vitest/src/defaults.ts b/packages/vitest/src/defaults.ts index f06e14bf4c65..0190f27ffb89 100644 --- a/packages/vitest/src/defaults.ts +++ b/packages/vitest/src/defaults.ts @@ -33,6 +33,7 @@ export const coverageConfigDefaults: ResolvedCoverageOptions = { cleanOnRerun: true, reportsDirectory: './coverage', exclude: defaultCoverageExcludes, + reportOnFailure: true, reporter: [['text', {}], ['html', {}], ['clover', {}], ['json', {}]], // default extensions used by c8, plus '.vue' and '.svelte' // see https://github.com/istanbuljs/schema/blob/master/default-extension.js diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index bdbe27f6bf0d..3cd67b33687c 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -692,6 +692,9 @@ export class Vitest { } private async reportCoverage(allTestsRun: boolean) { + if (!this.config.coverage.reportOnFailure && this.state.getCountOfFailedTests() > 0) + return + if (this.coverageProvider) { this.logger.log(c.blue(' % ') + c.dim('Coverage report from ') + c.yellow(this.coverageProvider.name)) await this.coverageProvider.reportCoverage({ allTestsRun }) diff --git a/packages/vitest/src/types/coverage.ts b/packages/vitest/src/types/coverage.ts index fe2d62562d18..65cf0d9e800b 100644 --- a/packages/vitest/src/types/coverage.ts +++ b/packages/vitest/src/types/coverage.ts @@ -77,6 +77,7 @@ type FieldsWithDefaultValues = | 'reportsDirectory' | 'exclude' | 'extension' + | 'reportOnFailure' export type ResolvedCoverageOptions = & CoverageOptions @@ -208,6 +209,13 @@ export interface BaseCoverageOptions { * @default false */ thresholdAutoUpdate?: boolean + + /** + * Generate coverage report even when tests fail. + * + * @default true + */ + reportOnFailure?: boolean } export interface CoverageIstanbulOptions extends BaseCoverageOptions { diff --git a/test/coverage-test/test/configuration-options.test-d.ts b/test/coverage-test/test/configuration-options.test-d.ts index 8b9b0de67c99..d9f17e107fa6 100644 --- a/test/coverage-test/test/configuration-options.test-d.ts +++ b/test/coverage-test/test/configuration-options.test-d.ts @@ -105,6 +105,7 @@ test('provider module', () => { extension: ['string'], reporter: [['html', {}], ['json', { file: 'string' }]], reportsDirectory: 'string', + reportOnFailure: true, } }, clean(_: boolean) {},