Skip to content

Commit ed8b7c0

Browse files
authoredOct 2, 2024··
fix(vitest): install dependencies with the same version when prompted (#6611)
1 parent 288c5c8 commit ed8b7c0

File tree

6 files changed

+12
-7
lines changed

6 files changed

+12
-7
lines changed
 

‎docs/guide/index.md

+4
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ You can specify additional CLI options like `--port` or `--https`. For a full li
230230

231231
Learn more about the [Command Line Interface](/guide/cli)
232232

233+
## Automatic Dependency Installation
234+
235+
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.
236+
233237
## IDE Integrations
234238

235239
We also provided a official extension for Visual Studio Code to enhance your testing experience with Vitest.

‎packages/vitest/src/node/cli/cli-api.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export async function startVitest(
6060

6161
if (requiredPackages) {
6262
if (
63-
!(await ctx.packageInstaller.ensureInstalled(requiredPackages, root))
63+
!(await ctx.packageInstaller.ensureInstalled(requiredPackages, root, ctx.version))
6464
) {
6565
process.exitCode = 1
6666
return ctx

‎packages/vitest/src/node/packageInstaller.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { isCI } from '../utils/env'
77
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
88

99
export class VitestPackageInstaller {
10-
async ensureInstalled(dependency: string, root: string) {
10+
async ensureInstalled(dependency: string, root: string, version?: string) {
1111
if (process.env.VITEST_SKIP_INSTALL_CHECKS) {
1212
return true
1313
}
@@ -49,13 +49,14 @@ export class VitestPackageInstaller {
4949
})
5050

5151
if (install) {
52+
const packageName = version ? `${dependency}@${version}` : dependency
5253
await (
5354
await import('@antfu/install-pkg')
54-
).installPackage(dependency, { dev: true })
55+
).installPackage(packageName, { dev: true })
5556
// TODO: somehow it fails to load the package after installation, remove this when it's fixed
5657
process.stderr.write(
5758
c.yellow(
58-
`\nPackage ${dependency} installed, re-run the command to start.\n`,
59+
`\nPackage ${packageName} installed, re-run the command to start.\n`,
5960
),
6061
)
6162
process.exit()

‎packages/vitest/src/node/plugins/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export async function VitestPlugin(
3434
const getRoot = () => ctx.config?.root || options.root || process.cwd()
3535

3636
async function UIPlugin() {
37-
await ctx.packageInstaller.ensureInstalled('@vitest/ui', getRoot())
37+
await ctx.packageInstaller.ensureInstalled('@vitest/ui', getRoot(), ctx.version)
3838
return (await import('@vitest/ui')).default(ctx)
3939
}
4040

‎packages/vitest/src/node/reporters/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function createReporters(
4242
const [reporterName, reporterOptions] = referenceOrInstance
4343

4444
if (reporterName === 'html') {
45-
await ctx.packageInstaller.ensureInstalled('@vitest/ui', runner.root)
45+
await ctx.packageInstaller.ensureInstalled('@vitest/ui', runner.root, ctx.version)
4646
const CustomReporter = await loadCustomReporterModule(
4747
'@vitest/ui/reporter',
4848
runner,

‎packages/vitest/src/node/workspace.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ export class WorkspaceProject {
359359
if (!this.isBrowserEnabled()) {
360360
return
361361
}
362-
await this.ctx.packageInstaller.ensureInstalled('@vitest/browser', this.config.root)
362+
await this.ctx.packageInstaller.ensureInstalled('@vitest/browser', this.config.root, this.ctx.version)
363363
const { createBrowserServer } = await import('@vitest/browser')
364364
await this.browser?.close()
365365
const browser = await createBrowserServer(

0 commit comments

Comments
 (0)
Please sign in to comment.