Skip to content

Commit 3bb2cc3

Browse files
authored
Add settings info to the message shown as each run starts (#568)
1 parent eb6489d commit 3bb2cc3

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

src/lib/run-event/helpers.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const formatStartMessage = ({ count, path, formFactor, locale }) => {
2+
const message = ['Running Lighthouse on', path];
3+
4+
// Build a list of settings used for this run.
5+
const settings = [];
6+
if (locale) {
7+
settings.push(`the “${locale}” locale`);
8+
}
9+
if (formFactor === 'desktop') {
10+
settings.push('the “desktop” preset');
11+
}
12+
if (settings.length) {
13+
message.push(`using ${settings.join(' and ')}`);
14+
}
15+
16+
if (count?.total > 1) {
17+
message.push(`(${count.i}/${count.total})`);
18+
}
19+
20+
return message.join(' ');
21+
};

src/lib/run-event/helpers.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { formatStartMessage } from './helpers.js';
2+
3+
describe('formatStartMessage', () => {
4+
it('should format a message using only the path', () => {
5+
const result = formatStartMessage({ path: 'https://example.com/path' });
6+
expect(result).toEqual('Running Lighthouse on https://example.com/path');
7+
});
8+
9+
it('should format a message using only the path and count', () => {
10+
const result = formatStartMessage({
11+
count: { i: 1, total: 2 },
12+
path: 'https://example.com/path',
13+
});
14+
expect(result).toEqual(
15+
'Running Lighthouse on https://example.com/path (1/2)',
16+
);
17+
});
18+
19+
it('should format a message using a single feature', () => {
20+
const result = formatStartMessage({
21+
path: 'https://example.com/path',
22+
formFactor: 'desktop',
23+
});
24+
expect(result).toEqual(
25+
'Running Lighthouse on https://example.com/path using the “desktop” preset',
26+
);
27+
});
28+
29+
it('should format a message using multiple features', () => {
30+
const result = formatStartMessage({
31+
path: 'https://example.com/path',
32+
formFactor: 'desktop',
33+
locale: 'de',
34+
});
35+
expect(result).toEqual(
36+
'Running Lighthouse on https://example.com/path using the “de” locale and the “desktop” preset',
37+
);
38+
});
39+
40+
it('should format a message using all available inputs', () => {
41+
const result = formatStartMessage({
42+
count: { i: 1, total: 2 },
43+
path: 'https://example.com/path',
44+
formFactor: 'desktop',
45+
locale: 'es',
46+
});
47+
expect(result).toEqual(
48+
'Running Lighthouse on https://example.com/path using the “es” locale and the “desktop” preset (1/2)',
49+
);
50+
});
51+
});

src/lib/run-event/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import runAuditWithUrl from '../../lib/run-audit-with-url/index.js';
66
import runAuditWithServer from '../../lib/run-audit-with-server/index.js';
77
import getConfiguration from '../get-configuration/index.js';
88

9+
import { formatStartMessage } from './helpers.js';
10+
911
const runEvent = async ({
1012
event,
1113
constants,
@@ -57,12 +59,14 @@ const runEvent = async ({
5759
const { serveDir, path, url, thresholds, output_path } = auditConfig;
5860
const fullPath = [serveDir, path].join('/');
5961

60-
let countMessage = '';
61-
if (auditConfigs.length > 1) {
62-
countMessage = ` (${i}/${auditConfigs.length})`;
63-
}
62+
const startMessage = formatStartMessage({
63+
count: { i, total: auditConfigs.length },
64+
path: fullPath,
65+
formFactor: settings?.settings.formFactor,
66+
locale: settings?.settings.locale,
67+
});
6468

65-
console.log(`Running Lighthouse on ${fullPath}${countMessage}`);
69+
console.log(startMessage);
6670

6771
const runner = isOnSuccess ? runAuditWithUrl : runAuditWithServer;
6872
const { errors, summary, shortSummary, details, report, runtimeError } =

0 commit comments

Comments
 (0)