|
| 1 | +import fs from 'node:fs'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | + |
| 5 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 6 | + |
| 7 | +import { hasBaseUrlInTsconfig } from '../utils/tsconfig.js'; |
| 8 | + |
| 9 | +const tempDirs: string[] = []; |
| 10 | + |
| 11 | +function createTempDir() { |
| 12 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'vp-tsconfig-')); |
| 13 | + tempDirs.push(dir); |
| 14 | + return dir; |
| 15 | +} |
| 16 | + |
| 17 | +afterEach(() => { |
| 18 | + for (const dir of tempDirs.splice(0, tempDirs.length)) { |
| 19 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 20 | + } |
| 21 | +}); |
| 22 | + |
| 23 | +describe('hasBaseUrlInTsconfig', () => { |
| 24 | + it('detects baseUrl in JSONC tsconfig files', () => { |
| 25 | + const projectPath = createTempDir(); |
| 26 | + fs.writeFileSync( |
| 27 | + path.join(projectPath, 'tsconfig.json'), |
| 28 | + `{ |
| 29 | + "compilerOptions": { |
| 30 | + // Laravel starter tsconfig files commonly keep generated comments. |
| 31 | + "moduleResolution": "bundler", |
| 32 | + "baseUrl": ".", |
| 33 | + } |
| 34 | +} |
| 35 | +`, |
| 36 | + ); |
| 37 | + |
| 38 | + expect(hasBaseUrlInTsconfig(projectPath)).toBe(true); |
| 39 | + }); |
| 40 | + |
| 41 | + it('returns false when baseUrl is only present in a comment', () => { |
| 42 | + const projectPath = createTempDir(); |
| 43 | + fs.writeFileSync( |
| 44 | + path.join(projectPath, 'tsconfig.json'), |
| 45 | + `{ |
| 46 | + "compilerOptions": { |
| 47 | + // "baseUrl": ".", |
| 48 | + "moduleResolution": "bundler" |
| 49 | + } |
| 50 | +} |
| 51 | +`, |
| 52 | + ); |
| 53 | + |
| 54 | + expect(hasBaseUrlInTsconfig(projectPath)).toBe(false); |
| 55 | + }); |
| 56 | +}); |
0 commit comments