Skip to content

Commit

Permalink
fix(coverage): warn if vitest and coverage versions don't match
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Aug 10, 2024
1 parent 6e06278 commit 146a021
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
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' }
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."
`)
})

0 comments on commit 146a021

Please sign in to comment.