|
| 1 | +import fs from 'node:fs'; |
| 2 | +import { mkdtempSync } from 'node:fs'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | + |
| 6 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 7 | + |
| 8 | +import { findViteConfigUp } from '../resolve-vite-config'; |
| 9 | + |
| 10 | +describe('findViteConfigUp', () => { |
| 11 | + let tempDir: string; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + // Resolve symlinks (macOS /var -> /private/var) to match path.resolve behavior |
| 15 | + tempDir = fs.realpathSync(mkdtempSync(path.join(tmpdir(), 'vite-config-test-'))); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should find config in the start directory', () => { |
| 23 | + fs.writeFileSync(path.join(tempDir, 'vite.config.ts'), ''); |
| 24 | + const result = findViteConfigUp(tempDir, tempDir); |
| 25 | + expect(result).toBe(path.join(tempDir, 'vite.config.ts')); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should find config in a parent directory', () => { |
| 29 | + const subDir = path.join(tempDir, 'packages', 'my-lib'); |
| 30 | + fs.mkdirSync(subDir, { recursive: true }); |
| 31 | + fs.writeFileSync(path.join(tempDir, 'vite.config.ts'), ''); |
| 32 | + |
| 33 | + const result = findViteConfigUp(subDir, tempDir); |
| 34 | + expect(result).toBe(path.join(tempDir, 'vite.config.ts')); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should find config in an intermediate directory', () => { |
| 38 | + const subDir = path.join(tempDir, 'packages', 'my-lib', 'src'); |
| 39 | + fs.mkdirSync(subDir, { recursive: true }); |
| 40 | + fs.writeFileSync(path.join(tempDir, 'packages', 'vite.config.ts'), ''); |
| 41 | + |
| 42 | + const result = findViteConfigUp(subDir, tempDir); |
| 43 | + expect(result).toBe(path.join(tempDir, 'packages', 'vite.config.ts')); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should return undefined when no config exists', () => { |
| 47 | + const subDir = path.join(tempDir, 'packages', 'my-lib'); |
| 48 | + fs.mkdirSync(subDir, { recursive: true }); |
| 49 | + |
| 50 | + const result = findViteConfigUp(subDir, tempDir); |
| 51 | + expect(result).toBeUndefined(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should not traverse beyond stopDir', () => { |
| 55 | + const parentConfig = path.join(tempDir, 'vite.config.ts'); |
| 56 | + fs.writeFileSync(parentConfig, ''); |
| 57 | + const stopDir = path.join(tempDir, 'packages'); |
| 58 | + const subDir = path.join(stopDir, 'my-lib'); |
| 59 | + fs.mkdirSync(subDir, { recursive: true }); |
| 60 | + |
| 61 | + const result = findViteConfigUp(subDir, stopDir); |
| 62 | + // Should not find the config in tempDir because stopDir is packages/ |
| 63 | + expect(result).toBeUndefined(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should prefer the closest config file', () => { |
| 67 | + const subDir = path.join(tempDir, 'packages', 'my-lib'); |
| 68 | + fs.mkdirSync(subDir, { recursive: true }); |
| 69 | + fs.writeFileSync(path.join(tempDir, 'vite.config.ts'), ''); |
| 70 | + fs.writeFileSync(path.join(tempDir, 'packages', 'vite.config.ts'), ''); |
| 71 | + |
| 72 | + const result = findViteConfigUp(subDir, tempDir); |
| 73 | + expect(result).toBe(path.join(tempDir, 'packages', 'vite.config.ts')); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should find .js config files', () => { |
| 77 | + const subDir = path.join(tempDir, 'packages', 'my-lib'); |
| 78 | + fs.mkdirSync(subDir, { recursive: true }); |
| 79 | + fs.writeFileSync(path.join(tempDir, 'vite.config.js'), ''); |
| 80 | + |
| 81 | + const result = findViteConfigUp(subDir, tempDir); |
| 82 | + expect(result).toBe(path.join(tempDir, 'vite.config.js')); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should find .mts config files', () => { |
| 86 | + const subDir = path.join(tempDir, 'packages', 'my-lib'); |
| 87 | + fs.mkdirSync(subDir, { recursive: true }); |
| 88 | + fs.writeFileSync(path.join(tempDir, 'vite.config.mts'), ''); |
| 89 | + |
| 90 | + const result = findViteConfigUp(subDir, tempDir); |
| 91 | + expect(result).toBe(path.join(tempDir, 'vite.config.mts')); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should find .cjs config files', () => { |
| 95 | + const subDir = path.join(tempDir, 'packages', 'my-lib'); |
| 96 | + fs.mkdirSync(subDir, { recursive: true }); |
| 97 | + fs.writeFileSync(path.join(tempDir, 'vite.config.cjs'), ''); |
| 98 | + |
| 99 | + const result = findViteConfigUp(subDir, tempDir); |
| 100 | + expect(result).toBe(path.join(tempDir, 'vite.config.cjs')); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should find .cts config files', () => { |
| 104 | + const subDir = path.join(tempDir, 'packages', 'my-lib'); |
| 105 | + fs.mkdirSync(subDir, { recursive: true }); |
| 106 | + fs.writeFileSync(path.join(tempDir, 'vite.config.cts'), ''); |
| 107 | + |
| 108 | + const result = findViteConfigUp(subDir, tempDir); |
| 109 | + expect(result).toBe(path.join(tempDir, 'vite.config.cts')); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should find .mjs config files', () => { |
| 113 | + const subDir = path.join(tempDir, 'packages', 'my-lib'); |
| 114 | + fs.mkdirSync(subDir, { recursive: true }); |
| 115 | + fs.writeFileSync(path.join(tempDir, 'vite.config.mjs'), ''); |
| 116 | + |
| 117 | + const result = findViteConfigUp(subDir, tempDir); |
| 118 | + expect(result).toBe(path.join(tempDir, 'vite.config.mjs')); |
| 119 | + }); |
| 120 | +}); |
0 commit comments