From 4245790d63e3a6a3e192471f3d9e31c6967adff4 Mon Sep 17 00:00:00 2001 From: Jack Brewer Date: Tue, 4 Apr 2023 15:48:32 +0100 Subject: [PATCH] Add settings info to the message shown as each run starts --- src/lib/run-event/helpers.js | 21 +++++++++++++ src/lib/run-event/helpers.test.js | 51 +++++++++++++++++++++++++++++++ src/lib/run-event/index.js | 14 ++++++--- 3 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 src/lib/run-event/helpers.js create mode 100644 src/lib/run-event/helpers.test.js diff --git a/src/lib/run-event/helpers.js b/src/lib/run-event/helpers.js new file mode 100644 index 00000000..9e24712e --- /dev/null +++ b/src/lib/run-event/helpers.js @@ -0,0 +1,21 @@ +export const formatStartMessage = ({ count, path, formFactor, locale }) => { + const message = ['Running Lighthouse on', path]; + + // Build a list of settings used for this run. + const settings = []; + if (locale) { + settings.push(`the “${locale}” locale`); + } + if (formFactor === 'desktop') { + settings.push('the “desktop” preset'); + } + if (settings.length) { + message.push(`using ${settings.join(' and ')}`); + } + + if (count?.total > 1) { + message.push(`(${count.i}/${count.total})`); + } + + return message.join(' '); +}; diff --git a/src/lib/run-event/helpers.test.js b/src/lib/run-event/helpers.test.js new file mode 100644 index 00000000..1c49e28c --- /dev/null +++ b/src/lib/run-event/helpers.test.js @@ -0,0 +1,51 @@ +import { formatStartMessage } from './helpers.js'; + +describe('formatStartMessage', () => { + it('should format a message using only the path', () => { + const result = formatStartMessage({ path: 'https://example.com/path' }); + expect(result).toEqual('Running Lighthouse on https://example.com/path'); + }); + + it('should format a message using only the path and count', () => { + const result = formatStartMessage({ + count: { i: 1, total: 2 }, + path: 'https://example.com/path', + }); + expect(result).toEqual( + 'Running Lighthouse on https://example.com/path (1/2)', + ); + }); + + it('should format a message using a single feature', () => { + const result = formatStartMessage({ + path: 'https://example.com/path', + formFactor: 'desktop', + }); + expect(result).toEqual( + 'Running Lighthouse on https://example.com/path using the “desktop” preset', + ); + }); + + it('should format a message using multiple features', () => { + const result = formatStartMessage({ + path: 'https://example.com/path', + formFactor: 'desktop', + locale: 'de', + }); + expect(result).toEqual( + 'Running Lighthouse on https://example.com/path using the “de” locale and the “desktop” preset', + ); + }); + + it('should format a message using all available inputs', () => { + const result = formatStartMessage({ + count: { i: 1, total: 2 }, + path: 'https://example.com/path', + formFactor: 'desktop', + locale: 'es', + }); + expect(result).toEqual( + 'Running Lighthouse on https://example.com/path using the “es” locale and the “desktop” preset (1/2)', + ); + }); +}); diff --git a/src/lib/run-event/index.js b/src/lib/run-event/index.js index 5fb1c461..348f910d 100644 --- a/src/lib/run-event/index.js +++ b/src/lib/run-event/index.js @@ -6,6 +6,8 @@ import runAuditWithUrl from '../../lib/run-audit-with-url/index.js'; import runAuditWithServer from '../../lib/run-audit-with-server/index.js'; import getConfiguration from '../get-configuration/index.js'; +import { formatStartMessage } from './helpers.js'; + const runEvent = async ({ event, constants, @@ -57,12 +59,14 @@ const runEvent = async ({ const { serveDir, path, url, thresholds, output_path } = auditConfig; const fullPath = [serveDir, path].join('/'); - let countMessage = ''; - if (auditConfigs.length > 1) { - countMessage = ` (${i}/${auditConfigs.length})`; - } + const startMessage = formatStartMessage({ + count: { i, total: auditConfigs.length }, + path: fullPath, + formFactor: settings?.settings.formFactor, + locale: settings?.settings.locale, + }); - console.log(`Running Lighthouse on ${fullPath}${countMessage}`); + console.log(startMessage); const runner = isOnSuccess ? runAuditWithUrl : runAuditWithServer; const { errors, summary, shortSummary, details, report, runtimeError } =