lighthouse 9.5.0-dev.20221113 → 9.5.0-dev.20221115

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.
package/core/index.js CHANGED
@@ -10,9 +10,13 @@ import {Runner} from './runner.js';
10
10
  import {CriConnection} from './legacy/gather/connections/cri.js';
11
11
  import {Config} from './legacy/config/config.js';
12
12
  import UrlUtils from './lib/url-utils.js';
13
- import * as fraggleRock from './api.js';
14
13
  import {Driver} from './legacy/gather/driver.js';
15
14
  import {initializeConfig} from './config/config.js';
15
+ import {UserFlow, auditGatherSteps} from './user-flow.js';
16
+ import {ReportGenerator} from '../report/generator/report-generator.js';
17
+ import {startTimespanGather} from './gather/timespan-runner.js';
18
+ import {snapshotGather} from './gather/snapshot-runner.js';
19
+ import {navigationGather} from './gather/navigation-runner.js';
16
20
 
17
21
  /** @typedef {import('./legacy/gather/connections/connection.js').Connection} Connection */
18
22
 
@@ -40,7 +44,7 @@ import {initializeConfig} from './config/config.js';
40
44
  * @return {Promise<LH.RunnerResult|undefined>}
41
45
  */
42
46
  async function lighthouse(url, flags = {}, configJSON, page) {
43
- return fraggleRock.navigation(page, url, {config: configJSON, flags});
47
+ return navigation(page, url, {config: configJSON, flags});
44
48
  }
45
49
 
46
50
  /**
@@ -73,6 +77,75 @@ async function legacyNavigation(url, flags = {}, configJSON, userConnection) {
73
77
  return Runner.audit(artifacts, options);
74
78
  }
75
79
 
80
+ /**
81
+ * @param {LH.Puppeteer.Page} page
82
+ * @param {LH.UserFlow.Options} [options]
83
+ */
84
+ async function startFlow(page, options) {
85
+ return new UserFlow(page, options);
86
+ }
87
+
88
+ /**
89
+ * @param {LH.Puppeteer.Page|undefined} page
90
+ * @param {LH.NavigationRequestor|undefined} requestor
91
+ * @param {{config?: LH.Config.Json, flags?: LH.Flags}} [options]
92
+ * @return {Promise<LH.RunnerResult|undefined>}
93
+ */
94
+ async function navigation(page, requestor, options) {
95
+ const gatherResult = await navigationGather(page, requestor, options);
96
+ return Runner.audit(gatherResult.artifacts, gatherResult.runnerOptions);
97
+ }
98
+
99
+ /**
100
+ * @param {LH.Puppeteer.Page} page
101
+ * @param {{config?: LH.Config.Json, flags?: LH.Flags}} [options]
102
+ * @return {Promise<LH.RunnerResult|undefined>}
103
+ */
104
+ async function snapshot(page, options) {
105
+ const gatherResult = await snapshotGather(page, options);
106
+ return Runner.audit(gatherResult.artifacts, gatherResult.runnerOptions);
107
+ }
108
+
109
+ /**
110
+ * @param {LH.Puppeteer.Page} page
111
+ * @param {{config?: LH.Config.Json, flags?: LH.Flags}} [options]
112
+ * @return {Promise<{endTimespan: () => Promise<LH.RunnerResult|undefined>}>}
113
+ */
114
+ async function startTimespan(page, options) {
115
+ const {endTimespanGather} = await startTimespanGather(page, options);
116
+ const endTimespan = async () => {
117
+ const gatherResult = await endTimespanGather();
118
+ return Runner.audit(gatherResult.artifacts, gatherResult.runnerOptions);
119
+ };
120
+ return {endTimespan};
121
+ }
122
+
123
+ /**
124
+ * @template {LH.Result|LH.FlowResult} R
125
+ * @param {R} result
126
+ * @param {[R] extends [LH.Result] ? LH.OutputMode : Exclude<LH.OutputMode, 'csv'>} [format]
127
+ * @return {string}
128
+ */
129
+ function generateReport(result, format = 'html') {
130
+ const reportOutput = ReportGenerator.generateReport(result, format);
131
+ if (Array.isArray(reportOutput)) {
132
+ // In theory the output should never be an array.
133
+ // This is mostly for type checking.
134
+ return reportOutput[0];
135
+ } else {
136
+ return reportOutput;
137
+ }
138
+ }
139
+
140
+ /**
141
+ * @param {LH.UserFlow.FlowArtifacts} flowArtifacts
142
+ * @param {LH.Config.Json} [config]
143
+ */
144
+ async function auditFlowArtifacts(flowArtifacts, config) {
145
+ const {gatherSteps, name} = flowArtifacts;
146
+ return await auditGatherSteps(gatherSteps, {name, config});
147
+ }
148
+
76
149
  /**
77
150
  * Generate a Lighthouse Config.
78
151
  * @param {LH.Config.Json=} configJson Configuration for the Lighthouse run. If
@@ -113,6 +186,12 @@ export {default as Gatherer} from './gather/base-gatherer.js';
113
186
  export {NetworkRecords} from './computed/network-records.js';
114
187
  export {
115
188
  legacyNavigation,
189
+ startFlow,
190
+ navigation,
191
+ startTimespan,
192
+ snapshot,
193
+ generateReport,
194
+ auditFlowArtifacts,
116
195
  generateConfig,
117
196
  generateLegacyConfig,
118
197
  getAuditList,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "lighthouse",
3
3
  "type": "module",
4
- "version": "9.5.0-dev.20221113",
4
+ "version": "9.5.0-dev.20221115",
5
5
  "description": "Automated auditing, performance metrics, and best practices for the web.",
6
6
  "main": "./core/index.js",
7
7
  "bin": {
@@ -146,28 +146,42 @@ class ReportGenerator {
146
146
  .join(CRLF);
147
147
  }
148
148
 
149
+ /**
150
+ * @param {LHResult|FlowResult} result
151
+ * @return {result is FlowResult}
152
+ */
153
+ static isFlowResult(result) {
154
+ return 'steps' in result;
155
+ }
156
+
149
157
  /**
150
158
  * Creates the results output in a format based on the `mode`.
151
- * @param {LHResult} lhr
159
+ * @param {LHResult|FlowResult} result
152
160
  * @param {LHResult['configSettings']['output']} outputModes
153
161
  * @return {string|string[]}
154
162
  */
155
- static generateReport(lhr, outputModes) {
163
+ static generateReport(result, outputModes) {
156
164
  const outputAsArray = Array.isArray(outputModes);
157
165
  if (typeof outputModes === 'string') outputModes = [outputModes];
158
166
 
159
167
  const output = outputModes.map(outputMode => {
160
168
  // HTML report.
161
169
  if (outputMode === 'html') {
162
- return ReportGenerator.generateReportHtml(lhr);
170
+ if (ReportGenerator.isFlowResult(result)) {
171
+ return ReportGenerator.generateFlowReportHtml(result);
172
+ }
173
+ return ReportGenerator.generateReportHtml(result);
163
174
  }
164
175
  // CSV report.
165
176
  if (outputMode === 'csv') {
166
- return ReportGenerator.generateReportCSV(lhr);
177
+ if (ReportGenerator.isFlowResult(result)) {
178
+ throw new Error('CSV output is not support for user flows');
179
+ }
180
+ return ReportGenerator.generateReportCSV(result);
167
181
  }
168
182
  // JSON report.
169
183
  if (outputMode === 'json') {
170
- return JSON.stringify(lhr, null, 2);
184
+ return JSON.stringify(result, null, 2);
171
185
  }
172
186
 
173
187
  throw new Error('Invalid output mode: ' + outputMode);
@@ -16,6 +16,7 @@ import {ReportGenerator} from '../../generator/report-generator.js';
16
16
  import {readJson} from '../../../core/test/test-utils.js';
17
17
 
18
18
  const sampleResults = readJson('core/test/results/sample_v2.json');
19
+ const sampleFlowResult = readJson('core/test/fixtures/fraggle-rock/reports/sample-flow-result.json');
19
20
 
20
21
  describe('ReportGenerator', () => {
21
22
  describe('#replaceStrings', () => {
@@ -75,12 +76,23 @@ describe('ReportGenerator', () => {
75
76
  assert.doesNotThrow(_ => JSON.parse(jsonOutput));
76
77
  });
77
78
 
79
+ it('creates JSON for flow result', () => {
80
+ const jsonOutput = ReportGenerator.generateReport(sampleFlowResult, 'json');
81
+ assert.doesNotThrow(_ => JSON.parse(jsonOutput));
82
+ });
83
+
78
84
  it('creates HTML for results', () => {
79
85
  const htmlOutput = ReportGenerator.generateReport(sampleResults, 'html');
80
86
  assert.ok(/<!doctype/gim.test(htmlOutput));
81
87
  assert.ok(/<html lang="en"/gim.test(htmlOutput));
82
88
  });
83
89
 
90
+ it('creates HTML for flow result', () => {
91
+ const htmlOutput = ReportGenerator.generateReport(sampleFlowResult, 'html');
92
+ assert.ok(/<!doctype/gim.test(htmlOutput));
93
+ assert.ok(/<html lang="en"/gim.test(htmlOutput));
94
+ });
95
+
84
96
  it('creates CSV for results', async () => {
85
97
  const path = './.results-as-csv.csv';
86
98
 
@@ -126,6 +138,12 @@ category,audit,score,displayValue,description
126
138
  expect(csvOutput).toContain('pwa');
127
139
  });
128
140
 
141
+ it('throws when creating CSV for flow result', () => {
142
+ expect(() => {
143
+ ReportGenerator.generateReport(sampleFlowResult, 'csv');
144
+ }).toThrow('CSV output is not support for user flows');
145
+ });
146
+
129
147
  it('writes extended info', () => {
130
148
  const htmlOutput = ReportGenerator.generateReport(sampleResults, 'html');
131
149
  const outputCheck = new RegExp('dobetterweb/dbw_tester.css', 'i');
package/core/api.js DELETED
@@ -1,80 +0,0 @@
1
- /**
2
- * @license Copyright 2020 The Lighthouse Authors. All Rights Reserved.
3
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
4
- * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
5
- */
6
-
7
- import {UserFlow, auditGatherSteps} from './user-flow.js';
8
- import {snapshotGather} from './gather/snapshot-runner.js';
9
- import {startTimespanGather} from './gather/timespan-runner.js';
10
- import {navigationGather} from './gather/navigation-runner.js';
11
- import {ReportGenerator} from '../report/generator/report-generator.js';
12
- import {Runner} from './runner.js';
13
-
14
- /**
15
- * @param {LH.Puppeteer.Page} page
16
- * @param {LH.UserFlow.Options} [options]
17
- */
18
- async function startFlow(page, options) {
19
- return new UserFlow(page, options);
20
- }
21
-
22
- /**
23
- * @param {LH.Puppeteer.Page|undefined} page
24
- * @param {LH.NavigationRequestor|undefined} requestor
25
- * @param {{config?: LH.Config.Json, flags?: LH.Flags}} [options]
26
- * @return {Promise<LH.RunnerResult|undefined>}
27
- */
28
- async function navigation(page, requestor, options) {
29
- const gatherResult = await navigationGather(page, requestor, options);
30
- return Runner.audit(gatherResult.artifacts, gatherResult.runnerOptions);
31
- }
32
-
33
- /**
34
- * @param {LH.Puppeteer.Page} page
35
- * @param {{config?: LH.Config.Json, flags?: LH.Flags}} [options]
36
- * @return {Promise<LH.RunnerResult|undefined>}
37
- */
38
- async function snapshot(page, options) {
39
- const gatherResult = await snapshotGather(page, options);
40
- return Runner.audit(gatherResult.artifacts, gatherResult.runnerOptions);
41
- }
42
-
43
- /**
44
- * @param {LH.Puppeteer.Page} page
45
- * @param {{config?: LH.Config.Json, flags?: LH.Flags}} [options]
46
- * @return {Promise<{endTimespan: () => Promise<LH.RunnerResult|undefined>}>}
47
- */
48
- async function startTimespan(page, options) {
49
- const {endTimespanGather} = await startTimespanGather(page, options);
50
- const endTimespan = async () => {
51
- const gatherResult = await endTimespanGather();
52
- return Runner.audit(gatherResult.artifacts, gatherResult.runnerOptions);
53
- };
54
- return {endTimespan};
55
- }
56
-
57
- /**
58
- * @param {LH.FlowResult} flowResult
59
- */
60
- async function generateFlowReport(flowResult) {
61
- return ReportGenerator.generateFlowReportHtml(flowResult);
62
- }
63
-
64
- /**
65
- * @param {LH.UserFlow.FlowArtifacts} flowArtifacts
66
- * @param {LH.Config.Json} [config]
67
- */
68
- async function auditFlowArtifacts(flowArtifacts, config) {
69
- const {gatherSteps, name} = flowArtifacts;
70
- return await auditGatherSteps(gatherSteps, {name, config});
71
- }
72
-
73
- export {
74
- snapshot,
75
- startTimespan,
76
- navigation,
77
- startFlow,
78
- generateFlowReport,
79
- auditFlowArtifacts,
80
- };