|
1 | | -import { expect, test } from 'vitest' |
2 | | -import { runInlineTests, runVitest, useFS } from '../../test-utils' |
| 1 | +import { existsSync } from 'node:fs' |
| 2 | +import { join } from 'pathe' |
| 3 | +import { expect, onTestFinished, test, vi } from 'vitest' |
| 4 | +import { runInlineTests, runVitest, runVitestCli, useFS, useTmpFS } from '../../test-utils' |
3 | 5 |
|
4 | 6 | test("if file has import.meta.glob, it's not cached", async () => { |
5 | 7 | const { createFile } = useFS( |
@@ -602,6 +604,63 @@ test('fsModuleCache does not fail the run when the cache directory cannot be cre |
602 | 604 | expect(stderr.match(/could not be created/g)).toHaveLength(1) |
603 | 605 | }) |
604 | 606 |
|
| 607 | +test('VITEST_FS_MODULE_CACHE=false disables the cache when the config does not set it', async () => { |
| 608 | + const args = [ |
| 609 | + 'run', |
| 610 | + '--root', |
| 611 | + './fixtures/caching/unwritable-cache', |
| 612 | + '--fsModuleCachePath', |
| 613 | + './not-a-directory/cache', |
| 614 | + ] |
| 615 | + const env = { VITEST_FS_MODULE_CACHE: 'false' } |
| 616 | + |
| 617 | + const disabled = await runVitestCli({ nodeOptions: { env } }, ...args) |
| 618 | + expect(disabled.stderr).toBe('') |
| 619 | + expect(disabled.exitCode).toBe(0) |
| 620 | + |
| 621 | + // the CLI option has priority over the environment |
| 622 | + const enabled = await runVitestCli({ nodeOptions: { env } }, ...args, '--fsModuleCache') |
| 623 | + expect(enabled.stderr.match(/could not be created/g)).toHaveLength(1) |
| 624 | + expect(enabled.exitCode).toBe(0) |
| 625 | +}) |
| 626 | + |
| 627 | +test('VITEST_FS_MODULE_CACHE_PATH moves the cache when the config does not set it', async () => { |
| 628 | + vi.stubEnv('VITEST_FS_MODULE_CACHE_PATH', './custom-cache') |
| 629 | + onTestFinished(() => { |
| 630 | + vi.unstubAllEnvs() |
| 631 | + }) |
| 632 | + const structure = () => ({ |
| 633 | + 'sum.js': `export const sum = (a, b) => a + b`, |
| 634 | + 'sum.test.js': /* js */ ` |
| 635 | + import { expect, test } from "vitest" |
| 636 | + import { sum } from "./sum.js" |
| 637 | + test("adds", () => { |
| 638 | + expect(sum(1, 2)).toBe(3) |
| 639 | + }) |
| 640 | + `, |
| 641 | + }) |
| 642 | + |
| 643 | + const fromEnv = useTmpFS(structure()) |
| 644 | + const fromEnvRun = await runVitest({ root: fromEnv.root }) |
| 645 | + expect(fromEnvRun.stderr).toBe('') |
| 646 | + expect(existsSync(join(fromEnv.root, 'custom-cache/CACHEDIR.TAG'))).toBe(true) |
| 647 | + |
| 648 | + const fromConfig = useTmpFS({ |
| 649 | + ...structure(), |
| 650 | + 'vitest.config.js': /* js */ ` |
| 651 | + export default { |
| 652 | + test: { |
| 653 | + fsModuleCachePath: './node_modules/.vitest-fs-cache', |
| 654 | + }, |
| 655 | + } |
| 656 | + `, |
| 657 | + }) |
| 658 | + const fromConfigRun = await runVitest({ root: fromConfig.root }) |
| 659 | + expect(fromConfigRun.stderr).toBe('') |
| 660 | + expect(existsSync(join(fromConfig.root, 'custom-cache'))).toBe(false) |
| 661 | + expect(existsSync(join(fromConfig.root, 'node_modules/.vitest-fs-cache/CACHEDIR.TAG'))).toBe(true) |
| 662 | +}) |
| 663 | + |
605 | 664 | declare module 'vitest' { |
606 | 665 | export interface ProvidedContext { |
607 | 666 | generated: string[] |
|
0 commit comments