Skip to content

Commit e662c7b

Browse files
authoredAug 12, 2024··
fix(coverage): warn if vitest and @vitest/* versions don't match (#6317)
1 parent 53fafef commit e662c7b

File tree

5 files changed

+81
-4
lines changed

5 files changed

+81
-4
lines changed
 

‎packages/coverage-istanbul/src/provider.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ import type { CoverageMap } from 'istanbul-lib-coverage'
2626
import libCoverage from 'istanbul-lib-coverage'
2727
import libSourceMaps from 'istanbul-lib-source-maps'
2828
import { type Instrumenter, createInstrumenter } from 'istanbul-lib-instrument'
29-
// @ts-expect-error @istanbuljs/schema has no type definitions
29+
// @ts-expect-error missing types
3030
import { defaults as istanbulDefaults } from '@istanbuljs/schema'
31-
3231
// @ts-expect-error missing types
3332
import _TestExclude from 'test-exclude'
33+
34+
import { version } from '../package.json' with { type: 'json' }
3435
import { COVERAGE_STORE_KEY } from './constants'
3536

3637
type Options = ResolvedCoverageOptions<'istanbul'>
@@ -76,6 +77,16 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co
7677
initialize(ctx: Vitest): void {
7778
const config: CoverageIstanbulOptions = ctx.config.coverage
7879

80+
if (ctx.version !== version) {
81+
ctx.logger.warn(
82+
c.yellow(
83+
`Loaded ${c.inverse(c.yellow(` vitest@${ctx.version} `))} and ${c.inverse(c.yellow(` @vitest/coverage-istanbul@${version} `))}.`
84+
+ '\nRunning mixed versions is not supported and may lead into bugs'
85+
+ '\nUpdate your dependencies and make sure the versions match.',
86+
),
87+
)
88+
}
89+
7990
this.ctx = ctx
8091
this.options = {
8192
...coverageConfigDefaults,

‎packages/coverage-v8/src/provider.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ import type {
3434
ResolvedCoverageOptions,
3535
} from 'vitest'
3636
import type { Vitest } from 'vitest/node'
37-
3837
// @ts-expect-error missing types
3938
import _TestExclude from 'test-exclude'
4039

40+
import { version } from '../package.json' with { type: 'json' }
41+
4142
interface TestExclude {
4243
new (opts: {
4344
cwd?: string | string[]
@@ -91,6 +92,16 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
9192
initialize(ctx: Vitest): void {
9293
const config: CoverageV8Options = ctx.config.coverage
9394

95+
if (ctx.version !== version) {
96+
ctx.logger.warn(
97+
c.yellow(
98+
`Loaded ${c.inverse(c.yellow(` vitest@${ctx.version} `))} and ${c.inverse(c.yellow(` @vitest/coverage-v8@${version} `))}.`
99+
+ '\nRunning mixed versions is not supported and may lead into bugs'
100+
+ '\nUpdate your dependencies and make sure the versions match.',
101+
),
102+
)
103+
}
104+
94105
this.ctx = ctx
95106
this.options = {
96107
...coverageConfigDefaults,

‎pnpm-lock.yaml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/coverage-test/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"type": "module",
44
"private": true,
55
"scripts": {
6-
"test": "vitest --workspace=vitest.workspace.custom.ts"
6+
"test": "vitest --workspace=vitest.workspace.custom.ts",
7+
"vitest": "vitest"
78
},
89
"devDependencies": {
910
"@ampproject/remapping": "^2.2.1",
@@ -19,6 +20,7 @@
1920
"istanbul-lib-report": "^3.0.1",
2021
"magic-string": "^0.30.10",
2122
"magicast": "^0.3.3",
23+
"strip-ansi": "^7.1.0",
2224
"unplugin-swc": "^1.4.4",
2325
"vite": "latest",
2426
"vitest": "workspace:*",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { expect, test, vi } from 'vitest'
2+
import { configDefaults } from 'vitest/config'
3+
import V8Provider from '@vitest/coverage-v8'
4+
import packageJson from '@vitest/coverage-v8/package.json'
5+
import IstanbulProvider from '@vitest/coverage-istanbul'
6+
import stripAnsi from 'strip-ansi'
7+
8+
const version = packageJson.version
9+
10+
test('v8 provider logs warning if versions do not match', async () => {
11+
const provider = await V8Provider.getProvider()
12+
const warn = vi.fn()
13+
14+
provider.initialize({
15+
version: '1.0.0',
16+
logger: { warn },
17+
config: configDefaults,
18+
} as any)
19+
20+
expect(warn).toHaveBeenCalled()
21+
22+
const message = warn.mock.calls[0][0]
23+
24+
expect(stripAnsi(message)).toMatchInlineSnapshot(`
25+
"Loaded vitest@1.0.0 and @vitest/coverage-v8@${version} .
26+
Running mixed versions is not supported and may lead into bugs
27+
Update your dependencies and make sure the versions match."
28+
`)
29+
})
30+
31+
test('istanbul provider logs warning if versions do not match', async () => {
32+
const provider = await IstanbulProvider.getProvider()
33+
const warn = vi.fn()
34+
35+
provider.initialize({
36+
version: '1.0.0',
37+
logger: { warn },
38+
config: configDefaults,
39+
} as any)
40+
41+
expect(warn).toHaveBeenCalled()
42+
43+
const message = warn.mock.calls[0][0]
44+
45+
expect(stripAnsi(message)).toMatchInlineSnapshot(`
46+
"Loaded vitest@1.0.0 and @vitest/coverage-istanbul@${version} .
47+
Running mixed versions is not supported and may lead into bugs
48+
Update your dependencies and make sure the versions match."
49+
`)
50+
})

0 commit comments

Comments
 (0)
Please sign in to comment.