-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01a23ce
commit c1bea1b
Showing
13 changed files
with
123 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import fs from 'fs' | ||
import { findUp } from 'find-up' | ||
import { resolve } from 'pathe' | ||
import { loadConfigFromFile } from 'vite' | ||
import { configFiles } from '../../constants' | ||
import type { CliOptions } from '../cli-api' | ||
import { slash } from '../../utils' | ||
|
||
export class VitestCache { | ||
static resolveCacheDir(root: string, dir: string | undefined) { | ||
return resolve(root, slash(dir || 'node_modules/.vitest')) | ||
} | ||
|
||
static async clearCache(options: CliOptions) { | ||
const root = resolve(options.root || process.cwd()) | ||
|
||
const configPath = options.config | ||
? resolve(root, options.config) | ||
: await findUp(configFiles, { cwd: root } as any) | ||
|
||
const config = await loadConfigFromFile({ command: 'serve', mode: 'test' }, configPath) | ||
|
||
if (!config) | ||
throw new Error(`[vitest] Not able to load config from ${configPath}`) | ||
|
||
const cache = config.config.test?.cache | ||
|
||
if (!cache) | ||
throw new Error('[vitest] Cache is disabled') | ||
|
||
const cachePath = VitestCache.resolveCacheDir(root, cache.dir) | ||
|
||
if (fs.existsSync(cachePath)) | ||
fs.rmSync(cachePath, { recursive: true, force: true }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"version":"0.16.0","results":[["/Users/sheremet/local-git/vitest/test/cache/test/clear-cache.test.ts",{"duration":32,"failed":false}]]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "@vitest/test-cache", | ||
"private": true, | ||
"scripts": { | ||
"test": "vitest", | ||
"coverage": "vitest run --coverage" | ||
}, | ||
"devDependencies": { | ||
"vitest": "workspace:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import fs, { promises as fsp } from 'fs' | ||
import { resolve } from 'pathe' | ||
import { describe, expect, test } from 'vitest' | ||
import { VitestCache } from '../../../packages/vitest/src/node/cache/index' | ||
|
||
const root = resolve(__dirname, '..') | ||
|
||
const pathBase = resolve(root, 'cache/.vitest-base') | ||
const pathCustom = resolve(root, 'cache/.vitest-custom') | ||
|
||
describe('vitest cache', async () => { | ||
await fsp.mkdir(pathBase, { recursive: true }) | ||
await fsp.mkdir(pathCustom, { recursive: true }) | ||
|
||
test('clears cache without specifying config path', async () => { | ||
await VitestCache.clearCache({}) | ||
|
||
expect(fs.existsSync(pathBase)).toBe(false) | ||
}) | ||
|
||
test('clears cache with specified config path', async () => { | ||
await VitestCache.clearCache({ config: 'vitest-custom.config.ts' }) | ||
|
||
expect(fs.existsSync(pathCustom)).toBe(false) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineConfig } from 'vite' | ||
|
||
export default defineConfig({ | ||
test: { | ||
cache: { | ||
dir: 'cache/.vitest-custom', | ||
}, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { defineConfig } from 'vite' | ||
|
||
export default defineConfig({ | ||
test: { | ||
threads: false, | ||
cache: { | ||
dir: 'cache/.vitest-base', | ||
}, | ||
}, | ||
}) |