-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
config-test.js
1486 lines (1312 loc) · 51.1 KB
/
config-test.js
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license Copyright 2016 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
import {strict as assert} from 'assert';
import path from 'path';
import log from 'lighthouse-logger';
import Config from '../../config/config.js';
import defaultConfig from '../../config/default-config.js';
import constants from '../../config/constants.js';
import Gatherer from '../../gather/gatherers/gatherer.js';
import Audit from '../../audits/audit.js';
import i18n from '../../lib/i18n/i18n.js';
import format from '../../../shared/localization/format.js';
import {createCommonjsRefs} from '../../scripts/esm-utils.js';
const {require, __filename, __dirname} = createCommonjsRefs(import.meta);
describe('Config', () => {
let origConfig;
beforeEach(() => {
origConfig = JSON.parse(JSON.stringify(defaultConfig));
});
it('returns new object', () => {
const config = {
audits: ['is-on-https'],
};
const newConfig = new Config(config);
assert.notEqual(config, newConfig);
});
it('doesn\'t change directly injected gatherer implementations', () => {
class MyGatherer extends Gatherer {}
class MyAudit extends Audit {
static get meta() {
return {
id: 'my-audit',
title: 'My audit',
failureTitle: 'My failing audit',
description: '.',
requiredArtifacts: ['MyGatherer'],
};
}
static audit() {}
}
const config = {
// Extend default to double test our ability to handle injection.
extends: 'lighthouse:default',
settings: {onlyAudits: ['my-audit']},
passes: [{
gatherers: [MyGatherer],
}],
audits: [MyAudit],
};
const newConfig = new Config(config);
assert.equal(MyGatherer, newConfig.passes[0].gatherers[0].implementation);
assert.equal(MyAudit, newConfig.audits[0].implementation);
});
it('doesn\'t change directly injected gatherer instances', () => {
class MyGatherer extends Gatherer {
constructor(secretVal) {
super();
this.secret = secretVal;
}
get name() {
// Use unique artifact name per instance so gatherers aren't deduplicated.
return `MyGatherer${this.secret}`;
}
}
const myGatherer1 = new MyGatherer(1729);
const myGatherer2 = new MyGatherer(6);
const config = {
passes: [{
gatherers: [
myGatherer1,
{instance: myGatherer2},
],
}],
};
const newConfig = new Config(config);
const configGatherers = newConfig.passes[0].gatherers;
assert(configGatherers[0].instance instanceof MyGatherer);
assert.equal(configGatherers[0].instance.secret, 1729);
assert(configGatherers[1].instance instanceof MyGatherer);
assert.equal(configGatherers[1].instance.secret, 6);
});
it('uses the default config when no config is provided', () => {
const config = new Config();
assert.deepStrictEqual(config.categories, origConfig.categories);
assert.deepStrictEqual(config.audits.map(a => a.path), origConfig.audits);
});
it('throws when a passName is used twice', () => {
const unlikelyPassName = 'unlikelyPassName';
const configJson = {
passes: [{
passName: unlikelyPassName,
gatherers: ['meta-elements'],
}, {
passName: unlikelyPassName,
gatherers: ['viewport-dimensions'],
}],
};
assert.throws(_ => new Config(configJson), /unique/);
});
it('defaults passName to defaultPass', () => {
class MyGatherer extends Gatherer {}
const configJson = {
passes: [{
gatherers: [MyGatherer],
}],
};
const config = new Config(configJson);
const defaultPass = config.passes.find(pass => pass.passName === 'defaultPass');
assert.ok(
defaultPass.gatherers.find(gatherer => gatherer.implementation === MyGatherer),
'defaultPass should have contained extra gatherer'
);
});
it('throws when an audit requires an artifact with no gatherer supplying it', async () => {
class NeedsWhatYouCantGive extends Audit {
static get meta() {
return {
id: 'missing-artifact-audit',
title: 'none',
failureTitle: 'none',
description: 'none',
requiredArtifacts: [
// Require fake artifact amidst base artifact and default artifacts.
'URL',
'ConsoleMessages',
'VRMLElements', // not a real gatherer
'ViewportDimensions',
],
};
}
static audit() {}
}
expect(() => new Config({
extends: 'lighthouse:default',
audits: [NeedsWhatYouCantGive],
// eslint-disable-next-line max-len
})).toThrow('VRMLElements gatherer, required by audit missing-artifact-audit, was not found in config');
});
// eslint-disable-next-line max-len
it('does not throw when an audit requests an optional artifact with no gatherer supplying it', async () => {
class DoesntNeedYourCrap extends Audit {
static get meta() {
return {
id: 'optional-artifact-audit',
title: 'none',
failureTitle: 'none',
description: 'none',
requiredArtifacts: [
'URL', // base artifact
'ViewportDimensions', // from gatherer
],
__internalOptionalArtifacts: [
'NotInTheConfig',
],
};
}
static audit() {}
}
// Shouldn't throw.
const config = new Config({
extends: 'lighthouse:default',
audits: [DoesntNeedYourCrap],
}, {
// Trigger filtering logic.
onlyAudits: ['optional-artifact-audit'],
});
expect(config.passes[0].gatherers.map(g => g.path)).toEqual(['viewport-dimensions']);
});
it('should keep optional artifacts in gatherers after filter', async () => {
class ButWillStillTakeYourCrap extends Audit {
static get meta() {
return {
id: 'optional-artifact-audit',
title: 'none',
failureTitle: 'none',
description: 'none',
requiredArtifacts: [
'URL', // base artifact
'ViewportDimensions', // from gatherer
],
__internalOptionalArtifacts: [
'SourceMaps', // Is in the config.
],
};
}
static audit() {}
}
const config = new Config({
extends: 'lighthouse:default',
audits: [ButWillStillTakeYourCrap],
}, {
// Trigger filtering logic.
onlyAudits: ['optional-artifact-audit'],
});
expect(config.passes[0].gatherers.map(g => g.path))
.toEqual(['viewport-dimensions', 'source-maps']);
});
it('should keep optional artifacts in gatherers after filter', async () => {
class ButWillStillTakeYourCrap extends Audit {
static get meta() {
return {
id: 'optional-artifact-audit',
title: 'none',
failureTitle: 'none',
description: 'none',
requiredArtifacts: [
'URL', // base artifact
'ViewportDimensions', // from gatherer
],
__internalOptionalArtifacts: [
'SourceMaps', // Is in the config.
],
};
}
static audit() {}
}
const config = new Config({
extends: 'lighthouse:default',
audits: [ButWillStillTakeYourCrap],
}, {
// Trigger filtering logic.
onlyAudits: ['optional-artifact-audit'],
});
expect(config.passes[0].gatherers.map(g => g.path))
.toEqual(['viewport-dimensions', 'source-maps']);
});
it('should keep optional artifacts in gatherers after filter', async () => {
class ButWillStillTakeYourCrap extends Audit {
static get meta() {
return {
id: 'optional-artifact-audit',
title: 'none',
failureTitle: 'none',
description: 'none',
requiredArtifacts: [
'URL', // base artifact
'ViewportDimensions', // from gatherer
],
__internalOptionalArtifacts: [
'SourceMaps', // Is in the config.
],
};
}
static audit() {}
}
const config = new Config({
extends: 'lighthouse:default',
audits: [ButWillStillTakeYourCrap],
}, {
// Trigger filtering logic.
onlyAudits: ['optional-artifact-audit'],
});
expect(config.passes[0].gatherers.map(g => g.path))
.toEqual(['viewport-dimensions', 'source-maps']);
});
it('does not throw when an audit requires only base artifacts', () => {
class BaseArtifactsAudit extends Audit {
static get meta() {
return {
id: 'base-artifacts-audit',
title: 'base',
failureTitle: 'base',
description: 'base',
requiredArtifacts: ['HostUserAgent', 'URL', 'Stacks', 'WebAppManifest'],
};
}
static audit() {}
}
const config = new Config({
extends: 'lighthouse:default',
audits: [BaseArtifactsAudit],
}, {onlyAudits: ['base-artifacts-audit']});
assert.strictEqual(config.audits.length, 1);
assert.strictEqual(config.audits[0].implementation.meta.id, 'base-artifacts-audit');
});
it('throws for unknown gatherers', () => {
const config = {
passes: [{
gatherers: ['fuzz'],
}],
audits: [
'is-on-https',
],
};
return assert.throws(_ => new Config(config),
/Unable to locate/);
});
it('doesn\'t mutate old gatherers when filtering passes', () => {
const configJSON = {
passes: [{
gatherers: [
'viewport-dimensions',
'meta-elements',
'inspector-issues',
],
}],
audits: ['is-on-https'],
};
const _ = new Config(configJSON);
assert.equal(configJSON.passes[0].gatherers.length, 3);
});
it('expands audits', () => {
const config = new Config({
audits: ['user-timings'],
});
assert.ok(Array.isArray(config.audits));
assert.equal(config.audits.length, 1);
assert.equal(config.audits[0].path, 'user-timings');
assert.equal(typeof config.audits[0].implementation, 'function');
assert.deepEqual(config.audits[0].options, {});
});
it('throws when an audit is not found', () => {
return assert.throws(_ => new Config({
audits: ['/fake-path/non-existent-audit'],
}), /locate audit/);
});
it('throws on a non-absolute config path', () => {
const configPath = '../../config/default-config.js';
return assert.throws(_ => new Config({
audits: [],
}, {configPath}), /absolute path/);
});
it('loads an audit relative to a config path', () => {
const configPath = __filename;
return assert.doesNotThrow(_ => new Config({
audits: ['../fixtures/valid-custom-audit'],
}, {configPath}));
});
it('loads an audit from node_modules/', () => {
return assert.throws(_ => new Config({
// Use a lighthouse dep as a stand in for a module.
audits: ['lighthouse-logger'],
}), function(err) {
// Should throw an audit validation error, but *not* an audit not found error.
return !/locate audit/.test(err) && /audit\(\) method/.test(err);
});
});
it('loads an audit relative to the working directory', () => {
// Construct an audit URL relative to current working directory, regardless
// of where test was started from.
const absoluteAuditPath = path.resolve(__dirname, '../fixtures/valid-custom-audit');
assert.doesNotThrow(_ => require.resolve(absoluteAuditPath));
const relativePath = path.relative(process.cwd(), absoluteAuditPath);
return assert.doesNotThrow(_ => new Config({
audits: [relativePath],
}));
});
it('throws but not for missing audit when audit has a dependency error', () => {
return assert.throws(_ => new Config({
audits: [path.resolve(__dirname, '../fixtures/invalid-audits/require-error.js')],
}), function(err) {
// We're expecting not to find parent class Audit, so only reject on our
// own custom locate audit error, not the usual MODULE_NOT_FOUND.
return !/locate audit/.test(err) && err.code === 'MODULE_NOT_FOUND';
});
});
it('throws when it finds invalid audits', () => {
const basePath = path.resolve(__dirname, '../fixtures/invalid-audits');
assert.throws(_ => new Config({
audits: [basePath + '/missing-audit'],
}), /audit\(\) method/);
assert.throws(_ => new Config({
audits: [basePath + '/missing-id'],
}), /meta.id property/);
assert.throws(_ => new Config({
audits: [basePath + '/missing-title'],
}), /meta.title property/);
assert.throws(_ => new Config({
audits: [
class BinaryButNoFailureTitleAudit extends Audit {
static get meta() {
return {
id: 'no-failure-title',
title: 'title',
description: 'help',
requiredArtifacts: [],
scoreDisplayMode: 'binary',
};
}
static audit() {
throw new Error('Unimplemented');
}
},
],
}), /no meta.failureTitle and should/);
assert.throws(_ => new Config({
audits: [basePath + '/missing-description'],
}), /meta.description property/);
assert.throws(_ => new Config({
audits: [
class EmptyStringDescriptionAudit extends Audit {
static get meta() {
return {
id: 'empty-string-description',
title: 'title',
failureTitle: 'none',
description: '',
requiredArtifacts: [],
};
}
static audit() {
throw new Error('Unimplemented');
}
},
],
}), /empty meta.description string/);
assert.throws(_ => new Config({
audits: [basePath + '/missing-required-artifacts'],
}), /meta.requiredArtifacts property/);
});
it('throws when a category references a non-existent audit', () => {
return assert.throws(_ => new Config({
audits: [],
categories: {
pwa: {
auditRefs: [
{id: 'missing-audit'},
],
},
},
}), /could not find missing-audit/);
});
it('throws when a category fails to specify an audit id', () => {
return assert.throws(_ => new Config({
audits: [],
categories: {
pwa: {
auditRefs: [
'oops-wrong-format',
],
},
},
}), /missing an audit id at pwa\[0\]/);
});
it('throws when an accessibility audit does not have a group', () => {
return assert.throws(_ => new Config({
audits: ['accessibility/color-contrast'],
categories: {
accessibility: {
auditRefs: [
{id: 'color-contrast'},
],
},
},
}), /does not have a group/);
});
it('throws when an audit references an unknown group', () => {
return assert.throws(_ => new Config({
groups: {
'group-a': {
title: 'Group A',
description: 'The best group around.',
},
},
audits: ['metrics/first-meaningful-paint'],
categories: {
pwa: {
auditRefs: [
{id: 'first-meaningful-paint', group: 'group-a'},
{id: 'first-meaningful-paint', group: 'missing-group'},
],
},
},
}), /unknown group missing-group/);
});
it('throws when a manual audit has weight', () => {
return assert.throws(_ => new Config({
audits: ['manual/pwa-cross-browser'],
categories: {
accessibility: {
auditRefs: [
{id: 'pwa-cross-browser', weight: 10},
],
},
},
}), /cross-browser .*has a positive weight/);
});
it('filters the config', () => {
const config = new Config({
settings: {
onlyCategories: ['needed-category'],
onlyAudits: ['color-contrast'],
},
passes: [
{recordTrace: true, gatherers: []},
{passName: 'a11y', gatherers: ['accessibility']},
],
audits: [
'accessibility/color-contrast',
'metrics/first-meaningful-paint',
'metrics/first-contentful-paint',
'metrics/cumulative-layout-shift',
],
categories: {
'needed-category': {
auditRefs: [
{id: 'first-meaningful-paint'},
{id: 'first-contentful-paint'},
],
},
'other-category': {
auditRefs: [
{id: 'color-contrast'},
{id: 'cumulative-layout-shift'},
],
},
'unused-category': {
auditRefs: [
{id: 'cumulative-layout-shift'},
],
},
},
});
assert.equal(config.audits.length, 3, 'reduces audit count');
assert.equal(config.passes.length, 2, 'preserves both passes');
assert.ok(config.passes[0].recordTrace, 'preserves recordTrace pass');
assert.ok(!config.categories['unused-category'], 'removes unused categories');
assert.equal(config.categories['needed-category'].auditRefs.length, 2);
assert.equal(config.categories['other-category'].auditRefs.length, 1);
});
it('filters the config w/ skipAudits', () => {
const config = new Config({
settings: {
skipAudits: ['first-meaningful-paint'],
},
passes: [
{recordTrace: true, gatherers: []},
{passName: 'a11y', gatherers: ['accessibility']},
],
audits: [
'accessibility/color-contrast',
'metrics/first-meaningful-paint',
'metrics/first-contentful-paint',
'metrics/cumulative-layout-shift',
],
categories: {
'needed-category': {
auditRefs: [
{id: 'first-meaningful-paint'},
{id: 'first-contentful-paint'},
{id: 'color-contrast'},
],
},
'other-category': {
auditRefs: [
{id: 'color-contrast'},
{id: 'cumulative-layout-shift'},
],
},
},
});
assert.equal(config.audits.length, 3, 'skips the FMP audit');
assert.equal(config.passes.length, 2, 'preserves both passes');
assert.ok(config.passes[0].recordTrace, 'preserves recordTrace pass');
assert.equal(config.categories['needed-category'].auditRefs.length, 2,
'removes skipped audit from category');
});
it('filtering filters out traces when not needed', () => {
const warnings = [];
const saveWarning = evt => warnings.push(evt);
log.events.addListener('warning', saveWarning);
const config = new Config({
extends: 'lighthouse:default',
settings: {
onlyCategories: ['accessibility'],
},
});
log.events.removeListener('warning', saveWarning);
assert.ok(config.audits.length, 'inherited audits by extension');
assert.equal(config.passes.length, 1, 'filtered out passes');
assert.equal(warnings.length, 1, 'warned about dropping trace');
assert.equal(config.passes[0].recordTrace, false, 'turns off tracing if not needed');
});
it('forces the first pass to have a fatal loadFailureMode', () => {
const warnings = [];
const saveWarning = evt => warnings.push(evt);
log.events.addListener('warning', saveWarning);
const config = new Config({
extends: 'lighthouse:default',
settings: {
onlyCategories: ['performance', 'pwa'],
},
passes: [
{passName: 'defaultPass', loadFailureMode: 'warn'},
],
});
log.events.removeListener('warning', saveWarning);
expect(warnings).toHaveLength(1);
expect(warnings[0][0]).toMatch(/loadFailureMode.*fatal/);
expect(config.passes[0]).toHaveProperty('loadFailureMode', 'fatal');
});
it('filters works with extension', () => {
const config = new Config({
extends: 'lighthouse:default',
settings: {
onlyCategories: ['performance'],
onlyAudits: ['is-on-https'],
},
});
assert.ok(config.audits.length, 'inherited audits by extension');
// +1 for `is-on-https`, +1 for `full-page-screenshot`.
assert.equal(config.audits.length, origConfig.categories.performance.auditRefs.length + 2);
assert.equal(config.passes.length, 1, 'filtered out passes');
assert.ok(config.audits.find(a => a.implementation.meta.id === 'is-on-https'));
assert.ok(config.audits.find(a => a.implementation.meta.id === 'full-page-screenshot'));
});
it('warns for invalid filters', () => {
const warnings = [];
const saveWarning = evt => warnings.push(evt);
log.events.addListener('warning', saveWarning);
const config = new Config({
extends: 'lighthouse:default',
settings: {
onlyCategories: ['performance', 'missing-category'],
onlyAudits: ['first-contentful-paint', 'missing-audit'],
},
});
log.events.removeListener('warning', saveWarning);
assert.ok(config, 'failed to generate config');
assert.equal(warnings.length, 3, 'did not warn enough');
});
it('throws for invalid use of skipAudits and onlyAudits', () => {
assert.throws(() => {
new Config({
extends: 'lighthouse:default',
settings: {
onlyAudits: ['first-meaningful-paint'],
skipAudits: ['first-meaningful-paint'],
},
});
});
});
it('cleans up flags for settings', () => {
const config = new Config({extends: 'lighthouse:default'},
{nonsense: 1, foo: 2, throttlingMethod: 'provided'});
assert.equal(config.settings.throttlingMethod, 'provided');
assert.ok(config.settings.nonsense === undefined, 'did not cleanup settings');
});
it('allows overriding of array-typed settings', () => {
const config = new Config({extends: 'lighthouse:default'}, {output: ['html']});
assert.deepStrictEqual(config.settings.output, ['html']);
});
it('extends the config', () => {
class CustomAudit extends Audit {
static get meta() {
return {
id: 'custom-audit',
title: 'none',
failureTitle: 'none',
description: 'none',
requiredArtifacts: [],
};
}
static audit() {
throw new Error('Unimplemented');
}
}
const config = new Config({
extends: 'lighthouse:default',
audits: [
CustomAudit,
],
});
const auditNames = new Set(config.audits.map(audit => audit.implementation.meta.id));
assert.ok(config, 'failed to generate config');
assert.ok(auditNames.has('custom-audit'), 'did not include custom audit');
assert.ok(auditNames.has('unused-javascript'), 'did not include full audits');
assert.ok(auditNames.has('first-meaningful-paint'), 'did not include default audits');
});
it('ensures quiet thresholds are sufficient when using devtools', () => {
const config = new Config({
extends: 'lighthouse:default',
settings: {
throttlingMethod: 'devtools',
},
});
assert.equal(config.settings.throttlingMethod, 'devtools');
assert.equal(config.passes[0].passName, 'defaultPass');
assert.ok(config.passes[0].pauseAfterFcpMs >= 5000, 'did not adjust fcp quiet ms');
assert.ok(config.passes[0].pauseAfterLoadMs >= 5000, 'did not adjust load quiet ms');
assert.ok(config.passes[0].cpuQuietThresholdMs >= 5000, 'did not adjust cpu quiet ms');
assert.ok(config.passes[0].networkQuietThresholdMs >= 5000, 'did not adjust network quiet ms');
assert.equal(config.passes[1].pauseAfterLoadMs, 0, 'should not have touched non-defaultPass');
});
it('does nothing when thresholds for devtools are already sufficient', () => {
const config = new Config({
extends: 'lighthouse:default',
settings: {
throttlingMethod: 'devtools',
onlyCategories: ['performance'],
},
passes: [
{
pauseAfterLoadMs: 10001,
cpuQuietThresholdMs: 10002,
networkQuietThresholdMs: 10003,
},
],
});
assert.equal(config.settings.throttlingMethod, 'devtools');
assert.equal(config.passes[0].pauseAfterLoadMs, 10001);
assert.equal(config.passes[0].cpuQuietThresholdMs, 10002);
assert.equal(config.passes[0].networkQuietThresholdMs, 10003);
});
it('only supports `lighthouse:default` extension', () => {
const createConfig = extendsValue => new Config({extends: extendsValue});
expect(() => createConfig(true)).toThrowError(/default` is the only valid extension/);
expect(() => createConfig('lighthouse')).toThrowError(/default` is the only valid/);
expect(() => createConfig('lighthouse:full')).toThrowError(/default` is the only valid/);
});
it('merges settings with correct priority', () => {
const config = new Config(
{
extends: 'lighthouse:default',
settings: {
disableStorageReset: true,
formFactor: 'desktop',
throttling: constants.throttling.desktopDense4G,
screenEmulation: constants.screenEmulationMetrics.desktop,
emulatedUserAgent: constants.userAgents.desktop,
},
},
{formFactor: 'desktop'}
);
assert.ok(config, 'failed to generate config');
assert.ok(typeof config.settings.maxWaitForLoad === 'number', 'missing setting from default');
assert.ok(config.settings.disableStorageReset, 'missing setting from extension config');
assert.ok(config.settings.formFactor === 'desktop', 'missing setting from flags');
});
it('inherits default settings when undefined', () => {
const config = new Config({settings: undefined});
assert.ok(typeof config.settings.maxWaitForLoad === 'number', 'missing setting from default');
});
describe('locale', () => {
it('falls back to default locale if none specified', () => {
const config = new Config({settings: undefined});
// Don't assert specific locale so it isn't tied to where tests are run, but
// check that it's valid and available.
assert.ok(config.settings.locale);
assert.strictEqual(config.settings.locale, i18n.lookupLocale(config.settings.locale));
});
it('uses config setting for locale if set', () => {
const locale = 'ar-XB';
const config = new Config({settings: {locale}});
assert.strictEqual(config.settings.locale, locale);
});
it('uses flag setting for locale if set', () => {
const settingsLocale = 'en-XA';
const flagsLocale = 'ar-XB';
const config = new Config({settings: {locale: settingsLocale}}, {locale: flagsLocale});
assert.strictEqual(config.settings.locale, flagsLocale);
});
});
describe('emulatedUserAgent', () => {
it('uses the default UA string when emulatedUserAgent is undefined', () => {
const config = new Config({});
expect(config.settings.emulatedUserAgent).toMatch(/^Mozilla\/5.*Chrome-Lighthouse$/);
});
it('uses the default UA string when emulatedUserAgent is true', () => {
const config = new Config({
settings: {
emulatedUserAgent: true,
},
});
expect(config.settings.emulatedUserAgent).toMatch(/^Mozilla\/5.*Chrome-Lighthouse$/);
});
it('does not use a UA string when emulatedUserAgent is false', () => {
const config = new Config({
settings: {
emulatedUserAgent: false,
},
});
expect(config.settings.emulatedUserAgent).toEqual(false);
});
it('uses the UA string provided if it is a string', () => {
const emulatedUserAgent = 'one weird trick to get a perfect LH score';
const config = new Config({
settings: {
emulatedUserAgent,
},
});
expect(config.settings.emulatedUserAgent).toEqual(emulatedUserAgent);
});
});
it('is idempotent when accepting a canonicalized Config as valid ConfigJson input', () => {
const config = new Config(defaultConfig);
const configAgain = new Config(config);
assert.deepEqual(config, configAgain);
});
it('is idempotent accepting a canonicalized filtered Config as valid ConfigJson input', () => {
const extendedJson = {
extends: 'lighthouse:default',
settings: {
onlyCategories: ['pwa'],
},
};
const config = new Config(extendedJson);
assert.equal(config.passes.length, 2, 'did not filter config');
assert.equal(Object.keys(config.categories).length, 1, 'did not filter config');
assert.deepEqual(config.settings.onlyCategories, ['pwa']);
const configAgain = new Config(config);
assert.deepEqual(config, configAgain);
});
describe('#extendConfigJSON', () => {
it('should merge passes', () => {
const configA = {
passes: [
{passName: 'passA', gatherers: ['a']},
{passName: 'passB', gatherers: ['b']},
{gatherers: ['c']},
],
};
const configB = {
passes: [
{passName: 'passB', recordTrace: true, gatherers: ['d']},
{gatherers: ['e']},
],
};
const merged = Config.extendConfigJSON(configA, configB);
assert.equal(merged.passes.length, 4);
assert.equal(merged.passes[1].recordTrace, true);
assert.deepEqual(merged.passes[1].gatherers, ['b', 'd']);
assert.deepEqual(merged.passes[3].gatherers, ['e']);
});
it('should merge audits', () => {
const configA = {audits: ['a', 'b']};
const configB = {audits: ['c']};
const merged = Config.extendConfigJSON(configA, configB);
assert.deepEqual(merged.audits, ['a', 'b', 'c']);
});
it('should merge categories', () => {
const configA = {categories: {A: {title: 'Acat'}, B: {title: 'Bcat'}}};
const configB = {categories: {C: {title: 'Ccat'}}};
const merged = Config.extendConfigJSON(configA, configB);
assert.deepStrictEqual(merged.categories, {
A: {title: 'Acat'},
B: {title: 'Bcat'},
C: {title: 'Ccat'},
});
});
it('should merge other values', () => {
const artifacts = {
traces: {defaultPass: '../some/long/path'},
devtoolsLogs: {defaultPass: 'path/to/devtools/log'},
};
const configA = {};
const configB = {extends: 'lighthouse:default', artifacts};
const merged = Config.extendConfigJSON(configA, configB);
assert.equal(merged.extends, 'lighthouse:default');
assert.equal(merged.artifacts, configB.artifacts);
});
});
describe('mergePlugins', () => {
// Include a configPath flag so that config.js looks for the plugins in the fixtures dir.
const configFixturePath = __dirname + '/../fixtures/config-plugins/';
it('should append audits', () => {
const configJson = {
audits: ['installable-manifest', 'metrics'],
plugins: ['lighthouse-plugin-simple'],
};
const config = new Config(configJson, {configPath: configFixturePath});
assert.deepStrictEqual(config.audits.map(a => a.path),
['installable-manifest', 'metrics', 'redirects', 'user-timings']);
});
it('should append and use plugin-prefixed groups', () => {
const configJson = {
audits: ['installable-manifest', 'metrics'],
plugins: ['lighthouse-plugin-simple'],
groups: {
configGroup: {title: 'This is a group in the base config'},
},
};
const config = new Config(configJson, {configPath: configFixturePath});
const groupIds = Object.keys(config.groups);
assert.ok(groupIds.length === 2);
assert.strictEqual(groupIds[0], 'configGroup');
assert.strictEqual(groupIds[1], 'lighthouse-plugin-simple-new-group');
assert.strictEqual(config.groups['lighthouse-plugin-simple-new-group'].title, 'New Group');
assert.strictEqual(config.categories['lighthouse-plugin-simple'].auditRefs[0].group,
'lighthouse-plugin-simple-new-group');
});
it('should append a category', () => {
const configJson = {
extends: 'lighthouse:default',
plugins: ['lighthouse-plugin-simple'],
};
const config = new Config(configJson, {configPath: configFixturePath});