Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): deprecate cache.dir option #5229

Merged
merged 19 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 2 additions & 10 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1732,18 +1732,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
6 changes: 1 addition & 5 deletions packages/vitest/src/node/cli/cli-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,11 +540,7 @@ 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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better to add a handler that throws an error? I think CLI will still propagate the value even if it's not defined in a schema

Copy link
Contributor Author

@fenghan34 fenghan34 Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I noticed that, I'll update it later

},
// cache can only be "false" or an object
transform(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 @@ -436,9 +436,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?.sequencer) {
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 @@ -612,10 +612,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 @@ -830,6 +833,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)
})
})
10 changes: 0 additions & 10 deletions test/core/test/cli-test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,6 @@ 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' },
})
})

test('typecheck correctly passes down arguments', () => {
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/')
})