Skip to content

Commit 0b08bc1

Browse files
authoredNov 15, 2024··
fix: respect cacheDir when optimizer is enabled (#6910)
1 parent 6324a00 commit 0b08bc1

File tree

5 files changed

+11
-9
lines changed

5 files changed

+11
-9
lines changed
 

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ export function resolveConfig(
677677
if (resolved.cache !== false) {
678678
let cacheDir = VitestCache.resolveCacheDir(
679679
'',
680-
resolve(viteConfig.cacheDir, 'vitest'),
680+
viteConfig.cacheDir,
681681
resolved.name,
682682
)
683683

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

+2
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ export async function VitestPlugin(
142142
?? viteConfig.test?.isolate,
143143
},
144144
},
145+
root: testConfig.root ?? viteConfig.test?.root,
146+
deps: testConfig.deps ?? viteConfig.test?.deps,
145147
},
146148
}
147149

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

+2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ export function VitestOptimizer(): Plugin {
1212
testConfig.deps?.optimizer?.web,
1313
viteConfig.optimizeDeps,
1414
testConfig,
15+
viteConfig.cacheDir,
1516
)
1617
const ssrOptimizer = resolveOptimizerConfig(
1718
testConfig.deps?.optimizer?.ssr,
1819
viteConfig.ssr?.optimizeDeps,
1920
testConfig,
21+
viteConfig.cacheDir,
2022
)
2123

2224
viteConfig.cacheDir

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export function resolveOptimizerConfig(
1313
_testOptions: DepsOptimizationOptions | undefined,
1414
viteOptions: DepOptimizationOptions | undefined,
1515
testConfig: InlineConfig,
16+
viteCacheDir: string | undefined,
1617
) {
1718
const testOptions = _testOptions || {}
1819
const newConfig: { cacheDir?: string; optimizeDeps: DepOptimizationOptions }
@@ -41,8 +42,6 @@ export function resolveOptimizerConfig(
4142
}
4243
else {
4344
const root = testConfig.root ?? process.cwd()
44-
const cacheDir
45-
= testConfig.cache !== false ? testConfig.cache?.dir : undefined
4645
const currentInclude = testOptions.include || viteOptions?.include || []
4746
const exclude = [
4847
'vitest',
@@ -60,8 +59,7 @@ export function resolveOptimizerConfig(
6059
(n: string) => !exclude.includes(n),
6160
)
6261

63-
newConfig.cacheDir
64-
= cacheDir ?? VitestCache.resolveCacheDir(root, cacheDir, testConfig.name)
62+
newConfig.cacheDir = (testConfig.cache !== false && testConfig.cache?.dir) || VitestCache.resolveCacheDir(root, viteCacheDir, testConfig.name)
6563
newConfig.optimizeDeps = {
6664
...viteOptions,
6765
...testOptions,

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test('default', async () => {
1515
expect(stderr).toBe('')
1616

1717
const cachePath = ctx!.cache.results.getCachePath()
18-
const path = resolve(project, 'node_modules/.vite/vitest/results.json')
18+
const path = resolve(project, 'node_modules/.vite/results.json')
1919
expect(cachePath).toMatch(path)
2020
})
2121

@@ -53,7 +53,7 @@ test('use cacheDir', async () => {
5353
expect(stderr).toBe('')
5454

5555
const cachePath = ctx!.cache.results.getCachePath()
56-
const path = resolve(root, 'node_modules/.vite-custom/vitest/results.json')
56+
const path = resolve(root, 'node_modules/.vite-custom/results.json')
5757
expect(cachePath).toMatch(path)
5858
})
5959

@@ -77,7 +77,7 @@ describe('with optimizer enabled', () => {
7777
expect(stderr).toBe('')
7878

7979
const cachePath = ctx!.cache.results.getCachePath()
80-
const path = resolve(project, 'node_modules/.vite/vitest/results.json')
80+
const path = resolve(root, 'node_modules/.vite/vitest/results.json')
8181
expect(cachePath).toBe(path)
8282
})
8383

@@ -117,7 +117,7 @@ describe('with optimizer enabled', () => {
117117
expect(stderr).toBe('')
118118

119119
const cachePath = ctx!.cache.results.getCachePath()
120-
const path = resolve(root, 'node_modules/.vite-custom/vitest/results.json')
120+
const path = resolve(root, 'node_modules/.vite-custom/results.json')
121121
expect(cachePath).toBe(path)
122122
})
123123
})

0 commit comments

Comments
 (0)
Please sign in to comment.