Skip to content

Commit d7e8b53

Browse files
authoredMar 15, 2024··
feat(config): deprecate cache.dir option (#5229)
1 parent 4f6e39c commit d7e8b53

File tree

10 files changed

+162
-34
lines changed

10 files changed

+162
-34
lines changed
 

‎docs/config/index.md

+2-10
Original file line numberDiff line numberDiff line change
@@ -1736,18 +1736,10 @@ Test above this limit will be queued to run when available slot appears.
17361736

17371737
### cache<NonProjectOption />
17381738

1739-
- **Type**: `false | { dir? }`
1739+
- **Type**: `false`
17401740
- **CLI**: `--no-cache`, `--cache=false`
17411741

1742-
Options to configure Vitest cache policy. At the moment Vitest stores cache for test results to run the longer and failed tests first.
1743-
1744-
#### cache.dir
1745-
1746-
- **Type**: `string`
1747-
- **Default**: `node_modules/.vitest`
1748-
- **CLI**: `--cache.dir=./cache`
1749-
1750-
Path to cache directory.
1742+
Use this option if you want to disable the cache feature. At the moment Vitest stores cache for test results to run the longer and failed tests first.
17511743

17521744
### sequence
17531745

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export class VitestCache {
2121
return this.stats.getStats(key)
2222
}
2323

24-
static resolveCacheDir(root: string, dir: string | undefined, projectName: string | undefined) {
25-
const baseDir = slash(dir || 'node_modules/.vitest')
24+
static resolveCacheDir(root: string, dir?: string, projectName?: string) {
25+
const baseDir = slash(dir || 'node_modules/.vite/vitest')
2626
return projectName
2727
? resolve(root, baseDir, crypto.createHash('md5').update(projectName, 'utf-8').digest('hex'))
2828
: resolve(root, baseDir)

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -549,14 +549,13 @@ export const cliOptionsConfig: VitestCLIOptions = {
549549
description: 'Enable cache',
550550
argument: '', // allow only boolean
551551
subcommands: {
552-
dir: {
553-
description: 'Path to the cache directory',
554-
argument: '<path>',
555-
normalize: true,
556-
},
552+
dir: null,
557553
},
554+
default: true,
558555
// cache can only be "false" or an object
559556
transform(cache) {
557+
if (typeof cache !== 'boolean' && cache)
558+
throw new Error('--cache.dir is deprecated')
560559
if (cache)
561560
return {}
562561
return cache

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,21 @@ export function resolveConfig(
446446
resolved.css.modules.classNameStrategy ??= 'stable'
447447
}
448448

449-
resolved.cache ??= { dir: '' }
450-
if (resolved.cache)
451-
resolved.cache.dir = VitestCache.resolveCacheDir(resolved.root, resolved.cache.dir, resolved.name)
449+
if (resolved.cache !== false) {
450+
let cacheDir = VitestCache.resolveCacheDir('', resolve(viteConfig.cacheDir, 'vitest'), resolved.name)
451+
452+
if (resolved.cache && resolved.cache.dir) {
453+
console.warn(
454+
c.yellow(
455+
`${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"`,
456+
),
457+
)
458+
459+
cacheDir = VitestCache.resolveCacheDir(resolved.root, resolved.cache.dir, resolved.name)
460+
}
461+
462+
resolved.cache = { dir: cacheDir }
463+
}
452464

453465
resolved.sequence ??= {} as any
454466
if (resolved.sequence.shuffle && typeof resolved.sequence.shuffle === 'object') {

‎packages/vitest/src/types/config.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,13 @@ export interface InlineConfig {
624624

625625
/**
626626
* Options for configuring cache policy.
627-
* @default { dir: 'node_modules/.vitest' }
627+
* @default { dir: 'node_modules/.vite/vitest' }
628628
*/
629629
cache?: false | {
630-
dir?: string
630+
/**
631+
* @deprecated Use Vite's "cacheDir" instead if you want to change the cache director. Note caches will be written to "cacheDir\/vitest".
632+
*/
633+
dir: string
631634
}
632635

633636
/**
@@ -854,6 +857,9 @@ export interface ResolvedConfig extends Omit<Required<UserConfig>, 'config' | 'f
854857
}
855858

856859
cache: {
860+
/**
861+
* @deprecated
862+
*/
857863
dir: string
858864
} | false
859865

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expect, test } from "vitest";
2+
3+
test('', () => {
4+
expect(true).toBe(true)
5+
})

‎test/config/test/cache.test.ts

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import { describe, expect, test } from 'vitest'
2+
import { resolve } from 'pathe'
3+
import { runVitest } from '../../test-utils'
4+
5+
const root = resolve(__dirname, '../fixtures/cache')
6+
const project = resolve(__dirname, '../')
7+
8+
test('default', async () => {
9+
const { vitest, stdout, stderr } = await runVitest({
10+
root,
11+
include: ['*.test.ts'],
12+
})
13+
14+
expect(stdout).toContain('✓ basic.test.ts >')
15+
expect(stderr).toBe('')
16+
17+
const cachePath = vitest!.cache.results.getCachePath()
18+
const path = resolve(project, 'node_modules/.vite/vitest/results.json')
19+
expect(cachePath).toMatch(path)
20+
})
21+
22+
test('use cache.dir', async () => {
23+
const { vitest, stdout, stderr } = await runVitest(
24+
{
25+
root,
26+
include: ['*.test.ts'],
27+
cache: {
28+
dir: 'node_modules/.vitest-custom',
29+
},
30+
},
31+
)
32+
33+
expect(stdout).toContain('✓ basic.test.ts >')
34+
expect(stderr).toContain('"cache.dir" is deprecated')
35+
36+
const cachePath = vitest!.cache.results.getCachePath()
37+
const path = resolve(root, 'node_modules/.vitest-custom/results.json')
38+
expect(cachePath).toMatch(path)
39+
})
40+
41+
test('use cacheDir', async () => {
42+
const { vitest, stdout, stderr } = await runVitest(
43+
{
44+
root,
45+
include: ['*.test.ts'],
46+
},
47+
[],
48+
'test',
49+
{ cacheDir: 'node_modules/.vite-custom' },
50+
)
51+
52+
expect(stdout).toContain('✓ basic.test.ts >')
53+
expect(stderr).toBe('')
54+
55+
const cachePath = vitest!.cache.results.getCachePath()
56+
const path = resolve(root, 'node_modules/.vite-custom/vitest/results.json')
57+
expect(cachePath).toMatch(path)
58+
})
59+
60+
describe('with optimizer enabled', () => {
61+
const deps = {
62+
optimizer: {
63+
web: {
64+
enabled: true,
65+
},
66+
},
67+
}
68+
69+
test('default', async () => {
70+
const { vitest, stdout, stderr } = await runVitest({
71+
root,
72+
include: ['*.test.ts'],
73+
deps,
74+
})
75+
76+
expect(stdout).toContain('✓ basic.test.ts >')
77+
expect(stderr).toBe('')
78+
79+
const cachePath = vitest!.cache.results.getCachePath()
80+
const path = resolve(project, 'node_modules/.vite/vitest/results.json')
81+
expect(cachePath).toBe(path)
82+
})
83+
84+
test('use cache.dir', async () => {
85+
const { vitest, stdout, stderr } = await runVitest(
86+
{
87+
root,
88+
include: ['*.test.ts'],
89+
deps,
90+
cache: {
91+
dir: 'node_modules/.vitest-custom',
92+
},
93+
},
94+
)
95+
96+
expect(stdout).toContain('✓ basic.test.ts >')
97+
expect(stderr).toContain('"cache.dir" is deprecated')
98+
99+
const cachePath = vitest!.cache.results.getCachePath()
100+
const path = resolve(root, 'node_modules/.vitest-custom/results.json')
101+
expect(cachePath).toBe(path)
102+
})
103+
104+
test('use cacheDir', async () => {
105+
const { vitest, stdout, stderr } = await runVitest(
106+
{
107+
root,
108+
include: ['*.test.ts'],
109+
deps,
110+
},
111+
[],
112+
'test',
113+
{ cacheDir: 'node_modules/.vite-custom' },
114+
)
115+
116+
expect(stdout).toContain('✓ basic.test.ts >')
117+
expect(stderr).toBe('')
118+
119+
const cachePath = vitest!.cache.results.getCachePath()
120+
const path = resolve(root, 'node_modules/.vite-custom/vitest/results.json')
121+
expect(cachePath).toBe(path)
122+
})
123+
})

‎test/core/test/cli-test.test.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,7 @@ test('maxConcurrency is parsed correctly', () => {
213213
test('cache is parsed correctly', () => {
214214
expect(getCLIOptions('--cache')).toEqual({ cache: {} })
215215
expect(getCLIOptions('--no-cache')).toEqual({ cache: false })
216-
217-
expect(getCLIOptions('--cache.dir=./test/cache.json')).toEqual({
218-
cache: { dir: 'test/cache.json' },
219-
})
220-
expect(getCLIOptions('--cache.dir ./test/cache.json')).toEqual({
221-
cache: { dir: 'test/cache.json' },
222-
})
223-
expect(getCLIOptions('--cache.dir .\\test\\cache.json')).toEqual({
224-
cache: { dir: 'test/cache.json' },
225-
})
216+
expect(() => getCLIOptions('--cache.dir=./cache')).toThrowError('--cache.dir is deprecated')
226217
})
227218

228219
test('shuffle is parsed correctly', () => {

‎test/optimize-deps/test/ssr.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ import { importMetaUrl } from '@vitest/test-dep-url'
88
// TODO: flaky on Windows
99
// https://github.com/vitest-dev/vitest/pull/5215#discussion_r1492066033
1010
test.skipIf(process.platform === 'win32')('import.meta.url', () => {
11-
expect(importMetaUrl).toContain('/node_modules/.vitest/deps_ssr/')
11+
expect(importMetaUrl).toContain('/node_modules/.vite/vitest/deps_ssr/')
1212
})

‎test/optimize-deps/test/web.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import { expect, test } from 'vitest'
66
import { importMetaUrl } from '@vitest/test-dep-url'
77

88
test('import.meta.url', () => {
9-
expect(importMetaUrl).toContain('/node_modules/.vitest/deps/')
9+
expect(importMetaUrl).toContain('/node_modules/.vite/vitest/deps/')
1010
})

0 commit comments

Comments
 (0)
Please sign in to comment.