|
| 1 | +import { existsSync, readdirSync, rmSync, statSync } from 'node:fs' |
| 2 | +import { join, resolve } from 'node:path' |
| 3 | +import { expect, test } from 'vitest' |
| 4 | +import { runVitestCli } from '../../test-utils' |
| 5 | + |
| 6 | +const fixture = resolve(import.meta.dirname, '../fixtures/compile-cache') |
| 7 | +const cacheDir = resolve(fixture, 'node_modules/.cache/compile-cache-test') |
| 8 | + |
| 9 | +function cacheEntries(): string[] { |
| 10 | + return readdirSync(cacheDir, { recursive: true, encoding: 'utf-8' }) |
| 11 | + .filter(file => statSync(join(cacheDir, file)).isFile()) |
| 12 | +} |
| 13 | + |
| 14 | +test('NODE_COMPILE_CACHE redirects the compile cache and the worker persists its graph', async () => { |
| 15 | + rmSync(cacheDir, { recursive: true, force: true }) |
| 16 | + |
| 17 | + // the fixture test asserts that the worker sees this exact NODE_COMPILE_CACHE |
| 18 | + const { exitCode } = await runVitestCli( |
| 19 | + { nodeOptions: { env: { |
| 20 | + NODE_COMPILE_CACHE: cacheDir, |
| 21 | + EXPECTED_COMPILE_CACHE_DIR: cacheDir, |
| 22 | + } } }, |
| 23 | + 'run', |
| 24 | + '--root', |
| 25 | + fixture, |
| 26 | + ) |
| 27 | + |
| 28 | + expect(exitCode).toBe(0) |
| 29 | + |
| 30 | + // jsdom is only loaded inside the worker, so its modules can reach the |
| 31 | + // cache only through the worker flush â the CLI graph alone is ~150 entries, |
| 32 | + // the worker graph pushes it past 1000 |
| 33 | + expect(cacheEntries().length).toBeGreaterThan(300) |
| 34 | +}) |
| 35 | + |
| 36 | +test('NODE_DISABLE_COMPILE_CACHE wins over NODE_COMPILE_CACHE', async () => { |
| 37 | + rmSync(cacheDir, { recursive: true, force: true }) |
| 38 | + |
| 39 | + const { exitCode } = await runVitestCli( |
| 40 | + { nodeOptions: { env: { |
| 41 | + NODE_COMPILE_CACHE: cacheDir, |
| 42 | + NODE_DISABLE_COMPILE_CACHE: '1', |
| 43 | + } } }, |
| 44 | + 'run', |
| 45 | + '--root', |
| 46 | + fixture, |
| 47 | + ) |
| 48 | + |
| 49 | + expect(exitCode).toBe(0) |
| 50 | + expect(existsSync(cacheDir)).toBe(false) |
| 51 | +}) |
| 52 | + |
| 53 | +test('the cache is stripped from workers when v8 coverage is enabled', async () => { |
| 54 | + rmSync(cacheDir, { recursive: true, force: true }) |
| 55 | + |
| 56 | + const { exitCode } = await runVitestCli( |
| 57 | + { nodeOptions: { env: { |
| 58 | + NODE_COMPILE_CACHE: cacheDir, |
| 59 | + EXPECT_COVERAGE_STRIPPED: '1', |
| 60 | + } } }, |
| 61 | + 'run', |
| 62 | + '--root', |
| 63 | + fixture, |
| 64 | + '--coverage.enabled', |
| 65 | + '--coverage.provider=v8', |
| 66 | + '--coverage.reporter=text', |
| 67 | + ) |
| 68 | + |
| 69 | + expect(exitCode).toBe(0) |
| 70 | + |
| 71 | + // the CLI process itself still persists its own graph (Node enabled its |
| 72 | + // cache from the env var at startup, before coverage is known), but the |
| 73 | + // worker's jsdom graph must not reach the cache |
| 74 | + const entries = existsSync(cacheDir) ? cacheEntries() : [] |
| 75 | + expect(entries.length).toBeLessThan(300) |
| 76 | +}) |
0 commit comments