@@ -5,6 +5,7 @@ import {launch, LaunchedChrome} from 'chrome-launcher';
55const lighthouse = require ( 'lighthouse' ) ;
66const perfConfig : any = require ( './lh-config' ) ;
77const opn = require ( 'opn' ) ;
8+ const path = require ( 'path' ) ;
89
910const Sheets = require ( './sheets/index' ) ;
1011const 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
2831const 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 ;
0 commit comments