From d7e8b53ed057b2127d8073b11f5f2732354bdd7c Mon Sep 17 00:00:00 2001 From: Han Date: Fri, 15 Mar 2024 17:37:58 +0800 Subject: [PATCH] feat(config): deprecate `cache.dir` option (#5229) --- docs/config/index.md | 12 +- packages/vitest/src/node/cache/index.ts | 4 +- packages/vitest/src/node/cli/cli-config.ts | 9 +- packages/vitest/src/node/config.ts | 18 ++- packages/vitest/src/types/config.ts | 10 +- test/config/fixtures/cache/basic.test.ts | 5 + test/config/test/cache.test.ts | 123 +++++++++++++++++++++ test/core/test/cli-test.test.ts | 11 +- test/optimize-deps/test/ssr.test.ts | 2 +- test/optimize-deps/test/web.test.ts | 2 +- 10 files changed, 162 insertions(+), 34 deletions(-) create mode 100644 test/config/fixtures/cache/basic.test.ts create mode 100644 test/config/test/cache.test.ts diff --git a/docs/config/index.md b/docs/config/index.md index 61a6a3f67737..5c9c75c87be6 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -1736,18 +1736,10 @@ Test above this limit will be queued to run when available slot appears. ### cache -- **Type**: `false | { dir? }` +- **Type**: `false` - **CLI**: `--no-cache`, `--cache=false` -Options to configure Vitest cache policy. At the moment Vitest stores cache for test results to run the longer and failed tests first. - -#### cache.dir - -- **Type**: `string` -- **Default**: `node_modules/.vitest` -- **CLI**: `--cache.dir=./cache` - -Path to cache directory. +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. ### sequence diff --git a/packages/vitest/src/node/cache/index.ts b/packages/vitest/src/node/cache/index.ts index 6783736ba23a..209722e78862 100644 --- a/packages/vitest/src/node/cache/index.ts +++ b/packages/vitest/src/node/cache/index.ts @@ -21,8 +21,8 @@ export class VitestCache { return this.stats.getStats(key) } - static resolveCacheDir(root: string, dir: string | undefined, projectName: string | undefined) { - const baseDir = slash(dir || 'node_modules/.vitest') + static resolveCacheDir(root: string, dir?: string, projectName?: string) { + const baseDir = slash(dir || 'node_modules/.vite/vitest') return projectName ? resolve(root, baseDir, crypto.createHash('md5').update(projectName, 'utf-8').digest('hex')) : resolve(root, baseDir) diff --git a/packages/vitest/src/node/cli/cli-config.ts b/packages/vitest/src/node/cli/cli-config.ts index 622fb13f50a7..b5e16b9f526c 100644 --- a/packages/vitest/src/node/cli/cli-config.ts +++ b/packages/vitest/src/node/cli/cli-config.ts @@ -549,14 +549,13 @@ export const cliOptionsConfig: VitestCLIOptions = { description: 'Enable cache', argument: '', // allow only boolean subcommands: { - dir: { - description: 'Path to the cache directory', - argument: '', - normalize: true, - }, + dir: null, }, + default: true, // cache can only be "false" or an object transform(cache) { + if (typeof cache !== 'boolean' && cache) + throw new Error('--cache.dir is deprecated') if (cache) return {} return cache diff --git a/packages/vitest/src/node/config.ts b/packages/vitest/src/node/config.ts index a6d3b3810a85..9d1c96a18fa3 100644 --- a/packages/vitest/src/node/config.ts +++ b/packages/vitest/src/node/config.ts @@ -446,9 +446,21 @@ export function resolveConfig( resolved.css.modules.classNameStrategy ??= 'stable' } - resolved.cache ??= { dir: '' } - if (resolved.cache) - resolved.cache.dir = VitestCache.resolveCacheDir(resolved.root, resolved.cache.dir, resolved.name) + if (resolved.cache !== false) { + let cacheDir = VitestCache.resolveCacheDir('', resolve(viteConfig.cacheDir, 'vitest'), resolved.name) + + if (resolved.cache && resolved.cache.dir) { + 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) + } + + resolved.cache = { dir: cacheDir } + } resolved.sequence ??= {} as any if (resolved.sequence.shuffle && typeof resolved.sequence.shuffle === 'object') { diff --git a/packages/vitest/src/types/config.ts b/packages/vitest/src/types/config.ts index 3dc2d42ef633..f75c660558c1 100644 --- a/packages/vitest/src/types/config.ts +++ b/packages/vitest/src/types/config.ts @@ -624,10 +624,13 @@ export interface InlineConfig { /** * Options for configuring cache policy. - * @default { dir: 'node_modules/.vitest' } + * @default { dir: 'node_modules/.vite/vitest' } */ cache?: false | { - dir?: string + /** + * @deprecated Use Vite's "cacheDir" instead if you want to change the cache director. Note caches will be written to "cacheDir\/vitest". + */ + dir: string } /** @@ -854,6 +857,9 @@ export interface ResolvedConfig extends Omit, 'config' | 'f } cache: { + /** + * @deprecated + */ dir: string } | false diff --git a/test/config/fixtures/cache/basic.test.ts b/test/config/fixtures/cache/basic.test.ts new file mode 100644 index 000000000000..36c7cc207dde --- /dev/null +++ b/test/config/fixtures/cache/basic.test.ts @@ -0,0 +1,5 @@ +import { expect, test } from "vitest"; + +test('', () => { + expect(true).toBe(true) +}) diff --git a/test/config/test/cache.test.ts b/test/config/test/cache.test.ts new file mode 100644 index 000000000000..379ddbc93d38 --- /dev/null +++ b/test/config/test/cache.test.ts @@ -0,0 +1,123 @@ +import { describe, expect, test } from 'vitest' +import { resolve } from 'pathe' +import { runVitest } from '../../test-utils' + +const root = resolve(__dirname, '../fixtures/cache') +const project = resolve(__dirname, '../') + +test('default', async () => { + const { vitest, stdout, stderr } = await runVitest({ + root, + include: ['*.test.ts'], + }) + + expect(stdout).toContain('✓ basic.test.ts >') + expect(stderr).toBe('') + + const cachePath = vitest!.cache.results.getCachePath() + const path = resolve(project, 'node_modules/.vite/vitest/results.json') + expect(cachePath).toMatch(path) +}) + +test('use cache.dir', async () => { + const { vitest, stdout, stderr } = await runVitest( + { + root, + include: ['*.test.ts'], + cache: { + dir: 'node_modules/.vitest-custom', + }, + }, + ) + + expect(stdout).toContain('✓ basic.test.ts >') + expect(stderr).toContain('"cache.dir" is deprecated') + + const cachePath = vitest!.cache.results.getCachePath() + const path = resolve(root, 'node_modules/.vitest-custom/results.json') + expect(cachePath).toMatch(path) +}) + +test('use cacheDir', async () => { + const { vitest, stdout, stderr } = await runVitest( + { + root, + include: ['*.test.ts'], + }, + [], + 'test', + { cacheDir: 'node_modules/.vite-custom' }, + ) + + expect(stdout).toContain('✓ basic.test.ts >') + expect(stderr).toBe('') + + const cachePath = vitest!.cache.results.getCachePath() + const path = resolve(root, 'node_modules/.vite-custom/vitest/results.json') + expect(cachePath).toMatch(path) +}) + +describe('with optimizer enabled', () => { + const deps = { + optimizer: { + web: { + enabled: true, + }, + }, + } + + test('default', async () => { + const { vitest, stdout, stderr } = await runVitest({ + root, + include: ['*.test.ts'], + deps, + }) + + expect(stdout).toContain('✓ basic.test.ts >') + expect(stderr).toBe('') + + const cachePath = vitest!.cache.results.getCachePath() + const path = resolve(project, 'node_modules/.vite/vitest/results.json') + expect(cachePath).toBe(path) + }) + + test('use cache.dir', async () => { + const { vitest, stdout, stderr } = await runVitest( + { + root, + include: ['*.test.ts'], + deps, + cache: { + dir: 'node_modules/.vitest-custom', + }, + }, + ) + + expect(stdout).toContain('✓ basic.test.ts >') + expect(stderr).toContain('"cache.dir" is deprecated') + + const cachePath = vitest!.cache.results.getCachePath() + const path = resolve(root, 'node_modules/.vitest-custom/results.json') + expect(cachePath).toBe(path) + }) + + test('use cacheDir', async () => { + const { vitest, stdout, stderr } = await runVitest( + { + root, + include: ['*.test.ts'], + deps, + }, + [], + 'test', + { cacheDir: 'node_modules/.vite-custom' }, + ) + + expect(stdout).toContain('✓ basic.test.ts >') + expect(stderr).toBe('') + + const cachePath = vitest!.cache.results.getCachePath() + const path = resolve(root, 'node_modules/.vite-custom/vitest/results.json') + expect(cachePath).toBe(path) + }) +}) diff --git a/test/core/test/cli-test.test.ts b/test/core/test/cli-test.test.ts index 90f5123799d4..1d4a3ce636cf 100644 --- a/test/core/test/cli-test.test.ts +++ b/test/core/test/cli-test.test.ts @@ -213,16 +213,7 @@ test('maxConcurrency is parsed correctly', () => { test('cache is parsed correctly', () => { expect(getCLIOptions('--cache')).toEqual({ cache: {} }) expect(getCLIOptions('--no-cache')).toEqual({ cache: false }) - - expect(getCLIOptions('--cache.dir=./test/cache.json')).toEqual({ - cache: { dir: 'test/cache.json' }, - }) - expect(getCLIOptions('--cache.dir ./test/cache.json')).toEqual({ - cache: { dir: 'test/cache.json' }, - }) - expect(getCLIOptions('--cache.dir .\\test\\cache.json')).toEqual({ - cache: { dir: 'test/cache.json' }, - }) + expect(() => getCLIOptions('--cache.dir=./cache')).toThrowError('--cache.dir is deprecated') }) test('shuffle is parsed correctly', () => { diff --git a/test/optimize-deps/test/ssr.test.ts b/test/optimize-deps/test/ssr.test.ts index dc59d558b84b..38d519ecad22 100644 --- a/test/optimize-deps/test/ssr.test.ts +++ b/test/optimize-deps/test/ssr.test.ts @@ -8,5 +8,5 @@ import { importMetaUrl } from '@vitest/test-dep-url' // 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/.vitest/deps_ssr/') + expect(importMetaUrl).toContain('/node_modules/.vite/vitest/deps_ssr/') }) diff --git a/test/optimize-deps/test/web.test.ts b/test/optimize-deps/test/web.test.ts index 308e7730640f..88af98f6f4af 100644 --- a/test/optimize-deps/test/web.test.ts +++ b/test/optimize-deps/test/web.test.ts @@ -6,5 +6,5 @@ import { expect, test } from 'vitest' import { importMetaUrl } from '@vitest/test-dep-url' test('import.meta.url', () => { - expect(importMetaUrl).toContain('/node_modules/.vitest/deps/') + expect(importMetaUrl).toContain('/node_modules/.vite/vitest/deps/') })