From bbb4bd560d0bf69634861f950dc814489ba89387 Mon Sep 17 00:00:00 2001 From: ChristopherPHolder Date: Thu, 12 Nov 2026 10:43:39 +0100 Subject: [PATCH 1/2] fix(cli): keep complete lighthouse configs from extending the default A flow config that lists its own artifacts and has no extends is complete. user-flow added extends: 'lighthouse:default' to it, and Lighthouse then failed merging its artifacts with the default ones (Expected object but got string). A complete config now gets no extends and takes only settings from the global config. Closes #348 --- .../mocks/user-flows/complete-config.uf.mts | 42 +++++++++ e2e/cli-e2e/tests/collect/config.e2e.test.ts | 35 ++++++++ e2e/cli-e2e/utils/setup.ts | 1 + .../commands/collect/utils/config/index.ts | 18 ++++ .../collect/utils/config/index.unit.test.ts | 88 +++++++++++++++++++ 5 files changed, 184 insertions(+) create mode 100644 e2e/cli-e2e/mocks/user-flows/complete-config.uf.mts create mode 100644 packages/cli/src/lib/commands/collect/utils/config/index.unit.test.ts diff --git a/e2e/cli-e2e/mocks/user-flows/complete-config.uf.mts b/e2e/cli-e2e/mocks/user-flows/complete-config.uf.mts new file mode 100644 index 00000000..90e75870 --- /dev/null +++ b/e2e/cli-e2e/mocks/user-flows/complete-config.uf.mts @@ -0,0 +1,42 @@ +// @ts-expect-error // This is a mock file! +import { UserFlowContext, UserFlowInteractionsFn, UserFlowProvider } from '@push-based/user-flow'; +import DevtoolsLog from 'lighthouse/core/gather/gatherers/devtools-log.js'; + +const interactions: UserFlowInteractionsFn = async (ctx: UserFlowContext): Promise => { + const { flow, collectOptions } = ctx; + const { url } = collectOptions; + + await flow.navigate(url, { + name: `Navigate to ${url}`, + }); +}; + +export default { + flowOptions: { + name: 'Complete Config Example', + // A complete config: it lists its own artifacts and does not extend the + // default config. The default config names the DevtoolsLog gatherer by + // path, this one gives its implementation. + config: { + artifacts: [ + { id: 'DevtoolsLog', gatherer: { implementation: DevtoolsLog } }, + ], + audits: ['network-requests'], + categories: { + network: { + title: 'Network', + auditRefs: [{ id: 'network-requests', weight: 0 }], + }, + }, + }, + }, + interactions, + launchOptions: { + args: [ + '--no-sandbox', + '--disable-setuid-sandbox', + '--disable-dev-shm-usage', + '--disable-gpu' + ] + } +} satisfies UserFlowProvider; diff --git a/e2e/cli-e2e/tests/collect/config.e2e.test.ts b/e2e/cli-e2e/tests/collect/config.e2e.test.ts index 7272ea00..7c73fc18 100644 --- a/e2e/cli-e2e/tests/collect/config.e2e.test.ts +++ b/e2e/cli-e2e/tests/collect/config.e2e.test.ts @@ -72,4 +72,39 @@ describe('collect config', () => { expect(stripNodeWarnings(stderr)).toBe(''); expect(code).toBe(0); }); + + it('should collect a flow with a complete LH config', async ({ + setupFns, + cli, + }) => { + setupFns.setupRcJson(DEFAULT_RC); + setupFns.setupUserFlows(USER_FLOW_MOCKS.COMPLETE_CONFIG); + + const { code, stderr } = await cli.run('user-flow', ['collect']); + + expect(stripNodeWarnings(stderr)).toBe(''); + expect(code).toBe(0); + }); + + it('should collect a complete LH config with a global config that extends', async ({ + setupFns, + cli, + }) => { + setupFns.setupRcJson({ + ...DEFAULT_RC, + collect: { + ...DEFAULT_RC.collect, + config: { + extends: 'lighthouse:default', + settings: { onlyCategories: ['network'] }, + }, + }, + }); + setupFns.setupUserFlows(USER_FLOW_MOCKS.COMPLETE_CONFIG); + + const { code, stderr } = await cli.run('user-flow', ['collect']); + + expect(stripNodeWarnings(stderr)).toBe(''); + expect(code).toBe(0); + }); }); diff --git a/e2e/cli-e2e/utils/setup.ts b/e2e/cli-e2e/utils/setup.ts index b28bb521..16fb8e4b 100644 --- a/e2e/cli-e2e/utils/setup.ts +++ b/e2e/cli-e2e/utils/setup.ts @@ -5,6 +5,7 @@ export const E2E_DIR = 'tmp/e2e'; export const USER_FLOW_MOCKS = { BASIC: 'e2e/cli-e2e/mocks/user-flows/basic-navigation.uf.mts', + COMPLETE_CONFIG: 'e2e/cli-e2e/mocks/user-flows/complete-config.uf.mts', } as const; export const DEFAULT_RC = { diff --git a/packages/cli/src/lib/commands/collect/utils/config/index.ts b/packages/cli/src/lib/commands/collect/utils/config/index.ts index 1945ec15..6cbaaddd 100644 --- a/packages/cli/src/lib/commands/collect/utils/config/index.ts +++ b/packages/cli/src/lib/commands/collect/utils/config/index.ts @@ -25,7 +25,25 @@ export function getLhConfigFromArgv(rc: Partial { + it('should be true for a config with artifacts and no extends', () => { + expect(isCompleteLhConfig(completeConfig)).toBe(true); + }); + + it('should be false for a config that extends another config', () => { + expect( + isCompleteLhConfig({ ...completeConfig, extends: 'lighthouse:default' }), + ).toBe(false); + }); + + it('should be false for a config without artifacts', () => { + expect(isCompleteLhConfig({ settings: { onlyCategories: ['performance'] } })).toBe(false); + expect(isCompleteLhConfig()).toBe(false); + }); +}); + +describe('mergeLhConfig', () => { + it('should extend the default config if none is given', () => { + expect(mergeLhConfig()).toStrictEqual({ + extends: 'lighthouse:default', + settings: {}, + }); + }); + + it('should add extends to a config without artifacts', () => { + expect( + mergeLhConfig({}, { settings: { onlyCategories: ['performance'] } }), + ).toStrictEqual({ + extends: 'lighthouse:default', + settings: { onlyCategories: ['performance'] }, + }); + }); + + it('should keep a given extends', () => { + expect( + mergeLhConfig({ extends: 'lighthouse:desktop' } as Config, {}), + ).toStrictEqual({ extends: 'lighthouse:desktop', settings: {} }); + }); + + it('should let the flow config override the global config', () => { + expect( + mergeLhConfig( + { settings: { onlyCategories: ['performance'], formFactor: 'mobile' } }, + { settings: { formFactor: 'desktop' } }, + ), + ).toStrictEqual({ + extends: 'lighthouse:default', + settings: { onlyCategories: ['performance'], formFactor: 'desktop' }, + }); + }); + + it('should not add extends to a complete flow config', () => { + const cfg = mergeLhConfig({}, completeConfig); + expect(cfg).toStrictEqual(completeConfig); + expect(cfg).not.toHaveProperty('extends'); + }); + + it('should take only settings from the global config for a complete flow config', () => { + const cfg = mergeLhConfig( + { + extends: 'lighthouse:default', + audits: ['global-audit'], + settings: { onlyCategories: ['custom'], formFactor: 'mobile' }, + }, + { ...completeConfig, settings: { formFactor: 'desktop' } }, + ); + expect(cfg).toStrictEqual({ + ...completeConfig, + settings: { onlyCategories: ['custom'], formFactor: 'desktop' }, + }); + expect(cfg).not.toHaveProperty('extends'); + }); +}); From 66719f3317a9245e4acb68f44205e230f73b1142 Mon Sep 17 00:00:00 2001 From: ChristopherPHolder Date: Fri, 13 Nov 2026 17:01:37 +0100 Subject: [PATCH 2/2] docs: explain how lighthouse configs are combined Document the order in which the global config, the flow config, flags and step options apply, when user-flow adds extends, and how complete configs are handled. The README (also the npm page), the GitHub docs and the docs site all describe the same rules. Also correct the claim that --configPath overrides the rc file: it replaces collect.configPath, and combining it with collect.config stops the run. --- README.md | 12 ++- packages/cli/docs/lh-configuraton.md | 93 +++++++++++++++++-- .../content/cli/lighthouse-configuration.md | 87 +++++++++++++++-- 3 files changed, 175 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 8faf01f1..8d0aa201 100644 --- a/README.md +++ b/README.md @@ -228,9 +228,17 @@ This command executes a set of user-flow definitions against the target URL and ## Configuration -The CLI supports the official [user-flow/lighthouse configuration](https://github.com/GoogleChrome/lighthouse/blob/master/docs/configuration.md). +The CLI supports the official [Lighthouse configuration](https://github.com/GoogleChrome/lighthouse/blob/main/docs/configuration.md). -Details on how to work with configurations can be found in the [configuratin section](./packages/cli/docs/lh-configuraton.md). +A flow gets its Lighthouse config from at most two sources: one global config for every flow (`--configPath`, or `collect.configPath` or `collect.config` in `.user-flowrc.json`) and `flowOptions.config` in the flow file. user-flow combines them like this: + +1. `settings` are merged key by key, and the flow config wins. Every other top-level key, such as `audits` or `categories`, is taken whole from the flow config if it has one, otherwise from the global config. +2. If the result has no `extends`, user-flow adds `extends: 'lighthouse:default'`, so a settings-only config still runs the default audits. +3. A flow config with `artifacts` and no `extends` is complete. It runs exactly as written, without `extends`, and takes only `settings` from the global config. To add artifacts on top of the default config, set `extends: 'lighthouse:default'` yourself. + +For a single setting, the order from lowest to highest priority is: Lighthouse default → global config → flow config → `flowOptions.flags` → options passed to a step. + +The [Lighthouse configuration guide](https://push-based.github.io/user-flow/docs/cli/lighthouse-configuration) has examples and the full rules (also on [GitHub](https://github.com/push-based/user-flow/blob/main/packages/cli/docs/lh-configuraton.md)). # Writing user flows for the CLI diff --git a/packages/cli/docs/lh-configuraton.md b/packages/cli/docs/lh-configuraton.md index e110ed7a..212036be 100644 --- a/packages/cli/docs/lh-configuraton.md +++ b/packages/cli/docs/lh-configuraton.md @@ -8,18 +8,95 @@ Lighthouse itself can get configured over a json object where any detail regards The official docs on [how to configure lighthouse](https://github.com/GoogleChrome/lighthouse/blob/main/docs/configuration.md) are located in their repository's docs. The [default lighthouse config](https://github.com/GoogleChrome/lighthouse/blob/946075bb7f0858861067d161a414bdccc0a19007/core/config/constants.js#L92) can be found in their repository's source. -The CLI will start lighthouse as with its default configuration. +Unless configured otherwise, the CLI starts lighthouse with its default configuration. -### Configuration Options +### Where configuration can live -The CLI provides multiple ways of configuration, every one with a different granularity. +Each flow gets its Lighthouse config from at most two sources: one **global** config shared by every flow, and the **flow** config in the flow file. -Places to put configurations: -- in a `.user-flowrc.json` - used for all flows (**global**) -- in the console as CLI param - used for all flows, overwrites rc file (**global**) -- in a `.uf.mts` - used for 1 specific flow (**local**) +| Level | Place | Applies to | +| ------ | ------------------------------------------- | ------------------------- | +| Global | `--configPath` on the command line | Every flow in this run | +| Global | `collect.configPath` in `.user-flowrc.json` | Every flow in the project | +| Global | `collect.config` in `.user-flowrc.json` | Every flow in the project | +| Flow | `flowOptions.config` in a `*.uf.mts` file | That one flow | -Local configurations will overwrite global configurations. +There is only ever one global config. `--configPath` on the command line replaces `collect.configPath` from the rc file. A path and an inline config can't be mixed, though: if the rc file has `collect.config` and you pass `--configPath`, the run stops with `configPath and config can't be used together`. + +### How the configs are combined + +For each flow, user-flow builds a single config and passes it to Lighthouse's `startFlow`. It gets there in four steps. + +1. The flow config is laid over the global config. `settings` are merged key by key, and the flow wins when both set the same key. Every other top-level key, such as `audits`, `categories` or `artifacts`, is taken whole from the flow config if it has one, and from the global config otherwise. They are not combined, so flow `audits` replace the global `audits`. +2. If the result has no `extends`, user-flow adds `extends: 'lighthouse:default'`. Without it, a config that only sets `settings` would run no audits at all. Configs that define their own artifacts are the exception, see [Complete configs](#complete-configs). +3. Lighthouse resolves `extends` by merging the config into its default config. Artifacts are matched by `id`, audits and category `auditRefs` are added to the default ones, and `settings` override the default settings. An array inside `settings`, such as `onlyCategories`, replaces the default array instead of extending it. A config without `extends` is used exactly as written. +4. `flowOptions.flags` and the options passed to a step override `settings` for that step. Step options, as in `flow.navigate(url, { onlyCategories: ['performance'] })`, win over `flowOptions.flags`. + +For a single setting, the order from lowest to highest priority is: + +Lighthouse default → global config → flow config → `flowOptions.flags` → step options + +#### Example + +**.user-flowrc.json (collect.config)** +```json +{ + "settings": { + "formFactor": "mobile", + "onlyCategories": ["performance", "accessibility"] + }, + "audits": ["my-global-audit"] +} +``` + +**user-flows/checkout.uf.mts (flowOptions.config)** +```typescript +config: { + settings: { formFactor: 'desktop' }, + audits: ['my-flow-audit'], +} +``` + +For the checkout flow, Lighthouse receives: + +```json +{ + "extends": "lighthouse:default", + "settings": { + "formFactor": "desktop", + "onlyCategories": ["performance", "accessibility"] + }, + "audits": ["my-flow-audit"] +} +``` + +`formFactor` comes from the flow and `onlyCategories` from the global config. `my-global-audit` is gone because the flow sets its own `audits`. Lighthouse then runs `my-flow-audit` next to its default audits. + +### Complete configs + +Some configs bring every artifact, audit and category they need and are written to run without the default config. Configs exported by Lighthouse plugin packages are a common case. If user-flow added `extends` to one of them, Lighthouse would merge its artifacts with the default ones, and that fails when both describe the same artifact differently. + +user-flow therefore treats a flow config with `artifacts` and no `extends` as complete. For a complete config it: + +- does not add `extends`, so Lighthouse runs the config as written, the same as `startFlow(page, { config })`; +- takes only `settings` from the global config, and ignores a global `extends`, `audits` or `categories` for this flow. + +**user-flows/soft-navigation.uf.mts** +```typescript +import { softNavigationConfig } from '@push-based/lighthouse-soft-navigation'; + +export default { + flowOptions: { name: 'Soft navigation', config: softNavigationConfig() }, + interactions: async ({ flow, collectOptions: { url } }) => { + await flow.navigate(url); + }, +}; +``` + +> [!IMPORTANT] +> To add your own artifacts on top of the default config, set `extends: 'lighthouse:default'` yourself. Without it the config counts as complete, and the report only contains the audits it lists. Lighthouse treats a config without `extends` the same way. + +### Where to put the config Custom configurations can be provided over a external `.json` file using `--configPath `. This option is also available in the `.user-flowrc.json` file under the `collect.configPath` property. diff --git a/packages/docs/src/content/cli/lighthouse-configuration.md b/packages/docs/src/content/cli/lighthouse-configuration.md index dd323ba1..d947b2ea 100644 --- a/packages/docs/src/content/cli/lighthouse-configuration.md +++ b/packages/docs/src/content/cli/lighthouse-configuration.md @@ -2,13 +2,86 @@ user-flow wraps [Lighthouse](https://www.npmjs.com/package/lighthouse) and runs ## Where configuration can live -The CLI accepts the same configuration at three levels of granularity. More specific levels win over more general ones. +Each flow gets its Lighthouse config from at most two sources: one **global** config shared by every flow, and the **flow** config in the flow file. -| Level | Place | Applies to | -| ------ | --------------------------------------------------------------- | --------------------------------------------- | -| Global | `collect.configPath` or `collect.config` in `.user-flowrc.json` | Every flow in the project | -| Global | `--configPath` on the command line | Every flow in this run, overrides the rc file | -| Local | `flowOptions.config` in a `*.uf.mts` file | That one flow only | +| Level | Place | Applies to | +| ------ | ------------------------------------------- | ------------------------- | +| Global | `--configPath` on the command line | Every flow in this run | +| Global | `collect.configPath` in `.user-flowrc.json` | Every flow in the project | +| Global | `collect.config` in `.user-flowrc.json` | Every flow in the project | +| Flow | `flowOptions.config` in a `*.uf.mts` file | That one flow | + +There is only ever one global config. `--configPath` on the command line replaces `collect.configPath` from the rc file. A path and an inline config can't be mixed, though: if the rc file has `collect.config` and you pass `--configPath`, the run stops with `configPath and config can't be used together`. + +## How the configs are combined + +For each flow, user-flow builds a single config and passes it to Lighthouse's `startFlow`. It gets there in four steps. + +1. The flow config is laid over the global config. `settings` are merged key by key, and the flow wins when both set the same key. Every other top-level key, such as `audits`, `categories` or `artifacts`, is taken whole from the flow config if it has one, and from the global config otherwise. They are not combined, so flow `audits` replace the global `audits`. +2. If the result has no `extends`, user-flow adds `extends: 'lighthouse:default'`. Without it, a config that only sets `settings` would run no audits at all. Configs that define their own artifacts are the exception, see [Complete configs](#complete-configs). +3. Lighthouse resolves `extends` by merging the config into its default config. Artifacts are matched by `id`, audits and category `auditRefs` are added to the default ones, and `settings` override the default settings. An array inside `settings`, such as `onlyCategories`, replaces the default array instead of extending it. A config without `extends` is used exactly as written. +4. `flowOptions.flags` and the options passed to a step override `settings` for that step. Step options, as in `flow.navigate(url, { onlyCategories: ['performance'] })`, win over `flowOptions.flags`. + +For a single setting, the order from lowest to highest priority is: + +Lighthouse default → global config → flow config → `flowOptions.flags` → step options + +### Example + +```json title=".user-flowrc.json (collect.config)" +{ + "settings": { + "formFactor": "mobile", + "onlyCategories": ["performance", "accessibility"] + }, + "audits": ["my-global-audit"] +} +``` + +```typescript title="user-flows/checkout.uf.mts (flowOptions.config)" +config: { + settings: { formFactor: 'desktop' }, + audits: ['my-flow-audit'], +} +``` + +For the checkout flow, Lighthouse receives: + +```json +{ + "extends": "lighthouse:default", + "settings": { + "formFactor": "desktop", + "onlyCategories": ["performance", "accessibility"] + }, + "audits": ["my-flow-audit"] +} +``` + +`formFactor` comes from the flow and `onlyCategories` from the global config. `my-global-audit` is gone because the flow sets its own `audits`. Lighthouse then runs `my-flow-audit` next to its default audits. + +## Complete configs + +Some configs bring every artifact, audit and category they need and are written to run without the default config. Configs exported by Lighthouse plugin packages are a common case. If user-flow added `extends` to one of them, Lighthouse would merge its artifacts with the default ones, and that fails when both describe the same artifact differently. + +user-flow therefore treats a flow config with `artifacts` and no `extends` as complete. For a complete config it: + +- does not add `extends`, so Lighthouse runs the config as written, the same as `startFlow(page, { config })`; +- takes only `settings` from the global config, and ignores a global `extends`, `audits` or `categories` for this flow. + +```typescript title="user-flows/soft-navigation.uf.mts" +import { softNavigationConfig } from '@push-based/lighthouse-soft-navigation'; + +export default { + flowOptions: { name: 'Soft navigation', config: softNavigationConfig() }, + interactions: async ({ flow, collectOptions: { url } }) => { + await flow.navigate(url); + }, +}; +``` + +> [!IMPORTANT] +> To add your own artifacts on top of the default config, set `extends: 'lighthouse:default'` yourself. Without it the config counts as complete, and the report only contains the audits it lists. Lighthouse treats a config without `extends` the same way. ## Option reference @@ -77,7 +150,7 @@ Small configurations can go straight into the rc file under `collect.config`: ## Configure a single flow -`flowOptions` accepts a `config` property with the same shape. It applies to that flow only and overrides the global configuration: +`flowOptions` accepts a `config` property with the same shape. It applies to that flow only and is combined with the global configuration as described in [How the configs are combined](#how-the-configs-are-combined): ```typescript title="user-flows/checkout.uf.mts" import { UserFlowProvider } from '@push-based/user-flow';