From 9f04386b7f8ccacf316ae420bc4589993631c08a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Mon, 28 Aug 2023 10:30:48 +0300 Subject: [PATCH] fix(coverage): log info only when terminal reporter is used (#4027) --- packages/coverage-istanbul/package.json | 1 + packages/coverage-istanbul/src/provider.ts | 12 ++++++++++++ packages/coverage-v8/src/provider.ts | 11 +++++++++++ packages/vitest/src/node/core.ts | 4 +--- pnpm-lock.yaml | 3 +++ 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/coverage-istanbul/package.json b/packages/coverage-istanbul/package.json index b29e00c8f6fb..40fb02c6043c 100644 --- a/packages/coverage-istanbul/package.json +++ b/packages/coverage-istanbul/package.json @@ -50,6 +50,7 @@ "istanbul-lib-report": "^3.0.1", "istanbul-lib-source-maps": "^4.0.1", "istanbul-reports": "^3.1.5", + "picocolors": "^1.0.0", "test-exclude": "^6.0.0" }, "devDependencies": { diff --git a/packages/coverage-istanbul/src/provider.ts b/packages/coverage-istanbul/src/provider.ts index 89cc3752a9c2..0ec38581e7ec 100644 --- a/packages/coverage-istanbul/src/provider.ts +++ b/packages/coverage-istanbul/src/provider.ts @@ -3,6 +3,7 @@ import { resolve } from 'pathe' import type { AfterSuiteRunMeta, CoverageIstanbulOptions, CoverageProvider, ReportContext, ResolvedCoverageOptions, Vitest } from 'vitest' import { coverageConfigDefaults, defaultExclude, defaultInclude } from 'vitest/config' import { BaseCoverageProvider } from 'vitest/coverage' +import c from 'picocolors' import libReport from 'istanbul-lib-report' import reports from 'istanbul-reports' import type { CoverageMap } from 'istanbul-lib-coverage' @@ -134,6 +135,9 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co watermarks: this.options.watermarks, }) + if (hasTerminalReporter(this.options.reporter)) + this.ctx.logger.log(c.blue(' % ') + c.dim('Coverage report from ') + c.yellow(this.name)) + for (const reporter of this.options.reporter) { reports.create(reporter[0], { skipFull: this.options.skipFull, @@ -253,3 +257,11 @@ function isEmptyCoverageRange(range: libCoverage.Range) { || range.end.column === undefined ) } + +function hasTerminalReporter(reporters: Options['reporter']) { + return reporters.some(([reporter]) => + reporter === 'text' + || reporter === 'text-summary' + || reporter === 'text-lcov' + || reporter === 'teamcity') +} diff --git a/packages/coverage-v8/src/provider.ts b/packages/coverage-v8/src/provider.ts index 7b3c28fa3095..56853b15f95d 100644 --- a/packages/coverage-v8/src/provider.ts +++ b/packages/coverage-v8/src/provider.ts @@ -143,6 +143,9 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage watermarks: this.options.watermarks, }) + if (hasTerminalReporter(this.options.reporter)) + this.ctx.logger.log(c.blue(' % ') + c.dim('Coverage report from ') + c.yellow(this.name)) + for (const reporter of this.options.reporter) { reports.create(reporter[0], { skipFull: this.options.skipFull, @@ -295,3 +298,11 @@ function normalizeTransformResults(fetchCaches: Map + reporter === 'text' + || reporter === 'text-summary' + || reporter === 'text-lcov' + || reporter === 'teamcity') +} diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index c61ae7032709..f7987840c033 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -730,10 +730,8 @@ export class Vitest { 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)) + if (this.coverageProvider) await this.coverageProvider.reportCoverage({ allTestsRun }) - } } async close() { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1d221ddaa436..784c1879f653 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -983,6 +983,9 @@ importers: istanbul-reports: specifier: ^3.1.5 version: 3.1.5 + picocolors: + specifier: ^1.0.0 + version: 1.0.0 test-exclude: specifier: ^6.0.0 version: 6.0.0