-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathnormalize-flags.unit.test.ts
More file actions
130 lines (120 loc) · 3.77 KB
/
normalize-flags.unit.test.ts
File metadata and controls
130 lines (120 loc) · 3.77 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
import ansis from 'ansis';
import path from 'node:path';
import { describe, expect, it } from 'vitest';
import { logger } from '@code-pushup/utils';
import { DEFAULT_CHROME_FLAGS, LIGHTHOUSE_OUTPUT_PATH } from './constants.js';
import { logUnsupportedFlagsInUse, normalizeFlags } from './normalize-flags.js';
import { LIGHTHOUSE_REPORT_NAME } from './runner/constants.js';
import type { LighthouseOptions } from './types.js';
describe('logUnsupportedFlagsInUse', () => {
it('should log unsupported entries', () => {
logUnsupportedFlagsInUse({ 'list-all-audits': true } as LighthouseOptions);
expect(logger.warn).toHaveBeenCalledTimes(1);
expect(logger.warn).toHaveBeenCalledWith(
`Used unsupported flags: ${ansis.bold('list-all-audits')}`,
);
});
it('should log only 3 details of unsupported entries', () => {
const unsupportedFlags = {
'list-all-audits': true,
'list-locales': '',
'list-trace-categories': '',
chromeIgnoreDefaultFlags: false,
enableErrorReporting: '',
precomputedLanternDataPath: '',
};
logUnsupportedFlagsInUse({
// unsupported
...unsupportedFlags,
} as unknown as LighthouseOptions);
expect(logger.warn).toHaveBeenCalledTimes(1);
expect(logger.warn).toHaveBeenCalledWith(
`Used unsupported flags: ${ansis.bold(
'list-all-audits, list-locales, list-trace-categories',
)} and 3 more.`,
);
});
});
describe('normalizeFlags', () => {
const normalizedDefaults = {
verbose: false,
saveAssets: false,
chromeFlags: DEFAULT_CHROME_FLAGS,
port: 0,
hostname: '127.0.0.1',
view: false,
channel: 'cli',
// custom overwrites in favour of the plugin
quiet: true,
output: ['json'],
outputPath: path.join(LIGHTHOUSE_OUTPUT_PATH, LIGHTHOUSE_REPORT_NAME),
};
it('should fill defaults with undefined flags', () => {
expect(normalizeFlags()).toStrictEqual(normalizedDefaults);
});
it('should fill defaults with empty flags', () => {
expect(normalizeFlags({})).toStrictEqual(normalizedDefaults);
});
it('should forward supported entries', () => {
expect(normalizeFlags({ verbose: true })).toEqual(
expect.objectContaining({ verbose: true }),
);
});
it('should refine entries', () => {
expect(
normalizeFlags({
onlyAudits: 'largest-contentful-paint',
skipAudits: 'is-on-https',
chromeFlags: '--headless=shell',
channel: 'cli',
}),
).toEqual(
expect.objectContaining({
onlyAudits: ['largest-contentful-paint'],
skipAudits: ['is-on-https'],
chromeFlags: ['--headless=shell'],
channel: 'cli',
}),
);
});
it('should rename entries', () => {
expect(
normalizeFlags({
onlyGroups: 'performance',
}),
).toEqual(
expect.objectContaining({
onlyCategories: ['performance'],
}),
);
});
it('should remove unsupported entries and log', () => {
const unsupportedFlags = {
'list-all-audits': '',
chromeIgnoreDefaultFlags: false,
};
const supportedFlags = {
verbose: true,
};
expect(
normalizeFlags({
// unsupported
...unsupportedFlags,
// supported
...supportedFlags,
} as unknown as LighthouseOptions),
).toEqual(expect.not.objectContaining({ 'list-all-audits': true }));
expect(logger.warn).toHaveBeenCalledTimes(1);
});
it('should remove any flag with an empty array as a value', () => {
const flags = {
onlyAudits: [],
skipAudits: [],
onlyCategories: [],
};
const result = normalizeFlags(flags);
expect(result).not.toHaveProperty('onlyAudits');
expect(result).not.toHaveProperty('skipAudits');
expect(result).not.toHaveProperty('onlyCategories');
});
});