From d85bd1e677c1395c6d5535b2dc714e09f572e50a Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Wed, 2 Oct 2024 13:05:31 +0200 Subject: [PATCH] fix(vitest): install dependencies with the same version when prompted --- docs/guide/index.md | 4 ++++ packages/vitest/src/node/cli/cli-api.ts | 2 +- packages/vitest/src/node/packageInstaller.ts | 7 ++++--- packages/vitest/src/node/plugins/index.ts | 2 +- packages/vitest/src/node/reporters/utils.ts | 2 +- packages/vitest/src/node/workspace.ts | 2 +- 6 files changed, 12 insertions(+), 7 deletions(-) diff --git a/docs/guide/index.md b/docs/guide/index.md index 21772b5eba45..cee99bf850cd 100644 --- a/docs/guide/index.md +++ b/docs/guide/index.md @@ -230,6 +230,10 @@ You can specify additional CLI options like `--port` or `--https`. For a full li Learn more about the [Command Line Interface](/guide/cli) +## Automatic Dependency Installation + +Vitest will prompt you to install certain dependencies if they are not already installed. You can disable this behavior by setting the `VITEST_SKIP_INSTALL_CHECKS=1` environment variable. + ## IDE Integrations We also provided a official extension for Visual Studio Code to enhance your testing experience with Vitest. diff --git a/packages/vitest/src/node/cli/cli-api.ts b/packages/vitest/src/node/cli/cli-api.ts index 486bf385d047..0735906d71e5 100644 --- a/packages/vitest/src/node/cli/cli-api.ts +++ b/packages/vitest/src/node/cli/cli-api.ts @@ -60,7 +60,7 @@ export async function startVitest( if (requiredPackages) { if ( - !(await ctx.packageInstaller.ensureInstalled(requiredPackages, root)) + !(await ctx.packageInstaller.ensureInstalled(requiredPackages, root, ctx.version)) ) { process.exitCode = 1 return ctx diff --git a/packages/vitest/src/node/packageInstaller.ts b/packages/vitest/src/node/packageInstaller.ts index 481b22c2211f..b0f6f3d21fae 100644 --- a/packages/vitest/src/node/packageInstaller.ts +++ b/packages/vitest/src/node/packageInstaller.ts @@ -7,7 +7,7 @@ import { isCI } from '../utils/env' const __dirname = url.fileURLToPath(new URL('.', import.meta.url)) export class VitestPackageInstaller { - async ensureInstalled(dependency: string, root: string) { + async ensureInstalled(dependency: string, root: string, version?: string) { if (process.env.VITEST_SKIP_INSTALL_CHECKS) { return true } @@ -49,13 +49,14 @@ export class VitestPackageInstaller { }) if (install) { + const packageName = version ? `${dependency}@${version}` : dependency await ( await import('@antfu/install-pkg') - ).installPackage(dependency, { dev: true }) + ).installPackage(packageName, { dev: true }) // TODO: somehow it fails to load the package after installation, remove this when it's fixed process.stderr.write( c.yellow( - `\nPackage ${dependency} installed, re-run the command to start.\n`, + `\nPackage ${packageName} installed, re-run the command to start.\n`, ), ) process.exit() diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index 81edeeff7f4c..fb82f7884b3e 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -34,7 +34,7 @@ export async function VitestPlugin( const getRoot = () => ctx.config?.root || options.root || process.cwd() async function UIPlugin() { - await ctx.packageInstaller.ensureInstalled('@vitest/ui', getRoot()) + await ctx.packageInstaller.ensureInstalled('@vitest/ui', getRoot(), ctx.version) return (await import('@vitest/ui')).default(ctx) } diff --git a/packages/vitest/src/node/reporters/utils.ts b/packages/vitest/src/node/reporters/utils.ts index e3306fa0e36a..8418747f9128 100644 --- a/packages/vitest/src/node/reporters/utils.ts +++ b/packages/vitest/src/node/reporters/utils.ts @@ -42,7 +42,7 @@ function createReporters( const [reporterName, reporterOptions] = referenceOrInstance if (reporterName === 'html') { - await ctx.packageInstaller.ensureInstalled('@vitest/ui', runner.root) + await ctx.packageInstaller.ensureInstalled('@vitest/ui', runner.root, ctx.version) const CustomReporter = await loadCustomReporterModule( '@vitest/ui/reporter', runner, diff --git a/packages/vitest/src/node/workspace.ts b/packages/vitest/src/node/workspace.ts index 51b9a7ef207e..96ed2f784feb 100644 --- a/packages/vitest/src/node/workspace.ts +++ b/packages/vitest/src/node/workspace.ts @@ -359,7 +359,7 @@ export class WorkspaceProject { if (!this.isBrowserEnabled()) { return } - await this.ctx.packageInstaller.ensureInstalled('@vitest/browser', this.config.root) + await this.ctx.packageInstaller.ensureInstalled('@vitest/browser', this.config.root, this.ctx.version) const { createBrowserServer } = await import('@vitest/browser') await this.browser?.close() const browser = await createBrowserServer(