|
| 1 | +/** |
| 2 | + * Compatibility layer for vitest imports across versions. |
| 3 | + * |
| 4 | + * Vitest 4.1 deprecated `vitest/runners` and `vitest/suite` subpath imports, |
| 5 | + * moving exports to the main `vitest` entry point. This module resolves |
| 6 | + * the correct imports at runtime to avoid deprecation warnings while |
| 7 | + * maintaining compatibility with vitest 3.2+. |
| 8 | + */ |
| 9 | + |
| 10 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 11 | +type AnyClass = new (...args: any[]) => any; |
| 12 | + |
| 13 | +// Resolve NodeBenchmarkRunner: vitest >= 4.1 exports it as BenchmarkRunner |
| 14 | +// from the main entry; older versions export it from vitest/runners. |
| 15 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 16 | +const vitestMod: any = await import("vitest"); |
| 17 | +const _BenchmarkRunner: AnyClass | undefined = vitestMod.BenchmarkRunner; |
| 18 | + |
| 19 | +let _NodeBenchmarkRunner: AnyClass; |
| 20 | +if (_BenchmarkRunner) { |
| 21 | + _NodeBenchmarkRunner = _BenchmarkRunner; |
| 22 | +} else { |
| 23 | + const runners = await import("vitest/runners"); |
| 24 | + _NodeBenchmarkRunner = runners.NodeBenchmarkRunner; |
| 25 | +} |
| 26 | +export const NodeBenchmarkRunner = _NodeBenchmarkRunner; |
| 27 | + |
| 28 | +// Resolve suite helpers: vitest >= 4.1 exposes them as TestRunner static |
| 29 | +// methods; older versions export them from vitest/suite. |
| 30 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 31 | +type SuiteFn = (...args: any[]) => any; |
| 32 | +let _getHooks: SuiteFn; |
| 33 | +let _getBenchFn: SuiteFn; |
| 34 | +let _getBenchOptions: SuiteFn; |
| 35 | + |
| 36 | +const TestRunner = vitestMod.TestRunner; |
| 37 | +if (TestRunner?.getSuiteHooks) { |
| 38 | + _getHooks = TestRunner.getSuiteHooks; |
| 39 | + _getBenchFn = TestRunner.getBenchFn; |
| 40 | + _getBenchOptions = TestRunner.getBenchOptions; |
| 41 | +} else { |
| 42 | + const suite = await import("vitest/suite"); |
| 43 | + _getHooks = suite.getHooks; |
| 44 | + _getBenchFn = suite.getBenchFn; |
| 45 | + _getBenchOptions = suite.getBenchOptions; |
| 46 | +} |
| 47 | + |
| 48 | +export const getHooks = _getHooks; |
| 49 | +export const getBenchFn = _getBenchFn; |
| 50 | +export const getBenchOptions = _getBenchOptions; |
0 commit comments