Skip to content

Commit b1610ea

Browse files
author
Marvin Frachet
authored
Options, Thresholds and Config in cypress.json (#132)
1 parent 1647d52 commit b1610ea

File tree

5 files changed

+56
-12
lines changed

5 files changed

+56
-12
lines changed
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
{
22
"baseUrl": "http://localhost:3000",
3-
"video": false
3+
"video": false,
4+
"lighthouse": {
5+
"options": {
6+
"formFactor": "desktop",
7+
"screenEmulation": {
8+
"width": 500,
9+
"height": 940,
10+
"deviceScaleRatio": 1,
11+
"mobile": false,
12+
"disable": false
13+
}
14+
}
15+
}
416
}

examples/cra-authenticated/cypress/integration/examples/homepage.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ context("The App", () => {
2222

2323
cy.lighthouse({
2424
performance: 50,
25-
"first-contentful-paint": 3000,
25+
"first-contentful-paint": 3500,
2626
accessibility: 50,
2727
"best-practices": 50,
2828
seo: 50,

examples/cra-authenticated/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/lighthouse/README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,13 @@ While I would recommend to make per-test assumptions, it's possible to define ge
105105
```json
106106
{
107107
"lighthouse": {
108-
"performance": 85,
109-
"accessibility": 50,
110-
"best-practices": 85,
111-
"seo": 85,
112-
"pwa": 50
108+
"thresholds": {
109+
"performance": 85,
110+
"accessibility": 50,
111+
"best-practices": 85,
112+
"seo": 85,
113+
"pwa": 50
114+
}
113115
}
114116
}
115117
```
@@ -136,6 +138,25 @@ const lighthouseConfig = {
136138
cy.lighthouse(thresholds, lighthouseOptions, lighthouseConfig);
137139
```
138140

141+
## Globally set options and configs
142+
143+
You can set default `lighthouseOptions` and `lighthouseConfig` to your `cypress.json` file using:
144+
145+
```json
146+
{
147+
"lighthouse": {
148+
"options": {
149+
/* put your options here, like formFactor by default */
150+
},
151+
"config": {
152+
/* put your config here */
153+
}
154+
}
155+
}
156+
```
157+
158+
These values can be override at the test level.
159+
139160
### Available metrics
140161

141162
With Lighthouse 6, we're now able to make assumptions on **categories** and **audits**.

packages/lighthouse/src/command-handler.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,18 @@ const lighthouseCommandHandler = (thresholds, opts, config) => {
2121
}
2222

2323
return cy.url().then((url) => {
24-
const configThresholds = Cypress.config("lighthouse");
24+
// Handling the default value in cypress.json for "thresholds", "config" and "options"
25+
const lighthouseConfig = Cypress.config("lighthouse");
26+
27+
const configThresholds = lighthouseConfig
28+
? lighthouseConfig.thresholds
29+
: undefined;
30+
31+
const globalOptions = lighthouseConfig
32+
? lighthouseConfig.options
33+
: undefined;
34+
35+
const globalConfig = lighthouseConfig ? lighthouseConfig.config : undefined;
2536

2637
if (!thresholds && !configThresholds) {
2738
cy.log(
@@ -35,8 +46,8 @@ const lighthouseCommandHandler = (thresholds, opts, config) => {
3546
.task("lighthouse", {
3647
url,
3748
thresholds: thresholds || configThresholds || defaultThresholds,
38-
opts,
39-
config,
49+
opts: opts || globalOptions,
50+
config: config || globalConfig,
4051
})
4152
.then(({ errors, results }) => {
4253
results.forEach((res) => {

0 commit comments

Comments
 (0)