Skip to content

Commit adfbd10

Browse files
authored
feat(cli): write HTML reports to .lighthouseci (#139)
1 parent afd1b3b commit adfbd10

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

packages/cli/test/test-utils.js

+9
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
*/
66
'use strict';
77

8+
const os = require('os');
89
const fs = require('fs');
910
const path = require('path');
11+
const rimraf = require('rimraf');
1012
const {spawn, spawnSync} = require('child_process');
1113
const testingLibrary = require('@testing-library/dom');
1214

@@ -42,6 +44,12 @@ async function safeDeleteFile(filePath) {
4244
}
4345
}
4446

47+
async function withTmpDir(fn) {
48+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'lighthouse-ci-'));
49+
await fn(tmpDir);
50+
rimraf.sync(tmpDir);
51+
}
52+
4553
async function startServer(sqlFile) {
4654
if (!sqlFile) {
4755
sqlFile = getSqlFilePath();
@@ -106,4 +114,5 @@ module.exports = {
106114
waitForCondition,
107115
getSqlFilePath,
108116
safeDeleteFile,
117+
withTmpDir,
109118
};

packages/utils/src/saved-reports.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ function loadSavedLHRs() {
3636
/**
3737
* @param {string} lhr
3838
*/
39-
function saveLHR(lhr) {
40-
const filename = `lhr-${Date.now()}.json`;
41-
const filePath = path.join(LHCI_DIR, filename);
39+
function saveLHR(lhr, baseDir = LHCI_DIR) {
40+
const baseFilename = `lhr-${Date.now()}`;
41+
const basePath = path.join(baseDir, baseFilename);
4242
ensureDirectoryExists();
43-
fs.writeFileSync(filePath, lhr);
43+
fs.writeFileSync(`${basePath}.json`, lhr);
44+
fs.writeFileSync(`${basePath}.html`, getHTMLReportForLHR(JSON.parse(lhr)));
4445
}
4546

4647
/**

packages/utils/test/saved-reports.test.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
/* eslint-env jest */
99

10-
const {replaceUrlPatterns} = require('@lhci/utils/src/saved-reports.js');
10+
const fs = require('fs');
11+
const path = require('path');
12+
const {withTmpDir} = require('../../cli/test/test-utils.js');
13+
const {replaceUrlPatterns, saveLHR} = require('@lhci/utils/src/saved-reports.js');
1114

12-
describe('replaceUrlPatterns', () => {
15+
describe('#replaceUrlPatterns', () => {
1316
it('should replace basic patterns', () => {
1417
const patterns = ['s/foo/bar/'];
1518
expect(replaceUrlPatterns('https://foo.com', patterns)).toEqual('https://bar.com');
@@ -45,3 +48,30 @@ describe('replaceUrlPatterns', () => {
4548
expect(() => replaceUrlPatterns('foo', ['nothing'])).toThrow();
4649
});
4750
});
51+
52+
describe('#saveLHR', () => {
53+
it('should save the lhr to json', async () => {
54+
await withTmpDir(dir => {
55+
saveLHR(JSON.stringify({lighthouseVersion: '5.6.0'}), dir);
56+
const files = fs.readdirSync(dir);
57+
expect(files.map(name => name.replace(/-\d+/, '-XXX'))).toContain('lhr-XXX.json');
58+
59+
const jsonFilePath = path.join(dir, files.find(f => f.endsWith('.json')));
60+
const contents = fs.readFileSync(jsonFilePath, 'utf8');
61+
expect(contents).toEqual(`{"lighthouseVersion":"5.6.0"}`);
62+
});
63+
});
64+
65+
it('should save the lhr to html', async () => {
66+
await withTmpDir(dir => {
67+
saveLHR(JSON.stringify({lighthouseVersion: '5.6.0'}), dir);
68+
const files = fs.readdirSync(dir);
69+
expect(files.map(name => name.replace(/-\d+/, '-XXX'))).toContain('lhr-XXX.html');
70+
71+
const jsonFilePath = path.join(dir, files.find(f => f.endsWith('.html')));
72+
const contents = fs.readFileSync(jsonFilePath, 'utf8');
73+
expect(contents).toMatch(/<!DOCTYPE html>/i);
74+
expect(contents).toMatch(/__LIGHTHOUSE_JSON__ = /);
75+
});
76+
});
77+
});

0 commit comments

Comments
 (0)