-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
config.test.ts
753 lines (636 loc) · 24.4 KB
/
config.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
import { getConsoleLogCalls } from '../utils';
import validateConfig from '@config/validator';
import {
Config,
ConfigWithOptionals,
LogLevel,
ResultParser,
} from '@config/types';
jest.unmock('@config');
jest.unmock('eslint');
const DEFAULT_CONFIGURATION: ConfigWithOptionals = {
repositories: ['test-repo', 'test-repot-2'],
extensions: ['ts', 'tsx'],
eslintrc: { root: true },
rulesUnderTesting: undefined,
pathIgnorePattern: undefined,
maxFileSizeBytes: undefined,
resultParser: undefined,
concurrentTasks: undefined,
CI: undefined,
cache: undefined,
logLevel: undefined,
timeLimit: undefined,
compare: undefined,
updateComparisonReference: undefined,
onComplete: undefined,
};
const mockConfig = jest.fn().mockReturnValue(DEFAULT_CONFIGURATION);
jest.mock('../../eslint-remote-tester.config.js', () => mockConfig());
function getConfig(): Config {
let config: Config;
jest.isolateModules(() => {
config = require('../../lib/config/config').default;
});
return config!;
}
describe('config', () => {
test('default values are set in CI mode', () => {
mockConfig.mockReturnValue({ ...DEFAULT_CONFIGURATION, CI: true });
const config = getConfig();
expect(config.rulesUnderTesting).toStrictEqual([]);
expect(config.resultParser).toBe('plaintext');
expect(config.logLevel).toBe('verbose');
expect(config.concurrentTasks).toBe(5);
expect(config.maxFileSizeBytes).toBe(2e6);
expect(config.timeLimit).toBe(5.5 * 60 * 60);
expect(config.pathIgnorePattern).toBe(undefined);
expect(config.cache).toBe(true);
expect(config.compare).toBe(false);
expect(config.updateComparisonReference).toBe(true);
expect(config.onComplete).toBe(undefined);
});
test('default values are set in CLI mode', () => {
mockConfig.mockReturnValue({ ...DEFAULT_CONFIGURATION, CI: false });
const config = getConfig();
expect(config.rulesUnderTesting).toStrictEqual([]);
expect(config.resultParser).toBe('markdown');
expect(config.logLevel).toBe('verbose');
expect(config.concurrentTasks).toBe(5);
expect(config.maxFileSizeBytes).toBe(2e6);
expect(config.timeLimit).toBe(5.5 * 60 * 60);
expect(config.pathIgnorePattern).toBe(undefined);
expect(config.cache).toBe(true);
expect(config.compare).toBe(false);
expect(config.updateComparisonReference).toBe(true);
expect(config.onComplete).toBe(undefined);
});
test('default value for CI is set', () => {
mockConfig.mockReturnValue({ ...DEFAULT_CONFIGURATION, CI: undefined });
const config = getConfig();
expect(config.CI).toBe(process.env.CI === 'true');
});
test('resultParser option is used when provided', () => {
mockConfig.mockReturnValue({
...DEFAULT_CONFIGURATION,
CI: true,
resultParser: 'markdown',
});
const config = getConfig();
// Should use value from configuration, not default value resolved by CI flag
expect(config.resultParser).toBe('markdown');
});
test('pathIgnorePattern is constructed into RegExp', () => {
mockConfig.mockReturnValue({
...DEFAULT_CONFIGURATION,
pathIgnorePattern: '(should-match-abc)',
});
const config = getConfig();
expect(config.pathIgnorePattern).toBeTruthy();
expect('should-match-abc').toMatch(config.pathIgnorePattern!);
expect('should-not-match-def').not.toMatch(config.pathIgnorePattern!);
});
test('erroneous pathIgnorePattern is ignored', () => {
mockConfig.mockReturnValue({
...DEFAULT_CONFIGURATION,
pathIgnorePattern: '(forgot-to-close-pattern=.*?',
});
const config = getConfig();
expect(config.pathIgnorePattern).toBe(undefined);
});
});
describe('validateConfig', () => {
describe('valid configuration options', () => {
afterEach(() => {
const validationErrors = getConsoleLogCalls();
expect(validationErrors).toHaveLength(0);
expect(process.exit).not.toHaveBeenCalled();
});
test('repositories accepts array with unique strings', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
repositories: ['a', 'b'],
});
});
test('repositories accepts array with a single string', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
repositories: ['a'],
});
});
test('extensions accepts array with unique strings', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
extensions: ['js', 'ts'],
});
});
test('extensions accepts array with a single string', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
extensions: ['js'],
});
});
test('eslintrc accepts valid eslint configuration', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
eslintrc: { rules: {} },
});
});
test('eslintrc accepts function', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
eslintrc: () => ({ rules: {} }),
});
});
test('rulesUnderTesting is optional', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
rulesUnderTesting: undefined,
});
});
test('rulesUnderTesting accepts empty array', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
rulesUnderTesting: [],
});
});
test('rulesUnderTesting accepts array with unique strings', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
rulesUnderTesting: ['a', 'b'],
});
});
test('rulesUnderTesting accepts array with a single string', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
rulesUnderTesting: ['a'],
});
});
test('rulesUnderTesting accepts function', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
rulesUnderTesting: () => true,
});
});
test('pathIgnorePattern is optional', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
pathIgnorePattern: undefined,
});
});
test('pathIgnorePattern accepts valid pattern', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
pathIgnorePattern: '(some-file\\.ts|other-file\\.ts)',
});
});
test('maxFileSizeBytes is optional', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
maxFileSizeBytes: undefined,
});
});
test('maxFileSizeBytes accepts positive number', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
maxFileSizeBytes: 1,
});
});
test('resultParser is optional', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
resultParser: undefined,
});
});
test('resultParser accepts plaintext and markdown', async () => {
const resultParsers: ResultParser[] = ['plaintext', 'markdown'];
for (const resultParser of resultParsers) {
await validateConfig({
...DEFAULT_CONFIGURATION,
resultParser,
});
}
});
test('concurrentTasks is optional', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
concurrentTasks: undefined,
});
});
test('concurrentTasks accepts number', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
concurrentTasks: 1,
});
});
test('CI is optional', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
CI: undefined,
});
});
test('CI accepts boolean', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
CI: true,
});
});
test('cache is optional', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
cache: undefined,
});
});
test('cache accepts boolean', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
cache: true,
});
});
test('logLevel is optional', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
logLevel: undefined,
});
});
test('logLevel accepts verbose, info, warn and error', async () => {
const logLevels: LogLevel[] = ['verbose', 'info', 'warn', 'error'];
for (const logLevel of logLevels) {
await validateConfig({ ...DEFAULT_CONFIGURATION, logLevel });
}
});
test('timeLimit is optional', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
timeLimit: undefined,
});
});
test('timeLimit accepts positive number', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
timeLimit: 1,
});
});
test('compare is optional', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
compare: undefined,
});
});
test('compare accepts boolean', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
compare: true,
});
});
test('updateComparisonReference is optional', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
updateComparisonReference: undefined,
});
});
test('updateComparisonReference accepts boolean', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
updateComparisonReference: true,
});
});
test('onComplete is optional', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
onComplete: undefined,
});
});
test('onComplete accepts function', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
onComplete: () => undefined,
});
});
});
describe('invalid configuration options', () => {
afterEach(() => {
expect(process.exit).toHaveBeenCalledTimes(1);
expect(process.exit).toHaveBeenCalledWith(1);
});
test('error message is displayed', async () => {
await validateConfig({});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch('Configuration validation errors:');
});
test('unknown options are unsupported', async () => {
const key = 'someMistypedKey';
const config = { ...DEFAULT_CONFIGURATION, [key]: true };
await validateConfig(config);
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
`- Options [${key}] are not supported`
);
});
test('repositories are required', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
repositories: undefined,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch('Missing repositories.');
});
test('repositories requires an array', async () => {
const repositories: any = { length: 10 };
await validateConfig({ ...DEFAULT_CONFIGURATION, repositories });
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch('repositories should be an array.');
});
test('repositories array should contain strings', async () => {
const repositories: any = [
'string',
{ value: 'invalid-structure' },
'string-2',
];
await validateConfig({ ...DEFAULT_CONFIGURATION, repositories });
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'repositories should contain only strings'
);
});
test('repositories should contain unique entries', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
repositories: [
'duplicate-1',
'valid',
'duplicate-1',
'duplicate-2',
'valid-2',
'duplicate-2',
],
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'repositories contains duplicate entries: [duplicate-1, duplicate-2]'
);
});
test('extensions are required', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
extensions: undefined,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch('Missing extensions.');
});
test('extensions requires an array', async () => {
const extensions: any = { length: 10 };
await validateConfig({ ...DEFAULT_CONFIGURATION, extensions });
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch('extensions should be an array.');
});
test('extensions array should contain strings', async () => {
const extensions: any = [
'js',
{ value: 'invalid-structure' },
'ts',
];
await validateConfig({ ...DEFAULT_CONFIGURATION, extensions });
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'extensions should contain only strings'
);
});
test('extensions should contain unique entries', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
repositories: ['js', 'ts', 'js', 'md', 'tsx', 'md'],
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'repositories contains duplicate entries: [js, md]'
);
});
test('eslintrc is required', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
eslintrc: undefined as any,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch('Missing eslintrc');
});
test('eslintrc as object is validated', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
eslintrc: { 'not-valid-key': true } as any,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'eslintrc: ESLint configuration in CLIOptions is invalid'
);
expect(validationError).toMatch(
'Unexpected top-level property "not-valid-key"'
);
});
test('eslintrc as function is validated', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
eslintrc: () => ({ 'not-valid-key': true } as any),
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'eslintrc: ESLint configuration in CLIOptions is invalid'
);
expect(validationError).toMatch(
'Unexpected top-level property "not-valid-key"'
);
expect(validationError).toMatch(
'Note that "config.eslintrc" is called with empty options during configuration validation.'
);
});
test('rulesUnderTesting requires an array or function', async () => {
const rulesUnderTesting: any = { length: 10 };
await validateConfig({
...DEFAULT_CONFIGURATION,
rulesUnderTesting,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'rulesUnderTesting should be either an array or function.'
);
});
test('rulesUnderTesting array should contain strings', async () => {
const rulesUnderTesting: any = [
'no-undef',
{ value: 'invalid-structure' },
'testing-library/prefer-wait-for',
];
await validateConfig({
...DEFAULT_CONFIGURATION,
rulesUnderTesting,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'rulesUnderTesting should contain only strings'
);
});
test('rulesUnderTesting should contain unique entries', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
rulesUnderTesting: [
'no-undef',
'react/some-rule',
'no-undef',
'no-shadow',
'prefer-const',
'no-shadow',
],
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'rulesUnderTesting contains duplicate entries: [no-undef, no-shadow]'
);
});
test.todo('rulesUnderTesting should contain valid ESLint rules');
test('pathIgnorePattern requires valid pattern', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
pathIgnorePattern: '(forgot-to-close-pattern=.*?',
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'pathIgnorePattern ((forgot-to-close-pattern=.*?) is not valid regex'
);
expect(validationError).toMatch('Unterminated group');
});
test('maxFileSizeBytes requires number', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
maxFileSizeBytes: 'text' as any,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'maxFileSizeBytes (text) should be a number.'
);
});
test('maxFileSizeBytes requires positive number', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
maxFileSizeBytes: -123,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'maxFileSizeBytes (-123) should be a positive number.'
);
});
test('CI requires boolean', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
CI: 'text' as any,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch('CI (text) should be a boolean.');
});
test('log level requires known value', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
logLevel: 'debug' as any,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'logLevel (debug) is not valid value.'
);
expect(validationError).toMatch(
'Known values are verbose, info, warn, error'
);
});
test('cache requires boolean', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
cache: 'text' as any,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'cache (text) should be a boolean.'
);
});
test('resultParser requires known value', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
resultParser: 'html' as any,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'resultParser (html) is not valid value.'
);
expect(validationError).toMatch(
'Known values are plaintext, markdown'
);
});
test('concurrentTasks requires number', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
concurrentTasks: 'text' as any,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'concurrentTasks (text) should be a number.'
);
});
test('concurrentTasks requires positive number', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
concurrentTasks: -1,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'concurrentTasks (-1) should be a positive number.'
);
});
test('timeLimit requires number', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
timeLimit: 'text' as any,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'timeLimit (text) should be a number.'
);
});
test('timeLimit requires positive number', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
timeLimit: -2,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'timeLimit (-2) should be a positive number.'
);
});
test('compare requires boolean', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
compare: 'text' as any,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'compare (text) should be a boolean.'
);
});
test('updateComparisonReference requires boolean', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
updateComparisonReference: 'text' as any,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'updateComparisonReference (text) should be a boolean.'
);
});
test('onComplete requires function', async () => {
await validateConfig({
...DEFAULT_CONFIGURATION,
onComplete: 'fetch(api).then(parseResponse)' as any,
});
const [validationError] = getConsoleLogCalls();
expect(validationError).toMatch(
'onComplete (fetch(api).then(parseResponse)) should be a function'
);
});
});
});