From e662c7b2c307adbf312684c21efd8d05b1284b9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Mon, 12 Aug 2024 14:02:12 +0300 Subject: [PATCH] fix(coverage): warn if `vitest` and `@vitest/*` versions don't match (#6317) --- packages/coverage-istanbul/src/provider.ts | 15 +++++- packages/coverage-v8/src/provider.ts | 13 ++++- pnpm-lock.yaml | 3 ++ test/coverage-test/package.json | 4 +- .../test/mixed-versions-warning.unit.test.ts | 50 +++++++++++++++++++ 5 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 test/coverage-test/test/mixed-versions-warning.unit.test.ts diff --git a/packages/coverage-istanbul/src/provider.ts b/packages/coverage-istanbul/src/provider.ts index 8597ef200c56..19fd5dc7a47d 100644 --- a/packages/coverage-istanbul/src/provider.ts +++ b/packages/coverage-istanbul/src/provider.ts @@ -26,11 +26,12 @@ import type { CoverageMap } from 'istanbul-lib-coverage' import libCoverage from 'istanbul-lib-coverage' import libSourceMaps from 'istanbul-lib-source-maps' import { type Instrumenter, createInstrumenter } from 'istanbul-lib-instrument' -// @ts-expect-error @istanbuljs/schema has no type definitions +// @ts-expect-error missing types import { defaults as istanbulDefaults } from '@istanbuljs/schema' - // @ts-expect-error missing types import _TestExclude from 'test-exclude' + +import { version } from '../package.json' with { type: 'json' } import { COVERAGE_STORE_KEY } from './constants' type Options = ResolvedCoverageOptions<'istanbul'> @@ -76,6 +77,16 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co initialize(ctx: Vitest): void { const config: CoverageIstanbulOptions = ctx.config.coverage + if (ctx.version !== version) { + ctx.logger.warn( + c.yellow( + `Loaded ${c.inverse(c.yellow(` vitest@${ctx.version} `))} and ${c.inverse(c.yellow(` @vitest/coverage-istanbul@${version} `))}.` + + '\nRunning mixed versions is not supported and may lead into bugs' + + '\nUpdate your dependencies and make sure the versions match.', + ), + ) + } + this.ctx = ctx this.options = { ...coverageConfigDefaults, diff --git a/packages/coverage-v8/src/provider.ts b/packages/coverage-v8/src/provider.ts index ca038ed47902..72116e4d577a 100644 --- a/packages/coverage-v8/src/provider.ts +++ b/packages/coverage-v8/src/provider.ts @@ -34,10 +34,11 @@ import type { ResolvedCoverageOptions, } from 'vitest' import type { Vitest } from 'vitest/node' - // @ts-expect-error missing types import _TestExclude from 'test-exclude' +import { version } from '../package.json' with { type: 'json' } + interface TestExclude { new (opts: { cwd?: string | string[] @@ -91,6 +92,16 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage initialize(ctx: Vitest): void { const config: CoverageV8Options = ctx.config.coverage + if (ctx.version !== version) { + ctx.logger.warn( + c.yellow( + `Loaded ${c.inverse(c.yellow(` vitest@${ctx.version} `))} and ${c.inverse(c.yellow(` @vitest/coverage-v8@${version} `))}.` + + '\nRunning mixed versions is not supported and may lead into bugs' + + '\nUpdate your dependencies and make sure the versions match.', + ), + ) + } + this.ctx = ctx this.options = { ...coverageConfigDefaults, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 78c6cce8ea9b..43d9cd63644a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1276,6 +1276,9 @@ importers: magicast: specifier: ^0.3.3 version: 0.3.3 + strip-ansi: + specifier: ^7.1.0 + version: 7.1.0 unplugin-swc: specifier: ^1.4.4 version: 1.4.4(@swc/core@1.4.1)(rollup@4.20.0) diff --git a/test/coverage-test/package.json b/test/coverage-test/package.json index 15e5e659f693..4c6a2e365c6c 100644 --- a/test/coverage-test/package.json +++ b/test/coverage-test/package.json @@ -3,7 +3,8 @@ "type": "module", "private": true, "scripts": { - "test": "vitest --workspace=vitest.workspace.custom.ts" + "test": "vitest --workspace=vitest.workspace.custom.ts", + "vitest": "vitest" }, "devDependencies": { "@ampproject/remapping": "^2.2.1", @@ -19,6 +20,7 @@ "istanbul-lib-report": "^3.0.1", "magic-string": "^0.30.10", "magicast": "^0.3.3", + "strip-ansi": "^7.1.0", "unplugin-swc": "^1.4.4", "vite": "latest", "vitest": "workspace:*", diff --git a/test/coverage-test/test/mixed-versions-warning.unit.test.ts b/test/coverage-test/test/mixed-versions-warning.unit.test.ts new file mode 100644 index 000000000000..ea450763cace --- /dev/null +++ b/test/coverage-test/test/mixed-versions-warning.unit.test.ts @@ -0,0 +1,50 @@ +import { expect, test, vi } from 'vitest' +import { configDefaults } from 'vitest/config' +import V8Provider from '@vitest/coverage-v8' +import packageJson from '@vitest/coverage-v8/package.json' +import IstanbulProvider from '@vitest/coverage-istanbul' +import stripAnsi from 'strip-ansi' + +const version = packageJson.version + +test('v8 provider logs warning if versions do not match', async () => { + const provider = await V8Provider.getProvider() + const warn = vi.fn() + + provider.initialize({ + version: '1.0.0', + logger: { warn }, + config: configDefaults, + } as any) + + expect(warn).toHaveBeenCalled() + + const message = warn.mock.calls[0][0] + + expect(stripAnsi(message)).toMatchInlineSnapshot(` + "Loaded vitest@1.0.0 and @vitest/coverage-v8@${version} . + Running mixed versions is not supported and may lead into bugs + Update your dependencies and make sure the versions match." + `) +}) + +test('istanbul provider logs warning if versions do not match', async () => { + const provider = await IstanbulProvider.getProvider() + const warn = vi.fn() + + provider.initialize({ + version: '1.0.0', + logger: { warn }, + config: configDefaults, + } as any) + + expect(warn).toHaveBeenCalled() + + const message = warn.mock.calls[0][0] + + expect(stripAnsi(message)).toMatchInlineSnapshot(` + "Loaded vitest@1.0.0 and @vitest/coverage-istanbul@${version} . + Running mixed versions is not supported and may lead into bugs + Update your dependencies and make sure the versions match." + `) +})