|
| 1 | +import fs from 'node:fs'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | + |
| 5 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 6 | + |
| 7 | +import { findTsconfigFiles, removeEsModuleInteropFalseFromFile } from '../tsconfig.js'; |
| 8 | + |
| 9 | +describe('findTsconfigFiles', () => { |
| 10 | + let tmpDir: string; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tsconfig-test-')); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 18 | + }); |
| 19 | + |
| 20 | + it('finds all tsconfig variants', () => { |
| 21 | + fs.writeFileSync(path.join(tmpDir, 'tsconfig.json'), '{}'); |
| 22 | + fs.writeFileSync(path.join(tmpDir, 'tsconfig.app.json'), '{}'); |
| 23 | + fs.writeFileSync(path.join(tmpDir, 'tsconfig.node.json'), '{}'); |
| 24 | + fs.writeFileSync(path.join(tmpDir, 'tsconfig.build.json'), '{}'); |
| 25 | + fs.writeFileSync(path.join(tmpDir, 'other.json'), '{}'); |
| 26 | + fs.writeFileSync(path.join(tmpDir, 'package.json'), '{}'); |
| 27 | + |
| 28 | + const files = findTsconfigFiles(tmpDir); |
| 29 | + const expected = [ |
| 30 | + path.join(tmpDir, 'tsconfig.app.json'), |
| 31 | + path.join(tmpDir, 'tsconfig.build.json'), |
| 32 | + path.join(tmpDir, 'tsconfig.json'), |
| 33 | + path.join(tmpDir, 'tsconfig.node.json'), |
| 34 | + ]; |
| 35 | + expect(new Set(files)).toEqual(new Set(expected)); |
| 36 | + expect(files).toHaveLength(4); |
| 37 | + }); |
| 38 | + |
| 39 | + it('returns empty array for non-existent directory', () => { |
| 40 | + expect(findTsconfigFiles('/non-existent-dir-12345')).toEqual([]); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns empty array when no tsconfig files exist', () => { |
| 44 | + fs.writeFileSync(path.join(tmpDir, 'package.json'), '{}'); |
| 45 | + expect(findTsconfigFiles(tmpDir)).toEqual([]); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe('removeEsModuleInteropFalseFromFile', () => { |
| 50 | + let tmpDir: string; |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tsconfig-test-')); |
| 54 | + }); |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('removes esModuleInterop: false', () => { |
| 61 | + const filePath = path.join(tmpDir, 'tsconfig.json'); |
| 62 | + fs.writeFileSync( |
| 63 | + filePath, |
| 64 | + JSON.stringify( |
| 65 | + { |
| 66 | + compilerOptions: { |
| 67 | + target: 'ES2023', |
| 68 | + esModuleInterop: false, |
| 69 | + strict: true, |
| 70 | + }, |
| 71 | + }, |
| 72 | + null, |
| 73 | + 2, |
| 74 | + ), |
| 75 | + ); |
| 76 | + |
| 77 | + const result = removeEsModuleInteropFalseFromFile(filePath); |
| 78 | + expect(result).toBe(true); |
| 79 | + |
| 80 | + const content = JSON.parse(fs.readFileSync(filePath, 'utf-8')); |
| 81 | + expect(content.compilerOptions).not.toHaveProperty('esModuleInterop'); |
| 82 | + expect(content.compilerOptions.target).toBe('ES2023'); |
| 83 | + expect(content.compilerOptions.strict).toBe(true); |
| 84 | + }); |
| 85 | + |
| 86 | + it('preserves comments in JSONC', () => { |
| 87 | + const filePath = path.join(tmpDir, 'tsconfig.json'); |
| 88 | + const content = `{ |
| 89 | + // This is a comment |
| 90 | + "compilerOptions": { |
| 91 | + "target": "ES2023", |
| 92 | + "esModuleInterop": false, |
| 93 | + /* block comment */ |
| 94 | + "strict": true |
| 95 | + } |
| 96 | +} |
| 97 | +`; |
| 98 | + fs.writeFileSync(filePath, content); |
| 99 | + |
| 100 | + const result = removeEsModuleInteropFalseFromFile(filePath); |
| 101 | + expect(result).toBe(true); |
| 102 | + |
| 103 | + const newContent = fs.readFileSync(filePath, 'utf-8'); |
| 104 | + expect(newContent).toContain('// This is a comment'); |
| 105 | + expect(newContent).toContain('/* block comment */'); |
| 106 | + expect(newContent).not.toContain('esModuleInterop'); |
| 107 | + expect(newContent).toContain('"strict": true'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('handles esModuleInterop: false as last property (trailing comma on previous line is valid JSONC)', () => { |
| 111 | + const filePath = path.join(tmpDir, 'tsconfig.json'); |
| 112 | + const content = `{ |
| 113 | + "compilerOptions": { |
| 114 | + "target": "ES2023", |
| 115 | + "esModuleInterop": false |
| 116 | + } |
| 117 | +} |
| 118 | +`; |
| 119 | + fs.writeFileSync(filePath, content); |
| 120 | + |
| 121 | + const result = removeEsModuleInteropFalseFromFile(filePath); |
| 122 | + expect(result).toBe(true); |
| 123 | + |
| 124 | + const newContent = fs.readFileSync(filePath, 'utf-8'); |
| 125 | + expect(newContent).not.toContain('esModuleInterop'); |
| 126 | + expect(newContent).toContain('"target": "ES2023"'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('leaves esModuleInterop: true untouched', () => { |
| 130 | + const filePath = path.join(tmpDir, 'tsconfig.json'); |
| 131 | + const original = JSON.stringify({ compilerOptions: { esModuleInterop: true } }, null, 2); |
| 132 | + fs.writeFileSync(filePath, original); |
| 133 | + |
| 134 | + const result = removeEsModuleInteropFalseFromFile(filePath); |
| 135 | + expect(result).toBe(false); |
| 136 | + expect(fs.readFileSync(filePath, 'utf-8')).toBe(original); |
| 137 | + }); |
| 138 | + |
| 139 | + it('returns false for non-existent file', () => { |
| 140 | + expect(removeEsModuleInteropFalseFromFile('/non-existent-file.json')).toBe(false); |
| 141 | + }); |
| 142 | + |
| 143 | + it('returns false when no compilerOptions', () => { |
| 144 | + const filePath = path.join(tmpDir, 'tsconfig.json'); |
| 145 | + fs.writeFileSync(filePath, '{}'); |
| 146 | + |
| 147 | + expect(removeEsModuleInteropFalseFromFile(filePath)).toBe(false); |
| 148 | + }); |
| 149 | + |
| 150 | + it('returns false when esModuleInterop is not present', () => { |
| 151 | + const filePath = path.join(tmpDir, 'tsconfig.json'); |
| 152 | + fs.writeFileSync(filePath, JSON.stringify({ compilerOptions: { strict: true } }, null, 2)); |
| 153 | + |
| 154 | + expect(removeEsModuleInteropFalseFromFile(filePath)).toBe(false); |
| 155 | + }); |
| 156 | +}); |
0 commit comments