Skip to content

Commit

Permalink
fix: respect cacheDir when optimizer is enabled (#6910)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Nov 15, 2024
1 parent 6324a00 commit 0b08bc1
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/vitest/src/node/config/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ export function resolveConfig(
if (resolved.cache !== false) {
let cacheDir = VitestCache.resolveCacheDir(
'',
resolve(viteConfig.cacheDir, 'vitest'),
viteConfig.cacheDir,
resolved.name,
)

Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ export async function VitestPlugin(
?? viteConfig.test?.isolate,
},
},
root: testConfig.root ?? viteConfig.test?.root,
deps: testConfig.deps ?? viteConfig.test?.deps,
},
}

Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/node/plugins/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ export function VitestOptimizer(): Plugin {
testConfig.deps?.optimizer?.web,
viteConfig.optimizeDeps,
testConfig,
viteConfig.cacheDir,
)
const ssrOptimizer = resolveOptimizerConfig(
testConfig.deps?.optimizer?.ssr,
viteConfig.ssr?.optimizeDeps,
testConfig,
viteConfig.cacheDir,
)

viteConfig.cacheDir
Expand Down
6 changes: 2 additions & 4 deletions packages/vitest/src/node/plugins/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function resolveOptimizerConfig(
_testOptions: DepsOptimizationOptions | undefined,
viteOptions: DepOptimizationOptions | undefined,
testConfig: InlineConfig,
viteCacheDir: string | undefined,
) {
const testOptions = _testOptions || {}
const newConfig: { cacheDir?: string; optimizeDeps: DepOptimizationOptions }
Expand Down Expand Up @@ -41,8 +42,6 @@ export function resolveOptimizerConfig(
}
else {
const root = testConfig.root ?? process.cwd()
const cacheDir
= testConfig.cache !== false ? testConfig.cache?.dir : undefined
const currentInclude = testOptions.include || viteOptions?.include || []
const exclude = [
'vitest',
Expand All @@ -60,8 +59,7 @@ export function resolveOptimizerConfig(
(n: string) => !exclude.includes(n),
)

newConfig.cacheDir
= cacheDir ?? VitestCache.resolveCacheDir(root, cacheDir, testConfig.name)
newConfig.cacheDir = (testConfig.cache !== false && testConfig.cache?.dir) || VitestCache.resolveCacheDir(root, viteCacheDir, testConfig.name)
newConfig.optimizeDeps = {
...viteOptions,
...testOptions,
Expand Down
8 changes: 4 additions & 4 deletions test/config/test/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('default', async () => {
expect(stderr).toBe('')

const cachePath = ctx!.cache.results.getCachePath()
const path = resolve(project, 'node_modules/.vite/vitest/results.json')
const path = resolve(project, 'node_modules/.vite/results.json')
expect(cachePath).toMatch(path)
})

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

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

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

const cachePath = ctx!.cache.results.getCachePath()
const path = resolve(project, 'node_modules/.vite/vitest/results.json')
const path = resolve(root, 'node_modules/.vite/vitest/results.json')
expect(cachePath).toBe(path)
})

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

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

0 comments on commit 0b08bc1

Please sign in to comment.