test(docs-infra): improve logging output in test-pwa-score[-localhost] (#26459)

PR Close #26459
This commit is contained in:
George Kalpakas
2018-10-10 18:52:10 +03:00
committed by Miško Hevery
parent 1aa8cfbf74
commit 327c614799
2 changed files with 23 additions and 7 deletions

View File

@ -15,9 +15,11 @@ const chromeLauncher = require('chrome-launcher');
const lighthouse = require('lighthouse');
const printer = require('lighthouse/lighthouse-cli/printer');
const config = require('lighthouse/lighthouse-core/config/default-config.js');
const logger = require('lighthouse-logger');
// Constants
const CHROME_LAUNCH_OPTS = {};
const LIGHTHOUSE_FLAGS = {logLevel: 'info'};
const SKIPPED_HTTPS_AUDITS = ['redirects-http'];
const VIEWER_URL = 'https://googlechrome.github.io/lighthouse/viewer/';
@ -26,6 +28,7 @@ const VIEWER_URL = 'https://googlechrome.github.io/lighthouse/viewer/';
if (process.env.TRAVIS) {
process.env.LIGHTHOUSE_CHROMIUM_PATH = process.env.CHROME_BIN;
CHROME_LAUNCH_OPTS.chromeFlags = ['--no-sandbox'];
LIGHTHOUSE_FLAGS.logLevel = 'error';
}
// Run
@ -42,18 +45,20 @@ function _main(args) {
skipHttpsAudits(config);
}
launchChromeAndRunLighthouse(url, {}, config).
logger.setLevel(LIGHTHOUSE_FLAGS.logLevel);
launchChromeAndRunLighthouse(url, LIGHTHOUSE_FLAGS, config).
then(results => processResults(results, logFile)).
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`);
console.log('\nLighthouse PWA score:');
console.log(` - Expected: ${expectedScore.toFixed(0).padStart(3)} / 100 (or higher)`);
console.log(` - Actual: ${actualScore.toFixed(0).padStart(3)} / 100\n`);
if (actualScore < expectedScore) {
if (isNaN(actualScore) || (actualScore < expectedScore)) {
throw new Error(`PWA score is too low. (${actualScore} < ${expectedScore})`);
}
}
@ -99,7 +104,18 @@ function processResults(results, logFile) {
return printer.write(report, printer.OutputMode.json, logFile);
}
}).
then(() => Math.round(categories.pwa.score * 100));
then(() => {
const categoryData = Object.keys(categories).map(name => categories[name]);
const maxTitleLen = Math.max(...categoryData.map(({title}) => title.length));
console.log('\nAudit scores:');
categoryData.forEach(({title, score}) => {
const paddedTitle = `${title}:`.padEnd(maxTitleLen + 1);
const paddedScore = (score * 100).toFixed(0).padStart(3);
console.log(` - ${paddedTitle} ${paddedScore} / 100`);
});
}).
then(() => categories.pwa.score * 100);
}
function skipHttpsAudits(config) {