Skip to content

Commit

Permalink
feat(config): deprecate cache.dir option (#5229)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenghan34 authored Mar 15, 2024
1 parent 4f6e39c commit d7e8b53
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 34 deletions.
12 changes: 2 additions & 10 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1736,18 +1736,10 @@ Test above this limit will be queued to run when available slot appears.
### cache<NonProjectOption />
- **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
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/node/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions packages/vitest/src/node/cli/cli-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,14 +549,13 @@ export const cliOptionsConfig: VitestCLIOptions = {
description: 'Enable cache',
argument: '', // allow only boolean
subcommands: {
dir: {
description: 'Path to the cache directory',
argument: '<path>',
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
Expand Down
18 changes: 15 additions & 3 deletions packages/vitest/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
10 changes: 8 additions & 2 deletions packages/vitest/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

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

cache: {
/**
* @deprecated
*/
dir: string
} | false

Expand Down
5 changes: 5 additions & 0 deletions test/config/fixtures/cache/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { expect, test } from "vitest";

test('', () => {
expect(true).toBe(true)
})
123 changes: 123 additions & 0 deletions test/config/test/cache.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
11 changes: 1 addition & 10 deletions test/core/test/cli-test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/optimize-deps/test/ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/')
})
2 changes: 1 addition & 1 deletion test/optimize-deps/test/web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/')
})

0 comments on commit d7e8b53

Please sign in to comment.