From 0b08bc11c42f57fc589760f226ce840837fe2f7a Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 15 Nov 2024 19:03:45 +0900 Subject: [PATCH] fix: respect `cacheDir` when optimizer is enabled (#6910) --- packages/vitest/src/node/config/resolveConfig.ts | 2 +- packages/vitest/src/node/plugins/index.ts | 2 ++ packages/vitest/src/node/plugins/optimizer.ts | 2 ++ packages/vitest/src/node/plugins/utils.ts | 6 ++---- test/config/test/cache.test.ts | 8 ++++---- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/vitest/src/node/config/resolveConfig.ts b/packages/vitest/src/node/config/resolveConfig.ts index b83426ad761c..b35c5641b048 100644 --- a/packages/vitest/src/node/config/resolveConfig.ts +++ b/packages/vitest/src/node/config/resolveConfig.ts @@ -677,7 +677,7 @@ export function resolveConfig( if (resolved.cache !== false) { let cacheDir = VitestCache.resolveCacheDir( '', - resolve(viteConfig.cacheDir, 'vitest'), + viteConfig.cacheDir, resolved.name, ) diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index ae0c26ea824a..918bedf90097 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -142,6 +142,8 @@ export async function VitestPlugin( ?? viteConfig.test?.isolate, }, }, + root: testConfig.root ?? viteConfig.test?.root, + deps: testConfig.deps ?? viteConfig.test?.deps, }, } diff --git a/packages/vitest/src/node/plugins/optimizer.ts b/packages/vitest/src/node/plugins/optimizer.ts index f75153887d88..5077327ca8ee 100644 --- a/packages/vitest/src/node/plugins/optimizer.ts +++ b/packages/vitest/src/node/plugins/optimizer.ts @@ -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 diff --git a/packages/vitest/src/node/plugins/utils.ts b/packages/vitest/src/node/plugins/utils.ts index a17458c6b64b..ffbc55cf2995 100644 --- a/packages/vitest/src/node/plugins/utils.ts +++ b/packages/vitest/src/node/plugins/utils.ts @@ -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 } @@ -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', @@ -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, diff --git a/test/config/test/cache.test.ts b/test/config/test/cache.test.ts index 2d814ae04d87..d2e4ff7581e2 100644 --- a/test/config/test/cache.test.ts +++ b/test/config/test/cache.test.ts @@ -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) }) @@ -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) }) @@ -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) }) @@ -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) }) })