lighthouse-reporting 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +314 -2
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -4,10 +4,322 @@
4
4
 
5
5
  The reports include trend history support, allowing you to track performance improvements over time.
6
6
 
7
+ <p align="center">
8
+ <img alt="HTML" src="./docs/html_report.png" width="45%">
9
+ &nbsp; &nbsp; &nbsp; &nbsp;
10
+ <img alt="Plot" src="./docs/plot_report.png" width="45%">
11
+ </p>
12
+
7
13
  ## Usage
8
14
 
9
- ### Jenkins
15
+ Examples of usage with Playwright + Lighthouse (and Storybook).
16
+
17
+ ### In your frontend or testing framework
18
+ The example of usage [playwright](https://github.com/microsoft/playwright) and [playwright-lighthouse](https://github.com/abhinaba-ghosh/playwright-lighthouse) together.
19
+
20
+ <details>
21
+ <summary>lighthouse_pages.spec</summary>
22
+
23
+ ```ts
24
+ import path from 'path'
25
+ import { playAudit } from 'playwright-lighthouse'
26
+ import { playwrightLighthouseTest, getScores, writeCsvResult, writeHtmlListEntryWithRetry, LighthouseResult } from 'lighthouse-reporting'
27
+ import { MyPage1 } from '../../pages/my-page-1.page.js'
28
+ import { MyPage2 } from '../../pages/my-page-1.page.js'
29
+
30
+ playwrightLighthouseTest.setTimeout(60000)
31
+ const reportDir = path.join(process.cwd(), 'lighthouse')
32
+ const htmlFilePath = path.join(reportDir, 'index.html')
33
+
34
+ const swimlanes = ['performance']
35
+ const lighthousePages = [
36
+ { name: 'MyPage1', po: MyPage1, thresholds: { performance: 80, accessibility: 88, seo: 92 }, swimlanes },
37
+ { name: 'MyPage2', po: MyPage2, thresholds: { performance: 90, accessibility: 100, seo: 90 }, swimlanes },
38
+ ]
39
+
40
+ lighthousePages.forEach(({ name, po, thresholds, swimlanes }) => {
41
+ playwrightLighthouseTest(name, async ({ port, baseURL }) => {
42
+ const onlyCategories = ['accessibility', 'seo', 'performance']
43
+
44
+ const result: LighthouseResult = await playAudit({
45
+ url: baseURL + po.getPath('123'),
46
+ port,
47
+ thresholds,
48
+ reports: {
49
+ formats: {
50
+ html: true,
51
+ },
52
+ name,
53
+ directory: reportDir,
54
+ },
55
+ opts: {
56
+ onlyCategories,
57
+ screenEmulation: { disabled: true },
58
+ },
59
+ disableLogs: true,
60
+ ignoreError: true,
61
+ })
62
+
63
+ const scores = getScores(result)
64
+ await writeCsvResult(reportDir, name, scores, thresholds, swimlanes)
65
+ await writeHtmlListEntryWithRetry(htmlFilePath, name, scores, thresholds, result.comparisonError)
66
+
67
+ if (result.comparisonError) {
68
+ throw new Error(result.comparisonError)
69
+ }
70
+ })
71
+ })
72
+ ```
73
+
74
+ </details>
75
+
76
+ ### Or in Storybook
77
+
78
+ An example for storybook with lighthouse and screenshot testing
79
+
80
+ <details>
81
+ <summary>storybook.spec.ts</summary>
82
+
83
+ ```ts
84
+ import path from 'path'
85
+ import { BrowserContext } from '@playwright/test'
86
+ import { playAudit } from 'playwright-lighthouse'
87
+ import {
88
+ playwrightLighthouseTest,
89
+ getScores,
90
+ writeCsvResult,
91
+ writeHtmlListEntryWithRetry,
92
+ LighthouseResult,
93
+ StorybookIndexStory,
94
+ storybookPlaywright,
95
+ } from 'lighthouse-reporting'
96
+
97
+ playwrightLighthouseTest.setTimeout(60000)
98
+ const reportDir = path.join(process.cwd(), process.env.LH_REPORT_DIR as string)
99
+ const htmlFilePath = path.join(reportDir, 'index.html')
100
+
101
+ const stories = storybookPlaywright.getStories('./storybook-static/index.json', (story) => {
102
+ // skip docs, etc
103
+ if (story.type !== 'story') {
104
+ return false
105
+ }
106
+ // only include stories with test tag
107
+ if (!story.tags.includes('test')) {
108
+ return false
109
+ }
110
+ return true
111
+ })
112
+
113
+ stories.forEach((story) => {
114
+ playwrightLighthouseTest(`${story.title} - ${story.name}`, async ({ context, port, baseURL }) => {
115
+ await runLighthouse(story, context, port, baseURL)
116
+ await storybookPlaywright.captureScreenshot(story, context)
117
+ })
118
+ })
119
+
120
+ const runLighthouse = async (story: StorybookIndexStory, context: BrowserContext, port: number, baseURL?: string) => {
121
+ const onlyCategories = ['accessibility']
122
+ const thresholds = { accessibility: 100 }
123
+ const name = story.id
124
+
125
+ const page = context.pages()[0]
126
+ await page.goto(`/iframe.html?id=${story.id}`)
127
+
128
+ const result: LighthouseResult = await playAudit({
129
+ url: baseURL + `/iframe.html?id=${story.id}`,
130
+ port,
131
+ thresholds,
132
+ reports: {
133
+ formats: {
134
+ html: true,
135
+ },
136
+ name,
137
+ directory: reportDir,
138
+ },
139
+ opts: {
140
+ onlyCategories,
141
+ screenEmulation: { disabled: true },
142
+ },
143
+ disableLogs: true,
144
+ ignoreError: true,
145
+ })
146
+
147
+ const scores = getScores(result)
148
+ await writeCsvResult(reportDir, name, scores, thresholds)
149
+ await writeHtmlListEntryWithRetry(htmlFilePath, name, scores, thresholds, result.comparisonError)
150
+ }
151
+ ```
152
+
153
+ </details>
154
+
155
+ ### Configs examples
156
+
157
+ <details>
158
+ <summary>playwright.storybook.config.ts</summary>
159
+
160
+ ```ts
161
+ import { PlaywrightTestConfig } from '@playwright/test'
162
+
163
+ const baseURL = 'http://127.0.0.1:6009'
164
+ // process.env.LH_REPORT_DIR = 'lighthouse-storybook' // adjust lighthouse output folder if required
165
+
166
+ const config: PlaywrightTestConfig = {
167
+ use: {
168
+ viewport: { width: 1280, height: 820 },
169
+ ignoreHTTPSErrors: true,
170
+ acceptDownloads: false,
171
+ trace: 'off',
172
+ baseURL,
173
+ screenshot: { mode: 'off' },
174
+ },
175
+ projects: [
176
+ {
177
+ name: 'chromium',
178
+ use: {
179
+ browserName: 'chromium',
180
+ launchOptions: { args: ['--disable-gpu'] },
181
+ },
182
+ retries: 0,
183
+ },
184
+ ],
185
+ expect: { toMatchSnapshot: { threshold: 0.2 } },
186
+ reporter: 'line',
187
+ testDir: 'test/storybook',
188
+ testMatch: '*.spec.ts',
189
+ fullyParallel: true,
190
+ globalSetup: './src/global-setup.ts',
191
+ globalTeardown: './src/global-teardown.ts',
192
+ forbidOnly: true,
193
+ webServer: [
194
+ {
195
+ command: 'npx http-server ./storybook-static --port 6009 --silent',
196
+ url: `${baseURL}/index.json`,
197
+ timeout: 15 * 1000,
198
+ reuseExistingServer: false,
199
+ ignoreHTTPSErrors: true,
200
+ },
201
+ ],
202
+ }
203
+ export default config
204
+ ```
205
+
206
+ </details>
207
+
208
+ <details>
209
+ <summary>global-setup.ts</summary>
210
+
211
+ ```ts
212
+ import { PlaywrightTestConfig } from '@playwright/test'
213
+
214
+ const baseURL = 'http://127.0.0.1:6009'
215
+ // process.env.LH_REPORT_DIR = 'lighthouse-storybook' // adjust lighthouse output folder if required
216
+
217
+ const config: PlaywrightTestConfig = {
218
+ use: {
219
+ viewport: { width: 1280, height: 820 },
220
+ ignoreHTTPSErrors: true,
221
+ acceptDownloads: false,
222
+ trace: 'off',
223
+ baseURL,
224
+ screenshot: { mode: 'off' },
225
+ },
226
+ projects: [
227
+ {
228
+ name: 'chromium',
229
+ use: {
230
+ browserName: 'chromium',
231
+ launchOptions: { args: ['--disable-gpu'] },
232
+ },
233
+ retries: 0,
234
+ },
235
+ ],
236
+ expect: { toMatchSnapshot: { threshold: 0.2 } },
237
+ reporter: 'line',
238
+ testDir: 'test/storybook',
239
+ testMatch: '*.spec.ts',
240
+ fullyParallel: true,
241
+ globalSetup: './src/global-setup.ts',
242
+ globalTeardown: './src/global-teardown.ts',
243
+ forbidOnly: true,
244
+ webServer: [
245
+ {
246
+ command: 'npx http-server ./storybook-static --port 6009 --silent',
247
+ url: `${baseURL}/index.json`,
248
+ timeout: 15 * 1000,
249
+ reuseExistingServer: false,
250
+ ignoreHTTPSErrors: true,
251
+ },
252
+ ],
253
+ }
254
+ export default config
255
+ ```
256
+
257
+ </details>
258
+
259
+ <details>
260
+ <summary>global-teardown.ts</summary>
261
+
262
+ ```ts
263
+ import { lighthousePlaywrightTeardown } from 'lighthouse-reporting'
264
+
265
+ async function globalTeardown() {
266
+ await lighthousePlaywrightTeardown()
267
+ }
268
+
269
+ export default globalTeardown
270
+ ```
271
+
272
+ </details>
273
+
274
+ ## Jenkins
275
+
276
+ Plugins used [HTML Publisher](https://plugins.jenkins.io/htmlpublisher/) and [Plot](https://plugins.jenkins.io/plot/).
277
+
278
+ <details>
279
+ <summary>Jenkinsfile</summary>
280
+
281
+ ```
282
+ stage('Lighthouse') {
283
+ steps {
284
+ # run playwright-lighthouse tests
285
+ }
286
+
287
+ post {
288
+ always {
289
+ # html report per build
290
+ publishHTML(target: [
291
+ reportName : 'Lighthouse',
292
+ reportDir : "$WORKSPACE/lighthouse",
293
+ reportFiles : 'index.html',
294
+ keepAll : true,
295
+ alwaysLinkToLastBuild: true,
296
+ allowMissing : false
297
+ ])
298
+
299
+ # csv trend history report
300
+ script {
301
+ csvFiles = findFiles(glob: 'lighthouse/*.csv')
302
+ for (csvFile in csvFiles) {
303
+ filePath = "${csvFile}"
304
+ plot(csvFileName: filePath.substring(filePath.lastIndexOf("/") + 1),
305
+ csvSeries: [[displayTableFlag: false, exclusionValues: '', file: filePath, inclusionFlag: 'OFF', url: '']],
306
+ exclZero: true,
307
+ group: 'my app',
308
+ numBuilds: '100',
309
+ style: 'line',
310
+ title: filePath.substring(filePath.lastIndexOf("/") + 1, filePath.indexOf(".")),
311
+ yaxis: 'score',
312
+ yaxisMaximum: '101',
313
+ yaxisMinimum: '15') # adjust scale
314
+ }
315
+ }
316
+ }
317
+ }
318
+ }
319
+ ```
320
+
321
+ </details>
10
322
 
11
- ### GitHub Actions
323
+ ## GitHub Actions
12
324
 
13
325
  TODO
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lighthouse-reporting",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "vite build && vite build -c vite.umd.config.ts && tsc -p ./tsconfig.build.json",
@@ -41,7 +41,7 @@
41
41
  "prettier": "^2.8.8",
42
42
  "semantic-release": "^21.0.2",
43
43
  "typescript": "^5.0.4",
44
- "vite": "^4.3.8",
44
+ "vite": "^4.3.9",
45
45
  "vite-plugin-static-copy": "^0.15.0"
46
46
  },
47
47
  "dependencies": {