|
| 1 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { afterEach, describe, expect, it } from 'vite-plus/test'; |
| 5 | +import { loadVoidConfig } from '../../src/config-loader.ts'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Coverage for the void.config.ts loader. jiti only aliases `void/config`, |
| 9 | + * so tsconfig path aliases fail to resolve — they get detected and produce |
| 10 | + * the same hint as env.ts. |
| 11 | + */ |
| 12 | + |
| 13 | +const roots: Array<string> = []; |
| 14 | + |
| 15 | +function createTempProject(config: string): string { |
| 16 | + const root = mkdtempSync(join(tmpdir(), 'void-config-loader-')); |
| 17 | + roots.push(root); |
| 18 | + writeFileSync(join(root, 'void.config.ts'), config); |
| 19 | + return root; |
| 20 | +} |
| 21 | + |
| 22 | +afterEach(() => { |
| 23 | + for (const root of roots.splice(0, roots.length)) { |
| 24 | + rmSync(root, { recursive: true, force: true }); |
| 25 | + } |
| 26 | +}); |
| 27 | + |
| 28 | +describe('loadVoidConfig', () => { |
| 29 | + it('loads void.config.ts with relative imports', () => { |
| 30 | + const root = createTempProject( |
| 31 | + 'import { defineConfig } from "void/config";\n' + |
| 32 | + 'import { SOURCE_DIR } from "./shared.ts";\n' + |
| 33 | + 'export default defineConfig({ sourceDir: SOURCE_DIR });\n', |
| 34 | + ); |
| 35 | + writeFileSync(join(root, 'shared.ts'), 'export const SOURCE_DIR = "src";\n'); |
| 36 | + |
| 37 | + expect(loadVoidConfig(root)).toEqual({ |
| 38 | + source: 'void.config.ts', |
| 39 | + value: { sourceDir: 'src' }, |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('gives a targeted hint when void.config.ts imports through a tsconfig path alias', () => { |
| 44 | + const root = createTempProject( |
| 45 | + 'import { defineConfig } from "void/config";\n' + |
| 46 | + 'import { SOURCE_DIR } from "@/lib/shared";\n' + |
| 47 | + 'export default defineConfig({ sourceDir: SOURCE_DIR });\n', |
| 48 | + ); |
| 49 | + |
| 50 | + expect(() => loadVoidConfig(root)).toThrow( |
| 51 | + /Could not load void\.config\.ts.*Hint: '@\/lib\/shared' looks like a tsconfig path alias.*use a relative import/s, |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it('does not add the alias hint for a genuine missing bare package', () => { |
| 56 | + const root = createTempProject('import "@definitely-not-installed/pkg";\nexport default {};\n'); |
| 57 | + |
| 58 | + expect(() => loadVoidConfig(root)).toThrow(/Could not load void\.config\.ts/); |
| 59 | + expect(() => loadVoidConfig(root)).not.toThrow(/tsconfig path alias/); |
| 60 | + }); |
| 61 | +}); |
0 commit comments