/
esbuild-plugin-typecheck.test.ts
353 lines (301 loc) · 12.1 KB
/
esbuild-plugin-typecheck.test.ts
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import { node } from 'execa';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
const xit = process.env.CI ? it.skip : it;
async function* walk(dirPath: string): AsyncIterable<string> {
for await (const d of await fs.promises.readdir(dirPath, { withFileTypes: true })) {
const entry = path.join(dirPath, d.name);
if (d.isDirectory()) yield* walk(entry);
else if (d.isFile()) yield entry;
}
}
interface RunOptions {
buildMode?: 'readonly' | 'write-output';
watch?: boolean;
}
function setup(relFixtureDirSrc: string) {
const fixtureDirSrc = path.join(__dirname, relFixtureDirSrc);
const fixtureDirOut = fs.mkdtempSync(path.join(__dirname, 'tmp-'));
async function copySrcFile(src: string, out: string) {
await fs.promises.copyFile(path.join(fixtureDirSrc, src), path.join(fixtureDirOut, out));
}
function copySrcFileSync(src: string, out: string) {
fs.copyFileSync(path.join(fixtureDirSrc, src), path.join(fixtureDirOut, out));
}
async function init() {
for await (const src of walk(fixtureDirSrc)) {
const out = path.join(fixtureDirOut, path.relative(fixtureDirSrc, src));
await fs.promises.mkdir(path.dirname(out), { recursive: true });
await fs.promises.copyFile(src, out);
}
}
async function cleanup() {
await fs.promises.rm(fixtureDirOut, { recursive: true });
}
async function run(
script: string,
copyQueue: [string, string][],
{ buildMode, watch }: RunOptions = {},
) {
const queue = [...copyQueue];
if (!watch) {
await Promise.all(queue.map(files => copySrcFile(...files)));
}
const scriptPath = path.join(fixtureDirOut, script);
const proc = node(scriptPath, {
all: true,
encoding: 'utf8',
reject: false,
cwd: path.dirname(scriptPath),
env: {
...process.env,
NO_COLOR: '1',
BUILD_MODE: buildMode,
WATCH: watch ? 'true' : undefined,
},
});
if (watch) {
proc.all!.on('data', (chunk: Buffer) => {
const str = chunk.toString();
if (/Typecheck finished in/.exec(str)) {
const files = queue.shift();
if (files) {
setTimeout(() => copySrcFileSync(...files), 300);
} else {
proc.cancel();
}
}
});
}
const { exitCode, all = '' } = await proc;
const output = all
.split(os.EOL)
.map(line => line.trimEnd())
.filter(Boolean)
.filter(l => !l.includes('Typecheck started'))
.map(l => l.replace(/Typecheck finished in (\d+ms)/, 'Typecheck finished in TIME'));
return { code: exitCode, output };
}
async function findTSOutput() {
const tsOutput: string[] = [];
for await (const file of walk(fixtureDirOut)) {
const rel = path.relative(fixtureDirOut, file);
const parts = rel.split(path.sep);
if (parts.includes('types') || parts.includes('build')) {
tsOutput.push(rel);
}
}
return tsOutput;
}
return { cleanup, init, run, findTSOutput };
}
describe('eslint-plugin-typecheck', () => {
describe('compile, once', () => {
const build = setup('fixture/compile');
beforeEach(async () => await build.init());
afterEach(async () => await build.cleanup());
it('completes successfully', async () => {
const { code, output } = await build.run('build.js', [['src/index.ts', 'src/index.ts']]);
expect(code).toBe(0);
expect(output).toEqual(['✔ Typecheck passed', 'ℹ Typecheck finished in TIME']);
});
it('reports errors', async () => {
const { code, output } = await build.run('build.js', [
['src/index-error.ts', 'src/index.ts'],
]);
expect(code).toBe(1);
expect(output).toEqual([
"src/index.ts(13,25): error TS2304: Cannot find name 'sourcePath'.",
'✖ Typecheck failed with 1 error',
'ℹ Typecheck finished in TIME',
]);
});
});
describe('compile, watch', () => {
const build = setup('fixture/compile');
beforeEach(async () => await build.init());
afterEach(async () => await build.cleanup());
it('watches for changes', async () => {
const { output } = await build.run(
'build.js',
[
['src/index-error.ts', 'src/index.ts'],
['src/index.ts', 'src/index.ts'],
['src/index-error.ts', 'src/index.ts'],
],
{ watch: true },
);
expect(output).toEqual([
'✔ Typecheck passed',
'ℹ Typecheck finished in TIME',
"src/index.ts(13,25): error TS2304: Cannot find name 'sourcePath'.",
'✖ Typecheck failed with 1 error',
'ℹ Typecheck finished in TIME',
'✔ Typecheck passed',
'ℹ Typecheck finished in TIME',
"src/index.ts(13,25): error TS2304: Cannot find name 'sourcePath'.",
'✖ Typecheck failed with 1 error',
'ℹ Typecheck finished in TIME',
]);
});
});
describe('build, once', () => {
const build = setup('fixture/build');
beforeEach(async () => await build.init());
afterEach(async () => await build.cleanup());
it('produces no output by default', async () => {
const { code, output } = await build.run('pkg-three/build.js', []);
expect(code).toBe(0);
expect(output).toEqual(['✔ Typecheck passed', 'ℹ Typecheck finished in TIME']);
await expect(build.findTSOutput()).resolves.toEqual([]);
});
it('reports errors across all dependencies', async () => {
const { code, output } = await build.run('pkg-three/build.js', [
['pkg-one/one-error.ts', 'pkg-one/one.ts'],
['pkg-two/two-error.ts', 'pkg-two/two.ts'],
]);
expect(code).toBe(1);
expect(output).toEqual([
"../pkg-one/one.ts(7,33): error TS2504: Type 'AsyncIterator<string, any, undefined>' must have a '[Symbol.asyncIterator]()' method that returns an async iterator.",
"../pkg-two/two.ts(8,33): error TS2504: Type 'AsyncIterator<string, any, undefined>' must have a '[Symbol.asyncIterator]()' method that returns an async iterator.",
'✖ Typecheck failed with 2 errors',
'ℹ Typecheck finished in TIME',
]);
await expect(build.findTSOutput()).resolves.toEqual([]);
});
it('reports errors in entry point', async () => {
const { code, output } = await build.run('pkg-three/build.js', [
['pkg-three/three-error.ts', 'pkg-three/three.ts'],
]);
expect(code).toBe(1);
expect(output).toEqual([
"three.ts(13,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.",
" Type 'undefined' is not assignable to type 'boolean'.",
"three.ts(18,7): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.",
'✖ Typecheck failed with 2 errors',
'ℹ Typecheck finished in TIME',
]);
await expect(build.findTSOutput()).resolves.toEqual([]);
});
it('writes output when buildMode is write-output', async () => {
const { code, output } = await build.run('pkg-three/build.js', [], {
buildMode: 'write-output',
});
expect(code).toBe(0);
expect(output).toEqual(['✔ Typecheck passed', 'ℹ Typecheck finished in TIME']);
await expect(build.findTSOutput()).resolves.toEqual([
'pkg-one/build/one.js',
'pkg-one/build/tsconfig.tsbuildinfo',
'pkg-one/types/one.d.ts',
'pkg-three/build/three.js',
'pkg-three/build/tsconfig.tsbuildinfo',
'pkg-three/types/three.d.ts',
'pkg-two/build/tsconfig.tsbuildinfo',
'pkg-two/build/two.js',
'pkg-two/types/two.d.ts',
]);
});
});
describe('build, watch', () => {
const build = setup('fixture/build');
beforeEach(async () => await build.init());
afterEach(async () => await build.cleanup());
it('produces no output by default', async () => {
const { output } = await build.run('pkg-three/build.js', [], { watch: true });
try {
expect(output).toEqual(['✔ Typecheck passed', 'ℹ Typecheck finished in TIME']);
} catch {
// TODO: remove after https://github.com/microsoft/TypeScript/pull/51626 is released
expect(output).toEqual([
'✔ Typecheck passed',
'ℹ Typecheck finished in TIME',
'✔ Typecheck passed',
'ℹ Typecheck finished in TIME',
]);
}
await expect(build.findTSOutput()).resolves.toEqual([]);
});
xit('reports errors across all dependencies [skip CI]', async () => {
const { output } = await build.run(
'pkg-three/build.js',
[
['pkg-one/one-error.ts', 'pkg-one/one.ts'],
['pkg-two/two-error.ts', 'pkg-two/two.ts'],
['pkg-one/one.ts', 'pkg-one/one.ts'],
['pkg-two/two.ts', 'pkg-two/two.ts'],
],
{ buildMode: 'write-output', watch: true },
);
expect(output).toEqual([
'✔ Typecheck passed',
'ℹ Typecheck finished in TIME',
// one-error
"../pkg-one/one.ts(7,33): error TS2504: Type 'AsyncIterator<string, any, undefined>' must have a '[Symbol.asyncIterator]()' method that returns an async iterator.",
'✖ Typecheck failed with 1 error',
'ℹ Typecheck finished in TIME',
// TODO: remove after https://github.com/microsoft/TypeScript/pull/51626 is released
"../pkg-one/one.ts(7,33): error TS2504: Type 'AsyncIterator<string, any, undefined>' must have a '[Symbol.asyncIterator]()' method that returns an async iterator.",
'✖ Typecheck failed with 1 error',
'ℹ Typecheck finished in TIME',
// /TODO
// two-error
"../pkg-two/two.ts(8,33): error TS2504: Type 'AsyncIterator<string, any, undefined>' must have a '[Symbol.asyncIterator]()' method that returns an async iterator.",
"../pkg-one/one.ts(7,33): error TS2504: Type 'AsyncIterator<string, any, undefined>' must have a '[Symbol.asyncIterator]()' method that returns an async iterator.",
'✖ Typecheck failed with 2 errors',
'ℹ Typecheck finished in TIME',
// one
"../pkg-two/two.ts(8,33): error TS2504: Type 'AsyncIterator<string, any, undefined>' must have a '[Symbol.asyncIterator]()' method that returns an async iterator.",
'✖ Typecheck failed with 1 error',
'ℹ Typecheck finished in TIME',
// two
'✔ Typecheck passed',
'ℹ Typecheck finished in TIME',
// TODO: remove after https://github.com/microsoft/TypeScript/pull/51626 is released
'✔ Typecheck passed',
'ℹ Typecheck finished in TIME',
// /TODO
]);
await expect(build.findTSOutput()).resolves.toEqual([
'pkg-one/build/one.js',
'pkg-one/build/tsconfig.tsbuildinfo',
'pkg-one/types/one.d.ts',
'pkg-three/build/three.js',
'pkg-three/build/tsconfig.tsbuildinfo',
'pkg-three/types/three.d.ts',
'pkg-two/build/tsconfig.tsbuildinfo',
'pkg-two/build/two.js',
'pkg-two/types/two.d.ts',
]);
}, 20000);
it('writes output when buildMode is write-output', async () => {
const { output } = await build.run('pkg-three/build.js', [], {
buildMode: 'write-output',
watch: true,
});
try {
expect(output).toEqual(['✔ Typecheck passed', 'ℹ Typecheck finished in TIME']);
} catch {
// TODO: remove after https://github.com/microsoft/TypeScript/pull/51626 is released
expect(output).toEqual([
'✔ Typecheck passed',
'ℹ Typecheck finished in TIME',
'✔ Typecheck passed',
'ℹ Typecheck finished in TIME',
]);
}
await expect(build.findTSOutput()).resolves.toEqual([
'pkg-one/build/one.js',
'pkg-one/build/tsconfig.tsbuildinfo',
'pkg-one/types/one.d.ts',
'pkg-three/build/three.js',
'pkg-three/build/tsconfig.tsbuildinfo',
'pkg-three/types/three.d.ts',
'pkg-two/build/tsconfig.tsbuildinfo',
'pkg-two/build/two.js',
'pkg-two/types/two.d.ts',
]);
});
});
});