ci(aio): verify that the Lighthouse PWA score remains above a threshold
This commit is contained in:

committed by
Pete Bacon Darwin

parent
a73050de48
commit
49d97e1216
@ -6,14 +6,17 @@ set +x -eu -o pipefail
|
||||
|
||||
INPUT_DIR=dist/
|
||||
OUTPUT_FILE=/tmp/snapshot.tar.gz
|
||||
AIO_BUILDS_HOST=https://ngbuilds.io
|
||||
UPLOAD_URL=$AIO_BUILDS_HOST/create-build/$TRAVIS_PULL_REQUEST/$TRAVIS_PULL_REQUEST_SHA
|
||||
AIO_BUILDS_DOMAIN=ngbuilds.io
|
||||
UPLOAD_URL=https://$AIO_BUILDS_DOMAIN/create-build/$TRAVIS_PULL_REQUEST/$TRAVIS_PULL_REQUEST_SHA
|
||||
DEPLOYED_URL=https://pr$TRAVIS_PULL_REQUEST-$TRAVIS_PULL_REQUEST_SHA.$AIO_BUILDS_DOMAIN
|
||||
|
||||
cd "`dirname $0`/.."
|
||||
|
||||
yarn run build
|
||||
# Build the app
|
||||
yarn build
|
||||
tar --create --gzip --directory "$INPUT_DIR" --file "$OUTPUT_FILE" .
|
||||
|
||||
# Deploy to staging
|
||||
exec 3>&1
|
||||
httpCode=$(
|
||||
curl --include --location --request POST --silent --write-out "\nHTTP_CODE: %{http_code}\n" \
|
||||
@ -30,4 +33,7 @@ if [ $httpCode -lt 200 ] || ([ $httpCode -ge 400 ] && [ $httpCode -ne 409 ]); th
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Run PWA-score tests
|
||||
yarn test-pwa-score -- "$DEPLOYED_URL" "$MIN_PWA_SCORE_PREVIEW"
|
||||
|
||||
cd -
|
||||
|
@ -5,6 +5,7 @@ set +x -eu -o pipefail
|
||||
|
||||
|
||||
FIREBASE_PROJECT_ID=aio-staging
|
||||
DEPLOYED_URL=https://$FIREBASE_PROJECT_ID.firebaseapp.com
|
||||
|
||||
cd "`dirname $0`/.."
|
||||
|
||||
@ -15,4 +16,7 @@ yarn build
|
||||
firebase use "$FIREBASE_PROJECT_ID" --token "$FIREBASE_TOKEN"
|
||||
firebase deploy --message "Commit: $TRAVIS_COMMIT" --non-interactive --token "$FIREBASE_TOKEN"
|
||||
|
||||
# Run PWA-score tests
|
||||
yarn test-pwa-score -- "$DEPLOYED_URL" "$MIN_PWA_SCORE_STAGING"
|
||||
|
||||
cd -
|
||||
|
106
aio/scripts/test-pwa-score.js
Normal file
106
aio/scripts/test-pwa-score.js
Normal file
@ -0,0 +1,106 @@
|
||||
#!/bin/env node
|
||||
|
||||
/**
|
||||
* Usage:
|
||||
* node scripts/test-pwa-score [<url> [<min-score>]]
|
||||
*
|
||||
* Defaults:
|
||||
* url: http://localhost:4200
|
||||
* minScore: 90
|
||||
*
|
||||
* (Ignores HTTPS-related audits, when run for HTTP URL.)
|
||||
*/
|
||||
|
||||
// Imports
|
||||
const lighthouse = require('lighthouse');
|
||||
const ChromeLauncher = require('lighthouse/lighthouse-cli/chrome-launcher').ChromeLauncher;
|
||||
const Printer = require('lighthouse/lighthouse-cli/printer');
|
||||
const config = require('lighthouse/lighthouse-core/config/default.json');
|
||||
|
||||
// Constants
|
||||
const FLAGS = {output: 'json'};
|
||||
|
||||
// Specify the path to Chrome on Travis
|
||||
if (process.env.TRAVIS) {
|
||||
process.env.LIGHTHOUSE_CHROMIUM_PATH = process.env.CHROME_BIN;
|
||||
}
|
||||
|
||||
// Run
|
||||
_main(process.argv.slice(2));
|
||||
|
||||
// Functions - Definitions
|
||||
function _main(args) {
|
||||
const {url, minScore} = parseInput(args);
|
||||
const isOnHttp = /^http:/.test(url);
|
||||
|
||||
console.log(`Running PWA audit for '${url}'...`);
|
||||
|
||||
if (isOnHttp) {
|
||||
ignoreHttpsAudits(config.aggregations);
|
||||
}
|
||||
|
||||
launchChromeAndRunLighthouse(url, FLAGS, config).
|
||||
then(getScore).
|
||||
then(score => evaluateScore(minScore, score)).
|
||||
catch(onError);
|
||||
}
|
||||
|
||||
function evaluateScore(expectedScore, actualScore) {
|
||||
console.log('Lighthouse PWA score:');
|
||||
console.log(` - Expected: ${expectedScore} / 100 (or higher)`);
|
||||
console.log(` - Actual: ${actualScore} / 100`);
|
||||
|
||||
if (actualScore < expectedScore) {
|
||||
throw new Error(`PWA score is too low. (${actualScore} < ${expectedScore})`);
|
||||
}
|
||||
}
|
||||
|
||||
function getScore(results) {
|
||||
const scoredAggregations = results.aggregations.filter(a => a.scored);
|
||||
const total = scoredAggregations.reduce((sum, a) => sum + a.total, 0);
|
||||
|
||||
return Math.round((total / scoredAggregations.length) * 100);
|
||||
}
|
||||
|
||||
function ignoreHttpsAudits(aggregations) {
|
||||
const httpsAudits = [
|
||||
'is-on-https',
|
||||
'redirects-http'
|
||||
];
|
||||
|
||||
console.info(`Ignoring HTTPS-related audits (${httpsAudits.join(', ')})...`);
|
||||
|
||||
aggregations.forEach(aggregation =>
|
||||
aggregation.items.forEach(item =>
|
||||
httpsAudits.map(key => item.audits[key]).forEach(audit =>
|
||||
// Ugly hack to ignore HTTPS-related audits (i.e. simulate them passing).
|
||||
// Only meant for use during development.
|
||||
audit && (audit.expectedValue = !audit.expectedValue))));
|
||||
}
|
||||
|
||||
function launchChromeAndRunLighthouse(url, flags, config) {
|
||||
const launcher = new ChromeLauncher({autoSelectChrome: true});
|
||||
|
||||
return launcher.run().
|
||||
then(() => lighthouse(url, flags, config)).
|
||||
then(results => launcher.kill().then(() => results)).
|
||||
catch(err => launcher.kill().then(() => { throw err; }, () => { throw err; }));
|
||||
}
|
||||
|
||||
function onError(err) {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function parseInput(args) {
|
||||
const url = args[0];
|
||||
const minScore = Number(args[1]);
|
||||
|
||||
if (!url) {
|
||||
onError('Invalid arguments: <URL> not specified.');
|
||||
} else if (isNaN(minScore)) {
|
||||
onError('Invalid arguments: <MIN_SCORE> not specified or not a number.');
|
||||
}
|
||||
|
||||
return {url, minScore};
|
||||
}
|
Reference in New Issue
Block a user