Skip to content

Commit 4901ae9

Browse files
authored
adds info on how to generate html reports (#141)
1 parent 874ec75 commit 4901ae9

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

packages/lighthouse/README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ The audits are things like the first meaningful paint and the score is provided
194194

195195
When using custom tools, it can be convenient to directly access the raw information they provide for doing manual things, such as generating a custom reports.
196196

197-
To do so, you can pass a `callback` function to the task initializer. Then, when an audit is run, this callback will we executed with the raw data of the underlying tool.
197+
To do so, you can pass a `callback` function to the task initializer. Then, when an audit is run, this callback will be executed with the raw data of the underlying tool.
198198

199199
In the `cypress/plugins/index.js` file:
200200

@@ -214,6 +214,56 @@ module.exports = (on, config) => {
214214
};
215215
```
216216

217+
#### Generating HTML reports
218+
219+
In order to have lighthouse's HTML reports available in your filesystem, you'll need to first specify `html` as the ouput for your lighthouseConfig.
220+
221+
```js
222+
const thresholds = {
223+
/* ... */
224+
};
225+
226+
const lighthouseOptions = {
227+
/* ... your lighthouse options */
228+
};
229+
230+
const lighthouseConfig = {
231+
output: 'html' //If output is not specified, then the json report will be generated
232+
/* ... your lighthouse configs */
233+
};
234+
235+
cy.lighthouse(thresholds, lighthouseOptions, lighthouseConfig);
236+
```
237+
238+
Secondly, whilst reading the raw report use `fs` to write the HTML report to disk.
239+
240+
```javascript
241+
const { lighthouse, prepareAudit } = require("@cypress-audit/lighthouse");
242+
const fs = require('fs');
243+
244+
module.exports = (on, config) => {
245+
on("before:browser:launch", (browser = {}, launchOptions) => {
246+
prepareAudit(launchOptions);
247+
});
248+
249+
on("task", {
250+
lighthouse: lighthouse((lighthouseReport) => {
251+
console.log('---- Writing lighthouse report to disk ----');
252+
253+
fs.writeFile(
254+
'lighthouse.html',
255+
lighthouseReport.report,
256+
(error: any) => {
257+
error
258+
? console.log(error)
259+
: console.log('Report created successfully');
260+
},
261+
);
262+
}),
263+
});
264+
};
265+
```
266+
217267
## Good to know
218268

219269
### Test with a production bundle

0 commit comments

Comments
 (0)