Skip to content

Commit d770a4c

Browse files
author
David Benson
committed
Merge remote-tracking branch 'origin/master' into feature/add_devtools_viewer_link_to_sheets
2 parents cddb412 + cc95684 commit d770a4c

10 files changed

Lines changed: 5481 additions & 10 deletions

File tree

lib/expectations.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ function validateMetrics(metrics: ExpectationMetrics) {
99

1010
if (!metrics || !metricsKeys.length) {
1111
console.error(getMessageWithPrefix('ERROR', 'NO_METRICS'));
12-
process.exit(0);
12+
process.exit(1);
1313
}
1414

1515
metricsKeys.forEach(key => {
1616
if (!metrics[key] || !metrics[key].warn || !metrics[key].error) {
1717
console.error(getMessageWithPrefix('ERROR', 'NO_EXPECTATION_ERROR', key));
18-
process.exit(0);
18+
process.exit(1);
1919
}
2020
});
2121
}
2222

23-
function normalizeMetrics(metrics: ExpectationMetrics) {
23+
function normalizeMetrics(metrics: ExpectationMetrics): NormalizedExpectationMetrics {
2424
let normalizedMetrics: NormalizedExpectationMetrics = {};
2525
Object.keys(metrics).forEach(key => {
2626
normalizedMetrics[key] = {

lib/index.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {launch, LaunchedChrome} from 'chrome-launcher';
55
const lighthouse = require('lighthouse');
66
const perfConfig: any = require('./lh-config');
77
const opn = require('opn');
8+
const path = require('path');
89

910
const Sheets = require('./sheets/index');
1011
const Chart = require('./chart/chart');
@@ -22,7 +23,9 @@ import {
2223
TermWritableStream,
2324
PWMetricsResults,
2425
SheetsConfig,
25-
ExpectationMetrics
26+
ExpectationMetrics,
27+
NormalizedExpectationMetrics,
28+
Timing
2629
} from '../types/types';
2730

2831
const MAX_LIGHTHOUSE_TRIES = 2;
@@ -40,7 +43,7 @@ class PWMetrics {
4043
};
4144
runs: number;
4245
sheets: SheetsConfig;
43-
expectations: ExpectationMetrics;
46+
expectations: ExpectationMetrics | NormalizedExpectationMetrics;
4447
clientSecret: AuthorizeCredentials;
4548
tryLighthouseCounter: number;
4649
launcher: LaunchedChrome;
@@ -53,21 +56,32 @@ class PWMetrics {
5356
this.clientSecret = opts.clientSecret;
5457
this.tryLighthouseCounter = 0;
5558

59+
// normalize path if provided
60+
if (this.flags.chromePath) {
61+
this.flags.chromePath = path.normalize(this.flags.chromePath);
62+
}
63+
5664
if (this.flags.expectations) {
5765
if (this.expectations) {
5866
expectations.validateMetrics(this.expectations);
5967
this.expectations = expectations.normalizeMetrics(this.expectations);
60-
} else throw new Error(getMessageWithPrefix('ERROR', 'NO_EXPECTATIONS_FOUND'));
68+
} else throw new Error(messages.getMessageWithPrefix('ERROR', 'NO_EXPECTATIONS_FOUND'));
6169
}
6270
}
6371

6472
async start() {
6573
const runs = Array.apply(null, {length: +this.runs}).map(Number.call, Number);
6674
let metricsResults: MetricsResults[] = [];
6775

76+
let resultHasExpectationErrors = false;
77+
6878
for (let runIndex of runs) {
6979
try {
70-
metricsResults[runIndex] = await this.run();
80+
const currentMetricResult: MetricsResults = await this.run();
81+
if (!resultHasExpectationErrors && this.flags.expectations) {
82+
resultHasExpectationErrors = this.resultHasExpectationErrors(currentMetricResult);
83+
}
84+
metricsResults[runIndex] = currentMetricResult;
7185
console.log(messages.getMessageWithPrefix('SUCCESS', 'SUCCESS_RUN', runIndex, runs.length));
7286
} catch (error) {
7387
metricsResults[runIndex] = error;
@@ -86,9 +100,25 @@ class PWMetrics {
86100
await sheets.appendResults(results.runs, this.flags.upload);
87101
}
88102
}
103+
104+
if (resultHasExpectationErrors && this.flags.expectations) {
105+
throw new Error(messages.getMessage('HAS_EXPECTATION_ERRORS'));
106+
}
107+
89108
return results;
90109
}
91110

111+
resultHasExpectationErrors(metrics: MetricsResults): boolean {
112+
return metrics.timings.some((timing: Timing) => {
113+
const expectation = this.expectations[timing.id];
114+
if (!expectation) {
115+
return false;
116+
}
117+
const expectedErrorLimit = expectation.error;
118+
return expectedErrorLimit !== undefined && timing.timing >= expectedErrorLimit;
119+
});
120+
}
121+
92122
async run(): Promise<MetricsResults> {
93123
try {
94124
let lhResults: LighthouseResults;
@@ -158,7 +188,8 @@ class PWMetrics {
158188
console.log(messages.getMessage('LAUNCHING_CHROME'));
159189
this.launcher = await launch({
160190
port: this.flags.port,
161-
chromeFlags: this.flags.chromeFlags
191+
chromeFlags: this.flags.chromeFlags,
192+
chromePath: this.flags.chromePath
162193
});
163194
this.flags.port = this.launcher.port;
164195
return this.launcher;

lib/metrics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports = {
3838
const checkAudits = (audits: LighthouseAudits) => Object.keys(audits).forEach(key => {
3939
const debugString = audits[key].debugString;
4040
if (audits[key].debugString)
41-
throw new Error(`${debugString} Audit key: ${key}`);
41+
console.log(`${debugString} Audit key: ${key}`);
4242
});
4343

4444
function prepareData(res: LighthouseResults): MetricsResults {

lib/utils/messages.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ const getMessage = function (messageType: string, ...args: any[]) {
7878
return 'Uploading trace to Google Drive...';
7979
case 'G_DRIVE_UPLOADED':
8080
return 'Trace uploaded to Google Drive...';
81+
case 'HAS_EXPECTATION_ERRORS':
82+
return 'Expectation with errors.';
8183
default:
8284
throw new Error(`No matching message ID: ${messageType}`);
8385
}

0 commit comments

Comments
 (0)