-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
test.ts
432 lines (332 loc) · 15.6 KB
/
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import * as path from 'path';
import test from 'ava';
import {verify, verifyWithFileName} from './fixtures/utils';
import tsd from '..';
import {Diagnostic} from '../lib/interfaces';
test('throw if no type definition was found', async t => {
await t.throwsAsync(tsd({cwd: path.join(__dirname, 'fixtures/no-tsd')}), {message: 'The type definition `index.d.ts` does not exist. Create one and try again.'});
});
test('throw if no test is found', async t => {
await t.throwsAsync(tsd({cwd: path.join(__dirname, 'fixtures/no-test')}), {message: 'The test file `index.test-d.ts` or `index.test-d.tsx` does not exist. Create one and try again.'});
});
test('return diagnostics', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/failure')});
verify(t, diagnostics, [
[5, 19, 'error', 'Argument of type \'number\' is not assignable to parameter of type \'string\'.']
]);
});
test('return diagnostics from imported files as well', async t => {
const cwd = path.join(__dirname, 'fixtures/failure-nested');
const diagnostics = await tsd({cwd});
verifyWithFileName(t, cwd, diagnostics, [
[5, 19, 'error', 'Argument of type \'number\' is not assignable to parameter of type \'string\'.', 'child.test-d.ts'],
[6, 19, 'error', 'Argument of type \'number\' is not assignable to parameter of type \'string\'.', 'index.test-d.ts'],
]);
});
test('fail if typings file is not part of `files` list', async t => {
const cwd = path.join(__dirname, 'fixtures/no-files');
const diagnostics = await tsd({cwd});
verifyWithFileName(t, cwd, diagnostics, [
[3, 1, 'error', 'TypeScript type definition `index.d.ts` is not part of the `files` list.', 'package.json'],
]);
});
test('allow specifying folders containing typings file in `files` list', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/files-folder')});
verify(t, diagnostics, []);
});
test('allow specifying negative gitignore-style patterns in `files` list', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/files-gitignore-patterns/negative-pattern')});
verify(t, diagnostics, [
[3, 1, 'error', 'TypeScript type definition `index.d.ts` is not part of the `files` list.'],
]);
});
test('allow specifying negated negative (positive) gitignore-style patterns in `files` list', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/files-gitignore-patterns/negative-pattern-negated')});
verify(t, diagnostics, []);
});
test('allow specifying root marker (/) gitignore-style patterns in `files` list', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/files-gitignore-patterns/root-marker-pattern')});
verify(t, diagnostics, []);
});
test('allow specifying glob patterns containing typings file in `files` list', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/files-glob')});
verify(t, diagnostics, []);
});
test('fail if `typings` property is used instead of `types`', async t => {
const cwd = path.join(__dirname, 'fixtures/types-property/typings');
const diagnostics = await tsd({cwd});
verifyWithFileName(t, cwd, diagnostics, [
[3, 1, 'error', 'Use property `types` instead of `typings`.', 'package.json'],
]);
});
test('fail if tests don\'t pass in strict mode', async t => {
const cwd = path.join(__dirname, 'fixtures/failure-strict-null-checks');
const diagnostics = await tsd({cwd});
verifyWithFileName(t, cwd, diagnostics, [
[
4,
19,
'error',
'Argument of type \'number | null\' is not assignable to parameter of type \'number\'.\n Type \'null\' is not assignable to type \'number\'.',
'index.test-d.ts',
],
]);
});
test('overridden config defaults to `strict` if `strict` is not explicitly overridden', async t => {
const cwd = path.join(__dirname, 'fixtures/strict-null-checks-as-default-config-value');
const diagnostics = await tsd({cwd});
verifyWithFileName(t, cwd, diagnostics, [
[
4,
19,
'error',
'Argument of type \'number | null\' is not assignable to parameter of type \'number\'.\n Type \'null\' is not assignable to type \'number\'.',
'index.test-d.ts',
],
]);
});
test('fail if types are used from a lib that was not explicitly specified', async t => {
const cwd = path.join(__dirname, 'fixtures/lib-config/failure-missing-lib');
const diagnostics = await tsd({cwd});
verifyWithFileName(t, cwd, diagnostics, [
[1, 22, 'error', 'Cannot find name \'Window\'.', 'index.d.ts'],
[4, 11, 'error', 'Cannot find name \'Window\'.', 'index.test-d.ts'],
]);
});
test('allow specifying a lib as a triple-slash-reference', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/lib-config/lib-as-triple-slash-reference')});
verify(t, diagnostics, []);
});
test('allow specifying a lib in package.json\'s `tsd` field', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/lib-config/lib-from-package-json')});
verify(t, diagnostics, []);
});
test('allow specifying a lib in tsconfig.json', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/lib-config/lib-from-tsconfig-json')});
verify(t, diagnostics, []);
});
test('add support for esm with esModuleInterop', async t => {
const diagnostics = await tsd({
cwd: path.join(__dirname, 'fixtures/esm')
});
verify(t, diagnostics, []);
});
test('add DOM support by default', async t => {
const diagnostics = await tsd({
cwd: path.join(__dirname, 'fixtures/dom')
});
verify(t, diagnostics, []);
});
test('a lib option in package.json overrdides a lib option in tsconfig.json', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/lib-config/lib-from-package-json-overrides-tsconfig-json')});
verify(t, diagnostics, []);
});
test('pass in loose mode when strict mode is disabled in settings', async t => {
const diagnostics = await tsd({
cwd: path.join(__dirname, 'fixtures/non-strict-check-with-config')
});
verify(t, diagnostics, []);
});
test('return no diagnostics', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/success')});
verify(t, diagnostics, []);
});
test('support non-barrel main', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/test-non-barrel-main')});
verify(t, diagnostics, []);
});
test('allow omitting `types` property when `main` property is missing but main is a barrel (`index.js`) and .d.ts file matches main', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/no-explicit-types-property/without-main')});
verify(t, diagnostics, [
[6, 0, 'error', 'Expected an error, but found none.']
]);
});
test('allow omitting `types` property when `main` property is set to a barrel (`index.js`) and .d.ts file matches main', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/no-explicit-types-property/with-main-barrel')});
verify(t, diagnostics, [
[6, 0, 'error', 'Expected an error, but found none.']
]);
});
test('allow omitting `types` property when `main` property is set to non-barrel (`foo.js`) and .d.ts file matches main', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/no-explicit-types-property/with-main-other')});
verify(t, diagnostics, [
[6, 0, 'error', 'Expected an error, but found none.']
]);
});
test('support testing in sub-directories', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/test-in-subdir')});
verify(t, diagnostics, []);
});
test('support top-level await', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/top-level-await')});
verify(t, diagnostics, []);
});
test('support default test directory', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/test-directory/default')});
verify(t, diagnostics, []);
});
test('support tsx in subdirectory', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/test-directory/tsx')});
verify(t, diagnostics, []);
});
test('support setting a custom test directory', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/test-directory/custom')});
verify(t, diagnostics, [
[4, 0, 'error', 'Expected an error, but found none.']
]);
});
test('expectError for classes', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/expect-error/classes')});
verify(t, diagnostics, []);
});
test('expectError for functions', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/expect-error/functions')});
verify(t, diagnostics, [
[5, 0, 'error', 'Expected an error, but found none.']
]);
});
test('expectError for generics', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/expect-error/generics')});
verify(t, diagnostics, []);
});
test('expectError should not ignore syntactical errors', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/expect-error/syntax')});
verify(t, diagnostics, [
[4, 29, 'error', '\')\' expected.'],
[5, 22, 'error', '\',\' expected.'],
[4, 0, 'error', 'Expected an error, but found none.'],
[5, 0, 'error', 'Expected an error, but found none.']
]);
});
test('expectError for values', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/expect-error/values')});
verify(t, diagnostics, [
[5, 0, 'error', 'Expected an error, but found none.']
]);
});
test('expectError for values (noImplicitAny disabled)', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/expect-error/values-disabled-no-implicit-any')});
verify(t, diagnostics, []);
});
test('missing import', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/missing-import')});
verify(t, diagnostics, [
[3, 18, 'error', 'Cannot find name \'Primitive\'.']
]);
});
test('tsx component', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/tsx/component')});
verify(t, diagnostics, []);
});
test('tsx component type', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/tsx/component-type')});
verify(t, diagnostics, []);
});
test('loose types', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/strict-types/loose')});
verify(t, diagnostics, [
[5, 0, 'error', 'Parameter type `string` is declared too wide for argument type `"cat"`.'],
[7, 0, 'error', 'Parameter type `string | number` is declared too wide for argument type `string`.'],
[8, 0, 'error', 'Parameter type `string | number` is declared too wide for argument type `number`.'],
[10, 0, 'error', 'Parameter type `string | Date` is declared too wide for argument type `Date`.'],
[11, 0, 'error', 'Parameter type `Promise<string | number>` is declared too wide for argument type `Promise<number>`.'],
[12, 0, 'error', 'Parameter type `string | Promise<string | number>` is declared too wide for argument type `Promise<string | number>`.'],
[14, 0, 'error', 'Parameter type `Promise<string | number>` is declared too wide for argument type `Promise<number>`.'],
[16, 0, 'error', 'Parameter type `Observable<string | number>` is declared too wide for argument type `Observable<string>`.'],
[20, 0, 'error', 'Parameter type `Observable<string | number> | Observable<string | number | boolean>` is declared too wide for argument type `Observable<string | number> | Observable<string>`.'],
[28, 0, 'error', 'Parameter type `Foo<string | Foo<string | number>> | Foo<Date> | Foo<Symbol>` is declared too wide for argument type `Foo<Date> | Foo<Symbol> | Foo<string | Foo<number>>`.'],
[32, 0, 'error', 'Parameter type `string | number` is not identical to argument type `any`.'],
[34, 0, 'error', 'Parameter type `Observable<string | number> | Observable<any> | Observable<string | number | boolean>` is not identical to argument type `Observable<string | number> | Observable<string> | Observable<string | number | boolean>`.']
]);
});
test('strict types', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/strict-types/strict')});
verify(t, diagnostics, []);
});
test('typings in custom directory', async t => {
const diagnostics = await tsd({
cwd: path.join(__dirname, 'fixtures/typings-custom-dir'),
typingsFile: 'utils/index.d.ts'
});
verify(t, diagnostics, [
[5, 19, 'error', 'Argument of type \'number\' is not assignable to parameter of type \'string\'.']
]);
});
test('specify test files manually', async t => {
const diagnostics = await tsd({
cwd: path.join(__dirname, 'fixtures/specify-test-files'),
testFiles: [
'unknown.test.ts'
]
});
verify(t, diagnostics, [
[5, 19, 'error', 'Argument of type \'number\' is not assignable to parameter of type \'string\'.']
]);
});
test('fails if typings file is not found in the specified path', async t => {
const error = await t.throwsAsync(tsd({
cwd: path.join(__dirname, 'fixtures/typings-custom-dir'),
typingsFile: 'unknown.d.ts'
}));
t.is(error.message, 'The type definition `unknown.d.ts` does not exist. Create one and try again.');
});
test('includes extended config files along with found ones', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/ts-config-extends')});
verify(t, diagnostics, [
[6, 64, 'error', 'Not all code paths return a value.'],
]);
});
test('errors in libs from node_modules are not reported', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/exclude-node-modules')});
const [nodeModuleDiagnostics, testFileDiagnostics, otherDiagnostics] = diagnostics.reduce(
([nodeModuleDiags, testFileDiags, otherDiags], diagnostic) => {
if (/[/\\]node_modules[/\\]/.test(diagnostic.fileName)) {
nodeModuleDiags.push(diagnostic);
} else if (/[/\\]fixtures[/\\]exclude-node-modules[/\\]/.test(diagnostic.fileName)) {
testFileDiags.push(diagnostic);
} else {
otherDiags.push(diagnostic);
}
return [nodeModuleDiags, testFileDiags, otherDiags];
},
[[], [], []] as Diagnostic[][]
);
t.deepEqual(
nodeModuleDiagnostics.length,
0,
'There must be no errors from node_modules folders when standard lib is not available (option `"noLib": true`).',
);
t.true(
otherDiagnostics.length > 0,
'There must be errors from tsd lib when standard lib is not available (option `"noLib": true`).',
);
const alloweOtherFileFailures = [
/[/\\]lib[/\\]index.d.ts$/,
/[/\\]lib[/\\]interfaces.d.ts$/,
];
otherDiagnostics.forEach(diagnostic => {
t.true(
alloweOtherFileFailures.some(allowedFileRe => allowedFileRe.test(diagnostic.fileName)),
`Found diagnostic from an unexpected file: ${diagnostic.fileName} - ${diagnostic.message}`,
);
});
verify(t, testFileDiagnostics, [
[3, 18, 'error', 'Cannot find name \'Bar\'.']
]);
});
test('allow specifying `rootDir` option in `tsconfig.json`', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/root-dir')});
verify(t, diagnostics, []);
});
test('prints the types of expressions passed to `printType` helper', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/print-type')});
verify(t, diagnostics, [
[4, 0, 'warning', 'Type for expression `aboveZero` is: `(foo: number) => number | null`'],
[5, 0, 'warning', 'Type for expression `null` is: `null`'],
[6, 0, 'warning', 'Type for expression `undefined` is: `undefined`'],
[7, 0, 'warning', 'Type for expression `null as any` is: `any`'],
[8, 0, 'warning', 'Type for expression `null as never` is: `never`'],
[9, 0, 'warning', 'Type for expression `null as unknown` is: `unknown`'],
[10, 0, 'warning', 'Type for expression `\'foo\'` is: `"foo"`'],
]);
});