Skip to content

Commit 708b10f

Browse files
authoredMay 4, 2023
fix: throw an error, if tests are collected with a different vitest version (#3301)
1 parent 93fbd02 commit 708b10f

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed
 

‎packages/runner/rollup.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { builtinModules } from 'node:module'
22
import esbuild from 'rollup-plugin-esbuild'
3+
import json from '@rollup/plugin-json'
34
import dts from 'rollup-plugin-dts'
45
import { defineConfig } from 'rollup'
56
import pkg from './package.json' assert { type: 'json' }
@@ -21,6 +22,7 @@ const plugins = [
2122
esbuild({
2223
target: 'node14',
2324
}),
25+
json(),
2426
]
2527

2628
export default defineConfig([

‎packages/runner/src/run.ts

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { collectTests } from './collect'
88
import { processError } from './utils/error'
99
import { setCurrentTest } from './test-state'
1010
import { hasFailed, hasTests } from './utils/tasks'
11+
import { markVersion } from './version'
1112

1213
const now = Date.now
1314

@@ -350,6 +351,8 @@ export async function runFiles(files: File[], runner: VitestRunner) {
350351
}
351352

352353
export async function startTests(paths: string[], runner: VitestRunner) {
354+
markVersion()
355+
353356
await runner.onBeforeCollect?.(paths)
354357

355358
const files = await collectTests(paths, runner)

‎packages/runner/src/suite.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import type { VitestRunner } from './types/runner'
44
import { createChainable } from './utils/chain'
55
import { collectTask, collectorContext, createTestContext, runWithSuite, withTimeout } from './context'
66
import { getHooks, setFn, setHooks } from './map'
7+
import { checkVersion } from './version'
78

89
// apis
910
export const suite = createSuite()
1011
export const test = createTest(
1112
function (name: string, fn?: TestFunction, options?: number | TestOptions) {
13+
checkVersion()
1214
getCurrentSuite().test.fn.call(this, name, fn, options)
1315
},
1416
)
@@ -182,6 +184,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
182184

183185
function createSuite() {
184186
function suiteFn(this: Record<string, boolean | undefined>, name: string, factory?: SuiteFactory, options?: number | TestOptions) {
187+
checkVersion()
185188
const mode: RunMode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run'
186189
return createSuiteCollector(name, factory, mode, this.concurrent, this.shuffle, options)
187190
}

‎packages/runner/src/version.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { version as VERSION } from '../package.json'
2+
3+
export function getVersion(): string {
4+
// @ts-expect-error internal variable
5+
return globalThis.__vitest_runner_version__ || VERSION
6+
}
7+
8+
export function markVersion(): void {
9+
// @ts-expect-error internal variable
10+
globalThis.__vitest_runner_version__ = VERSION
11+
}
12+
13+
export function checkVersion() {
14+
const collectVersion = getVersion()
15+
16+
if (collectVersion !== VERSION) {
17+
const error = `Version mismatch: Vitest started as ${collectVersion}, but tests are collected with ${VERSION} version.`
18+
+ '\n\n- If you are using global Vitest, make sure your package has the same version.'
19+
+ '\n- If you have a monorepo setup, make sure your main package has the same version as your test packages.'
20+
throw new Error(error)
21+
}
22+
}

0 commit comments

Comments
 (0)
Please sign in to comment.