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 1/2] 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 From 0d0f35f6930c98e034bcf61a53ce21d3b63fdec2 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Mon, 28 Aug 2023 11:32:22 +0200 Subject: [PATCH 2/2] fix(vitest): "vi" doesn't rely on context (#4031) --- packages/vitest/src/integrations/vi.ts | 47 +++++++++++++------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/packages/vitest/src/integrations/vi.ts b/packages/vitest/src/integrations/vi.ts index 5b5114cada71..cfac7728d788 100644 --- a/packages/vitest/src/integrations/vi.ts +++ b/packages/vitest/src/integrations/vi.ts @@ -200,7 +200,7 @@ function createVitest(): VitestUtils { return stack?.file || '' } - return { + const utils: VitestUtils = { useFakeTimers(config?: FakeTimerInstallOpts) { if (config) { _timers.configure(config) @@ -210,58 +210,58 @@ function createVitest(): VitestUtils { _timers.configure(workerState.config.fakeTimers) } _timers.useFakeTimers() - return this + return utils }, useRealTimers() { _timers.useRealTimers() _mockedDate = null - return this + return utils }, runOnlyPendingTimers() { _timers.runOnlyPendingTimers() - return this + return utils }, async runOnlyPendingTimersAsync() { await _timers.runOnlyPendingTimersAsync() - return this + return utils }, runAllTimers() { _timers.runAllTimers() - return this + return utils }, async runAllTimersAsync() { await _timers.runAllTimersAsync() - return this + return utils }, runAllTicks() { _timers.runAllTicks() - return this + return utils }, advanceTimersByTime(ms: number) { _timers.advanceTimersByTime(ms) - return this + return utils }, async advanceTimersByTimeAsync(ms: number) { await _timers.advanceTimersByTimeAsync(ms) - return this + return utils }, advanceTimersToNextTimer() { _timers.advanceTimersToNextTimer() - return this + return utils }, async advanceTimersToNextTimerAsync() { await _timers.advanceTimersToNextTimerAsync() - return this + return utils }, getTimerCount() { @@ -272,7 +272,7 @@ function createVitest(): VitestUtils { const date = time instanceof Date ? time : new Date(time) _mockedDate = date _timers.setSystemTime(date) - return this + return utils }, getMockedSystemTime() { @@ -285,7 +285,7 @@ function createVitest(): VitestUtils { clearAllTimers() { _timers.clearAllTimers() - return this + return utils }, // mocks @@ -337,17 +337,17 @@ function createVitest(): VitestUtils { clearAllMocks() { spies.forEach(spy => spy.mockClear()) - return this + return utils }, resetAllMocks() { spies.forEach(spy => spy.mockReset()) - return this + return utils }, restoreAllMocks() { spies.forEach(spy => spy.mockRestore()) - return this + return utils }, stubGlobal(name: string | symbol | number, value: any) { @@ -359,14 +359,14 @@ function createVitest(): VitestUtils { configurable: true, enumerable: true, }) - return this + return utils }, stubEnv(name: string, value: string) { if (!_stubsEnv.has(name)) _stubsEnv.set(name, process.env[name]) process.env[name] = value - return this + return utils }, unstubAllGlobals() { @@ -377,7 +377,7 @@ function createVitest(): VitestUtils { Object.defineProperty(globalThis, name, original) }) _stubsGlobal.clear() - return this + return utils }, unstubAllEnvs() { @@ -388,13 +388,13 @@ function createVitest(): VitestUtils { process.env[name] = original }) _stubsEnv.clear() - return this + return utils }, resetModules() { const state = getWorkerState() resetModules(state.moduleCache) - return this + return utils }, async dynamicImportSettled() { @@ -414,8 +414,9 @@ function createVitest(): VitestUtils { Object.assign(state.config, _config) } }, - } + + return utils } export const vitest = createVitest()