Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(coverage): warn if vitest and @vitest/* versions don't match #6317

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/coverage-istanbul/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
AriPerkkio marked this conversation as resolved.
Show resolved Hide resolved
import { COVERAGE_STORE_KEY } from './constants'

type Options = ResolvedCoverageOptions<'istanbul'>
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 12 additions & 1 deletion packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion test/coverage-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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:*",
Expand Down
50 changes: 50 additions & 0 deletions test/coverage-test/test/mixed-versions-warning.unit.test.ts
Original file line number Diff line number Diff line change
@@ -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."
`)
})