-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathtsconfig.spec.ts
More file actions
205 lines (179 loc) · 5.62 KB
/
tsconfig.spec.ts
File metadata and controls
205 lines (179 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { findTsconfigFiles, removeEsModuleInteropFalseFromFile } from '../tsconfig.js';
describe('findTsconfigFiles', () => {
let tmpDir: string;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tsconfig-test-'));
});
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
it('finds all tsconfig variants', () => {
fs.writeFileSync(path.join(tmpDir, 'tsconfig.json'), '{}');
fs.writeFileSync(path.join(tmpDir, 'tsconfig.app.json'), '{}');
fs.writeFileSync(path.join(tmpDir, 'tsconfig.node.json'), '{}');
fs.writeFileSync(path.join(tmpDir, 'tsconfig.build.json'), '{}');
fs.writeFileSync(path.join(tmpDir, 'other.json'), '{}');
fs.writeFileSync(path.join(tmpDir, 'package.json'), '{}');
const files = findTsconfigFiles(tmpDir);
const expected = [
path.join(tmpDir, 'tsconfig.app.json'),
path.join(tmpDir, 'tsconfig.build.json'),
path.join(tmpDir, 'tsconfig.json'),
path.join(tmpDir, 'tsconfig.node.json'),
];
expect(new Set(files)).toEqual(new Set(expected));
expect(files).toHaveLength(4);
});
it('returns empty array for non-existent directory', () => {
expect(findTsconfigFiles('/non-existent-dir-12345')).toEqual([]);
});
it('returns empty array when no tsconfig files exist', () => {
fs.writeFileSync(path.join(tmpDir, 'package.json'), '{}');
expect(findTsconfigFiles(tmpDir)).toEqual([]);
});
});
describe('removeEsModuleInteropFalseFromFile', () => {
let tmpDir: string;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tsconfig-test-'));
});
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
function writeAndRemove(filePath: string, content: string): string {
fs.writeFileSync(filePath, content);
const result = removeEsModuleInteropFalseFromFile(filePath);
expect(result).toBe(true);
return fs.readFileSync(filePath, 'utf-8');
}
it('removes esModuleInterop: false (middle property)', () => {
const filePath = path.join(tmpDir, 'tsconfig.json');
expect(
writeAndRemove(
filePath,
`{
"compilerOptions": {
"target": "ES2023",
"esModuleInterop": false,
"strict": true
}
}`,
),
).toMatchInlineSnapshot(`
"{
"compilerOptions": {
"target": "ES2023",
"strict": true
}
}"
`);
});
it('preserves comments in JSONC', () => {
const filePath = path.join(tmpDir, 'tsconfig.json');
expect(
writeAndRemove(
filePath,
`{
// This is a comment
"compilerOptions": {
"target": "ES2023",
"esModuleInterop": false,
/* block comment */
"strict": true
}
}`,
),
).toMatchInlineSnapshot(`
"{
// This is a comment
"compilerOptions": {
"target": "ES2023",
/* block comment */
"strict": true
}
}"
`);
});
it('handles esModuleInterop: false as last property', () => {
const filePath = path.join(tmpDir, 'tsconfig.json');
expect(
writeAndRemove(
filePath,
`{
"compilerOptions": {
"target": "ES2023",
"esModuleInterop": false
}
}`,
),
).toMatchInlineSnapshot(`
"{
"compilerOptions": {
"target": "ES2023"
}
}"
`);
});
it('handles inline block comment next to esModuleInterop: false', () => {
const filePath = path.join(tmpDir, 'tsconfig.json');
expect(
writeAndRemove(
filePath,
`{
"compilerOptions": {
"target": "ES2023",
"esModuleInterop": false /* reason */,
"strict": true
}
}`,
),
).toMatchInlineSnapshot(`
"{
"compilerOptions": {
"target": "ES2023" /* reason */,
"strict": true
}
}"
`);
});
it('handles compact single-line JSON', () => {
const filePath = path.join(tmpDir, 'tsconfig.json');
expect(
writeAndRemove(filePath, '{"compilerOptions":{"esModuleInterop": false, "strict": true}}'),
).toMatchInlineSnapshot(`"{"compilerOptions":{"strict": true}}"`);
});
it('handles compact single-line JSONC with spaces', () => {
const filePath = path.join(tmpDir, 'tsconfig.json');
expect(
writeAndRemove(
filePath,
'{ "compilerOptions": { "esModuleInterop": false, "strict": true } }',
),
).toMatchInlineSnapshot(`"{ "compilerOptions": {"strict": true } }"`);
});
it('leaves esModuleInterop: true untouched', () => {
const filePath = path.join(tmpDir, 'tsconfig.json');
const original = JSON.stringify({ compilerOptions: { esModuleInterop: true } }, null, 2);
fs.writeFileSync(filePath, original);
const result = removeEsModuleInteropFalseFromFile(filePath);
expect(result).toBe(false);
expect(fs.readFileSync(filePath, 'utf-8')).toBe(original);
});
it('returns false for non-existent file', () => {
expect(removeEsModuleInteropFalseFromFile('/non-existent-file.json')).toBe(false);
});
it('returns false when no compilerOptions', () => {
const filePath = path.join(tmpDir, 'tsconfig.json');
fs.writeFileSync(filePath, '{}');
expect(removeEsModuleInteropFalseFromFile(filePath)).toBe(false);
});
it('returns false when esModuleInterop is not present', () => {
const filePath = path.join(tmpDir, 'tsconfig.json');
fs.writeFileSync(filePath, JSON.stringify({ compilerOptions: { strict: true } }, null, 2));
expect(removeEsModuleInteropFalseFromFile(filePath)).toBe(false);
});
});