From 708b10fe227c04b4219a1e5d4b539def009729b4 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 4 May 2023 15:26:32 +0200 Subject: [PATCH] fix: throw an error, if tests are collected with a different vitest version (#3301) --- packages/runner/rollup.config.js | 2 ++ packages/runner/src/run.ts | 3 +++ packages/runner/src/suite.ts | 3 +++ packages/runner/src/version.ts | 22 ++++++++++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 packages/runner/src/version.ts diff --git a/packages/runner/rollup.config.js b/packages/runner/rollup.config.js index e9576529ff6..1d1f02aca17 100644 --- a/packages/runner/rollup.config.js +++ b/packages/runner/rollup.config.js @@ -1,5 +1,6 @@ import { builtinModules } from 'node:module' import esbuild from 'rollup-plugin-esbuild' +import json from '@rollup/plugin-json' import dts from 'rollup-plugin-dts' import { defineConfig } from 'rollup' import pkg from './package.json' assert { type: 'json' } @@ -21,6 +22,7 @@ const plugins = [ esbuild({ target: 'node14', }), + json(), ] export default defineConfig([ diff --git a/packages/runner/src/run.ts b/packages/runner/src/run.ts index 9eaaa97a4e1..e72819b0150 100644 --- a/packages/runner/src/run.ts +++ b/packages/runner/src/run.ts @@ -8,6 +8,7 @@ import { collectTests } from './collect' import { processError } from './utils/error' import { setCurrentTest } from './test-state' import { hasFailed, hasTests } from './utils/tasks' +import { markVersion } from './version' const now = Date.now @@ -350,6 +351,8 @@ export async function runFiles(files: File[], runner: VitestRunner) { } export async function startTests(paths: string[], runner: VitestRunner) { + markVersion() + await runner.onBeforeCollect?.(paths) const files = await collectTests(paths, runner) diff --git a/packages/runner/src/suite.ts b/packages/runner/src/suite.ts index 16fd20232f0..15050880ce5 100644 --- a/packages/runner/src/suite.ts +++ b/packages/runner/src/suite.ts @@ -4,11 +4,13 @@ import type { VitestRunner } from './types/runner' import { createChainable } from './utils/chain' import { collectTask, collectorContext, createTestContext, runWithSuite, withTimeout } from './context' import { getHooks, setFn, setHooks } from './map' +import { checkVersion } from './version' // apis export const suite = createSuite() export const test = createTest( function (name: string, fn?: TestFunction, options?: number | TestOptions) { + checkVersion() getCurrentSuite().test.fn.call(this, name, fn, options) }, ) @@ -182,6 +184,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m function createSuite() { function suiteFn(this: Record, name: string, factory?: SuiteFactory, options?: number | TestOptions) { + checkVersion() const mode: RunMode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run' return createSuiteCollector(name, factory, mode, this.concurrent, this.shuffle, options) } diff --git a/packages/runner/src/version.ts b/packages/runner/src/version.ts new file mode 100644 index 00000000000..3bfa04deb11 --- /dev/null +++ b/packages/runner/src/version.ts @@ -0,0 +1,22 @@ +import { version as VERSION } from '../package.json' + +export function getVersion(): string { + // @ts-expect-error internal variable + return globalThis.__vitest_runner_version__ || VERSION +} + +export function markVersion(): void { + // @ts-expect-error internal variable + globalThis.__vitest_runner_version__ = VERSION +} + +export function checkVersion() { + const collectVersion = getVersion() + + if (collectVersion !== VERSION) { + const error = `Version mismatch: Vitest started as ${collectVersion}, but tests are collected with ${VERSION} version.` + + '\n\n- If you are using global Vitest, make sure your package has the same version.' + + '\n- If you have a monorepo setup, make sure your main package has the same version as your test packages.' + throw new Error(error) + } +}