diff --git a/packages/browser/src/node/index.ts b/packages/browser/src/node/index.ts index 3dc3c672a..1c6312a37 100644 --- a/packages/browser/src/node/index.ts +++ b/packages/browser/src/node/index.ts @@ -69,6 +69,7 @@ export async function createBrowserServer( hmr: false, watch: null, }, + cacheDir: project.vite.config.cacheDir, plugins: [ ...prePlugins, ...(project.options?.plugins || []), diff --git a/packages/vitest/src/node/cache/index.ts b/packages/vitest/src/node/cache/index.ts index 3339ae440..5a1e4ceb9 100644 --- a/packages/vitest/src/node/cache/index.ts +++ b/packages/vitest/src/node/cache/index.ts @@ -24,13 +24,12 @@ export class VitestCache { } static resolveCacheDir(root: string, dir?: string, projectName?: string): string { - const baseDir = slash(dir || 'node_modules/.vite/vitest') - return projectName - ? resolve( - root, - baseDir, - hash('md5', projectName, 'hex'), - ) - : resolve(root, baseDir) + const baseDir = slash(dir || 'node_modules/.vite') + return resolve( + root, + baseDir, + 'vitest', + hash('md5', projectName || '', 'hex'), + ) } } diff --git a/packages/vitest/src/node/config/resolveConfig.ts b/packages/vitest/src/node/config/resolveConfig.ts index bf0dad29f..a4659f08f 100644 --- a/packages/vitest/src/node/config/resolveConfig.ts +++ b/packages/vitest/src/node/config/resolveConfig.ts @@ -24,7 +24,6 @@ import { import { benchmarkConfigDefaults, configDefaults } from '../../defaults' import { isCI, stdProvider } from '../../utils/env' import { getWorkersCountByPercentage } from '../../utils/workers' -import { VitestCache } from '../cache' import { builtinPools } from '../pool' import { BaseSequencer } from '../sequencers/BaseSequencer' import { RandomSequencer } from '../sequencers/RandomSequencer' @@ -745,29 +744,13 @@ export function resolveConfig( } if (resolved.cache !== false) { - let cacheDir = VitestCache.resolveCacheDir( - '', - viteConfig.cacheDir, - resolved.name, - ) - - if (resolved.cache && resolved.cache.dir) { - logger.console.warn( - c.yellow( - `${c.inverse( - c.yellow(' Vitest '), - )} "cache.dir" is deprecated, use Vite's "cacheDir" instead if you want to change the cache director. Note caches will be written to "cacheDir\/vitest"`, - ), - ) - - cacheDir = VitestCache.resolveCacheDir( - resolved.root, - resolved.cache.dir, - resolved.name, + if (resolved.cache && typeof resolved.cache.dir === 'string') { + vitest.logger.deprecate( + `"cache.dir" is deprecated, use Vite's "cacheDir" instead if you want to change the cache director. Note caches will be written to "cacheDir\/vitest"`, ) } - resolved.cache = { dir: cacheDir } + resolved.cache = { dir: viteConfig.cacheDir } } resolved.sequence ??= {} as any diff --git a/packages/vitest/src/node/logger.ts b/packages/vitest/src/node/logger.ts index 5581c4030..c1a46d521 100644 --- a/packages/vitest/src/node/logger.ts +++ b/packages/vitest/src/node/logger.ts @@ -111,7 +111,7 @@ export class Logger { } deprecate(message: string): void { - this.log(c.bold(c.bgYellow(' DEPRECATED ')), c.yellow(message)) + this.error(c.bold(c.bgYellow(' DEPRECATED ')), c.yellow(message)) } clearHighlightCache(filename?: string): void { diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index 5644ec41a..c31ee181d 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -144,6 +144,12 @@ export async function VitestPlugin( }, } + // inherit so it's available in VitestOptimizer + // I cannot wait to rewrite all of this in Vitest 4 + if (options.cache != null) { + config.test!.cache = options.cache + } + if (vitest.configOverride.project) { // project filter was set by the user, so we need to filter the project options.project = vitest.configOverride.project diff --git a/packages/vitest/src/node/plugins/optimizer.ts b/packages/vitest/src/node/plugins/optimizer.ts index 5077327ca..4c2f00ffe 100644 --- a/packages/vitest/src/node/plugins/optimizer.ts +++ b/packages/vitest/src/node/plugins/optimizer.ts @@ -1,4 +1,6 @@ import type { Plugin } from 'vite' +import { resolve } from 'pathe' +import { VitestCache } from '../cache' import { resolveOptimizerConfig } from './utils' export function VitestOptimizer(): Plugin { @@ -11,18 +13,23 @@ export function VitestOptimizer(): Plugin { const webOptimizer = resolveOptimizerConfig( testConfig.deps?.optimizer?.web, viteConfig.optimizeDeps, - testConfig, - viteConfig.cacheDir, ) const ssrOptimizer = resolveOptimizerConfig( testConfig.deps?.optimizer?.ssr, viteConfig.ssr?.optimizeDeps, - testConfig, - viteConfig.cacheDir, ) - viteConfig.cacheDir - = webOptimizer.cacheDir || ssrOptimizer.cacheDir || viteConfig.cacheDir + const root = resolve(viteConfig.root || process.cwd()) + const name = viteConfig.test?.name + const label = typeof name === 'string' ? name : (name?.label || '') + + viteConfig.cacheDir = VitestCache.resolveCacheDir( + resolve(root || process.cwd()), + testConfig.cache != null && testConfig.cache !== false + ? testConfig.cache.dir + : viteConfig.cacheDir, + label, + ) viteConfig.optimizeDeps = webOptimizer.optimizeDeps viteConfig.ssr ??= {} viteConfig.ssr.optimizeDeps = ssrOptimizer.optimizeDeps diff --git a/packages/vitest/src/node/plugins/utils.ts b/packages/vitest/src/node/plugins/utils.ts index 4486ebf46..17b3553e2 100644 --- a/packages/vitest/src/node/plugins/utils.ts +++ b/packages/vitest/src/node/plugins/utils.ts @@ -2,18 +2,15 @@ import type { DepOptimizationOptions, UserConfig as ViteConfig, } from 'vite' -import type { DepsOptimizationOptions, InlineConfig } from '../types/config' +import type { DepsOptimizationOptions } from '../types/config' import { dirname } from 'pathe' import { searchForWorkspaceRoot, version as viteVersion } from 'vite' import * as vite from 'vite' import { rootDir } from '../../paths' -import { VitestCache } from '../cache' export function resolveOptimizerConfig( _testOptions: DepsOptimizationOptions | undefined, viteOptions: DepOptimizationOptions | undefined, - testConfig: InlineConfig, - viteCacheDir: string | undefined, ): { cacheDir?: string; optimizeDeps: DepOptimizationOptions } { const testOptions = _testOptions || {} const newConfig: { cacheDir?: string; optimizeDeps: DepOptimizationOptions } @@ -41,7 +38,6 @@ export function resolveOptimizerConfig( } } else { - const root = testConfig.root ?? process.cwd() const currentInclude = testOptions.include || viteOptions?.include || [] const exclude = [ 'vitest', @@ -59,9 +55,6 @@ export function resolveOptimizerConfig( (n: string) => !exclude.includes(n), ) - const projectName = typeof testConfig.name === 'string' ? testConfig.name : testConfig.name?.label - - newConfig.cacheDir = (testConfig.cache !== false && testConfig.cache?.dir) || VitestCache.resolveCacheDir(root, viteCacheDir, projectName) newConfig.optimizeDeps = { ...viteOptions, ...testOptions, diff --git a/packages/vitest/src/node/plugins/workspace.ts b/packages/vitest/src/node/plugins/workspace.ts index 35c04912d..8fcbc8b84 100644 --- a/packages/vitest/src/node/plugins/workspace.ts +++ b/packages/vitest/src/node/plugins/workspace.ts @@ -33,7 +33,7 @@ export function WorkspaceVitestPlugin( return [ { name: 'vitest:project', - enforce: 'pre', + enforce: 'post', options() { this.meta.watchMode = false }, @@ -190,9 +190,9 @@ export function WorkspaceVitestPlugin( }, SsrReplacerPlugin(), ...CSSEnablerPlugin(project), - CoverageTransform(project.ctx), + CoverageTransform(project.vitest), ...MocksPlugins(), - VitestProjectResolver(project.ctx), + VitestProjectResolver(project.vitest), VitestOptimizer(), NormalizeURLPlugin(), ] diff --git a/packages/vitest/src/node/types/config.ts b/packages/vitest/src/node/types/config.ts index e30a5b060..06fca56cf 100644 --- a/packages/vitest/src/node/types/config.ts +++ b/packages/vitest/src/node/types/config.ts @@ -687,7 +687,7 @@ export interface InlineConfig { /** * Options for configuring cache policy. - * @default { dir: 'node_modules/.vite/vitest' } + * @default { dir: 'node_modules/.vite/vitest/{project-hash}' } */ cache?: | false diff --git a/test/config/test/cache.test.ts b/test/config/test/cache.test.ts index 386e28408..81e6b2ac0 100644 --- a/test/config/test/cache.test.ts +++ b/test/config/test/cache.test.ts @@ -4,7 +4,6 @@ import { describe, expect, test } from 'vitest' import { runVitest } from '../../test-utils' const root = resolve(__dirname, '../fixtures/cache') -const project = resolve(__dirname, '../') test('default', async () => { const { ctx, stdout, stderr } = await runVitest({ @@ -16,7 +15,7 @@ test('default', async () => { expect(stderr).toBe('') const cachePath = ctx!.cache.results.getCachePath() - const path = resolve(project, 'node_modules/.vite/results.json') + const path = resolve(root, 'node_modules/.vite/vitest/d41d8cd98f00b204e9800998ecf8427e/results.json') expect(cachePath).toMatch(path) }) @@ -35,7 +34,7 @@ test('use cache.dir', async () => { expect(stderr).toContain('"cache.dir" is deprecated') const cachePath = ctx!.cache.results.getCachePath() - const path = resolve(root, 'node_modules/.vitest-custom/results.json') + const path = resolve(root, 'node_modules/.vitest-custom/vitest/d41d8cd98f00b204e9800998ecf8427e/results.json') expect(cachePath).toMatch(path) }) @@ -54,7 +53,7 @@ test('use cacheDir', async () => { expect(stderr).toBe('') const cachePath = ctx!.cache.results.getCachePath() - const path = resolve(root, 'node_modules/.vite-custom/results.json') + const path = resolve(root, 'node_modules/.vite-custom/vitest/d41d8cd98f00b204e9800998ecf8427e/results.json') expect(cachePath).toMatch(path) }) @@ -103,7 +102,7 @@ describe('with optimizer enabled', () => { expect(stderr).toBe('') const cachePath = ctx!.cache.results.getCachePath() - const path = resolve(root, 'node_modules/.vite/vitest/results.json') + const path = resolve(root, 'node_modules/.vite/vitest/d41d8cd98f00b204e9800998ecf8427e/results.json') expect(cachePath).toBe(path) }) @@ -123,7 +122,7 @@ describe('with optimizer enabled', () => { expect(stderr).toContain('"cache.dir" is deprecated') const cachePath = ctx!.cache.results.getCachePath() - const path = resolve(root, 'node_modules/.vitest-custom/results.json') + const path = resolve(root, 'node_modules/.vitest-custom/vitest/d41d8cd98f00b204e9800998ecf8427e/results.json') expect(cachePath).toBe(path) }) @@ -143,7 +142,7 @@ describe('with optimizer enabled', () => { expect(stderr).toBe('') const cachePath = ctx!.cache.results.getCachePath() - const path = resolve(root, 'node_modules/.vite-custom/results.json') + const path = resolve(root, 'node_modules/.vite-custom/vitest/d41d8cd98f00b204e9800998ecf8427e/results.json') expect(cachePath).toBe(path) }) }) diff --git a/test/config/test/project.test.ts b/test/config/test/project.test.ts index fd6616601..d9f6b14b7 100644 --- a/test/config/test/project.test.ts +++ b/test/config/test/project.test.ts @@ -15,7 +15,7 @@ test.each([ ])('should match projects correctly: $pattern', async ({ pattern, expected }) => { const { ctx, stderr, stdout } = await runVitest({ root: 'fixtures/project', - reporters: ['basic'], + reporters: ['default'], project: pattern, }) diff --git a/test/config/test/projects.test.ts b/test/config/test/projects.test.ts index c91fe361a..b3aa172f9 100644 --- a/test/config/test/projects.test.ts +++ b/test/config/test/projects.test.ts @@ -3,11 +3,12 @@ import { expect, it } from 'vitest' import { runVitest } from '../../test-utils' it('correctly runs workspace tests when workspace config path is specified', async () => { + // TODO: remove the test in Vitest 4 const { stderr, stdout } = await runVitest({ root: 'fixtures/workspace', workspace: 'nested/e2e.projects.js', }) - expect(stderr).toBe('') + expect(stderr).toContain('The workspace file is deprecated and will be removed in the next major') expect(stdout).toContain('1 + 1 = 2') expect(stdout).not.toContain('2 + 2 = 4') }) diff --git a/test/coverage-test/fixtures/configs/vitest.workspace.multi-transforms.ts b/test/coverage-test/fixtures/configs/vitest.config.multi-transforms.ts similarity index 59% rename from test/coverage-test/fixtures/configs/vitest.workspace.multi-transforms.ts rename to test/coverage-test/fixtures/configs/vitest.config.multi-transforms.ts index 507ede9cf..d02e01c44 100644 --- a/test/coverage-test/fixtures/configs/vitest.workspace.multi-transforms.ts +++ b/test/coverage-test/fixtures/configs/vitest.config.multi-transforms.ts @@ -1,34 +1,38 @@ import { readFileSync } from "node:fs"; -import { Plugin, defineWorkspace } from "vitest/config"; +import { Plugin, defineConfig } from "vitest/config"; import MagicString from "magic-string"; -export default defineWorkspace([ - // Project that uses its own "root" and custom transform plugin - { - test: { - name: "custom-with-root", - root: "fixtures/workspaces/custom-2", - }, - plugins: [customFilePlugin("2")], - }, +export default defineConfig({ + test: { + projects: [ + // Project that uses its own "root" and custom transform plugin + { + test: { + name: "custom-with-root", + root: "fixtures/workspaces/custom-2", + }, + plugins: [customFilePlugin("2")], + }, - // Project that cannot transform "*.custom-x" files - { - test: { - name: "normal", - include: ["fixtures/test/math.test.ts"], - }, - }, + // Project that cannot transform "*.custom-x" files + { + test: { + name: "normal", + include: ["fixtures/test/math.test.ts"], + }, + }, - // Project that uses default "root" and has custom transform plugin - { - test: { - name: "custom", - include: ["fixtures/test/custom-1-syntax.test.ts"], - }, - plugins: [customFilePlugin("1")], - }, -]); + // Project that uses default "root" and has custom transform plugin + { + test: { + name: "custom", + include: ["fixtures/test/custom-1-syntax.test.ts"], + }, + plugins: [customFilePlugin("1")], + }, + ] + } +}); /** * Plugin for transforming `.custom-1` and/or `.custom-2` files to Javascript diff --git a/test/coverage-test/test/workspace.multi-transform.test.ts b/test/coverage-test/test/workspace.multi-transform.test.ts index 1a1652551..eecc0353e 100644 --- a/test/coverage-test/test/workspace.multi-transform.test.ts +++ b/test/coverage-test/test/workspace.multi-transform.test.ts @@ -3,7 +3,7 @@ import { isV8Provider, readCoverageMap, runVitest, test } from '../utils' test('{ all: true } includes uncovered files that require custom transform', async () => { await runVitest({ - workspace: 'fixtures/configs/vitest.workspace.multi-transforms.ts', + config: './fixtures/configs/vitest.config.multi-transforms.ts', coverage: { all: true, extension: ['.ts', '.custom-1', '.custom-2'], diff --git a/test/optimize-deps/test/ssr.test.ts b/test/optimize-deps/test/ssr.test.ts index 8f3b1b83d..39d92337d 100644 --- a/test/optimize-deps/test/ssr.test.ts +++ b/test/optimize-deps/test/ssr.test.ts @@ -8,5 +8,5 @@ import { expect, test } from 'vitest' // TODO: flaky on Windows // https://github.com/vitest-dev/vitest/pull/5215#discussion_r1492066033 test.skipIf(process.platform === 'win32')('import.meta.url', () => { - expect(importMetaUrl).toContain('/node_modules/.vite/vitest/deps_ssr/') + expect(importMetaUrl).toContain('/node_modules/.vite/vitest/d41d8cd98f00b204e9800998ecf8427e/deps_ssr/') }) diff --git a/test/optimize-deps/test/web.test.ts b/test/optimize-deps/test/web.test.ts index e7ce6cf41..2d1a0d64e 100644 --- a/test/optimize-deps/test/web.test.ts +++ b/test/optimize-deps/test/web.test.ts @@ -6,5 +6,5 @@ import { importMetaUrl } from '@vitest/test-dep-url' import { expect, test } from 'vitest' test('import.meta.url', () => { - expect(importMetaUrl).toContain('/node_modules/.vite/vitest/deps/') + expect(importMetaUrl).toContain('/node_modules/.vite/vitest/d41d8cd98f00b204e9800998ecf8427e/deps/') })