Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/browser/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export async function createBrowserServer(
hmr: false,
watch: null,
},
cacheDir: project.vite.config.cacheDir,
plugins: [
...prePlugins,
...(project.options?.plugins || []),
Expand Down
15 changes: 7 additions & 8 deletions packages/vitest/src/node/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
)
}
}
25 changes: 4 additions & 21 deletions packages/vitest/src/node/config/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 13 additions & 6 deletions packages/vitest/src/node/plugins/optimizer.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand Down
9 changes: 1 addition & 8 deletions packages/vitest/src/node/plugins/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -41,7 +38,6 @@ export function resolveOptimizerConfig(
}
}
else {
const root = testConfig.root ?? process.cwd()
const currentInclude = testOptions.include || viteOptions?.include || []
const exclude = [
'vitest',
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions packages/vitest/src/node/plugins/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function WorkspaceVitestPlugin(
return <VitePlugin[]>[
{
name: 'vitest:project',
enforce: 'pre',
enforce: 'post',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Workspace's plugins can no longer access resolved values in config: #8120

options() {
this.meta.watchMode = false
},
Expand Down Expand Up @@ -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(),
]
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions test/config/test/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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)
})

Expand All @@ -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)
})

Expand All @@ -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)
})

Expand Down Expand Up @@ -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)
})

Expand All @@ -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)
})

Expand All @@ -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)
})
})
2 changes: 1 addition & 1 deletion test/config/test/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})

Expand Down
3 changes: 2 additions & 1 deletion test/config/test/projects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/coverage-test/test/workspace.multi-transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
2 changes: 1 addition & 1 deletion test/optimize-deps/test/ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/')
})
2 changes: 1 addition & 1 deletion test/optimize-deps/test/web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/')
})