Repository navigation
build(docs-infra): use latest Chrome (75) to reliably pass PWA score tests on localhost #29921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ | |
|
|
||
| /** | ||
| * Usage: | ||
| * node scripts/test-pwa-score <url> <min-score> [<log-file>] | ||
| * ```sh | ||
| * node scripts/test-pwa-score <url> <min-score> [<log-file>] | ||
| * ``` | ||
| * | ||
| * Fails if the score is below `<min-score>`. | ||
| * If `<log-file>` is defined, the full results will be logged there. | ||
|
|
@@ -11,11 +13,11 @@ | |
| */ | ||
|
|
||
| // Imports | ||
| 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'); | ||
| const puppeteer = require('puppeteer'); | ||
|
|
||
| // Constants | ||
| const CHROME_LAUNCH_OPTS = {}; | ||
|
|
@@ -32,7 +34,7 @@ if (process.env.CI) { | |
| _main(process.argv.slice(2)); | ||
|
|
||
| // Functions - Definitions | ||
| function _main(args) { | ||
| async function _main(args) { | ||
| const {url, minScore, logFile} = parseInput(args); | ||
| const isOnHttp = /^http:/.test(url); | ||
|
|
||
|
|
@@ -44,10 +46,13 @@ function _main(args) { | |
|
|
||
| logger.setLevel(LIGHTHOUSE_FLAGS.logLevel); | ||
|
|
||
| launchChromeAndRunLighthouse(url, LIGHTHOUSE_FLAGS, config). | ||
| then(results => processResults(results, logFile)). | ||
| then(score => evaluateScore(minScore, score)). | ||
| catch(onError); | ||
| try { | ||
| const results = await launchChromeAndRunLighthouse(url, LIGHTHOUSE_FLAGS, config); | ||
| const score = await processResults(results, logFile); | ||
| evaluateScore(minScore, score); | ||
| } catch (err) { | ||
| onError(err); | ||
| } | ||
| } | ||
|
|
||
| function evaluateScore(expectedScore, actualScore) { | ||
|
|
@@ -60,13 +65,16 @@ function evaluateScore(expectedScore, actualScore) { | |
| } | ||
| } | ||
|
|
||
| function launchChromeAndRunLighthouse(url, flags, config) { | ||
| return chromeLauncher.launch(CHROME_LAUNCH_OPTS).then(chrome => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch 👍 |
||
| flags.port = chrome.port; | ||
| return lighthouse(url, flags, config). | ||
| then(results => chrome.kill().then(() => results)). | ||
| catch(err => chrome.kill().then(() => { throw err; }, () => { throw err; })); | ||
| }); | ||
| async function launchChromeAndRunLighthouse(url, flags, config) { | ||
| const browser = await puppeteer.launch({defaultViewport: null, headless: false}); | ||
| const browserVersion = await browser.version(); | ||
| flags.port = new URL(browser.wsEndpoint()).port; | ||
|
|
||
| try { | ||
| return {browserVersion, ...await lighthouse(url, flags, config)}; | ||
| } finally { | ||
| await browser.close(); | ||
| } | ||
| } | ||
|
|
||
| function onError(err) { | ||
|
|
@@ -88,33 +96,33 @@ function parseInput(args) { | |
| return {url, minScore, logFile}; | ||
| } | ||
|
|
||
| function processResults(results, logFile) { | ||
| async function processResults(results, logFile) { | ||
| const browserVersion = results.browserVersion; | ||
| const lhVersion = results.lhr.lighthouseVersion; | ||
| const categories = results.lhr.categories; | ||
| const report = results.report; | ||
|
|
||
| return Promise.resolve(). | ||
| then(() => { | ||
| if (logFile) { | ||
| console.log(`Saving results in '${logFile}'...`); | ||
| console.log(`(LightHouse viewer: ${VIEWER_URL})`); | ||
|
|
||
| return printer.write(report, printer.OutputMode.json, logFile); | ||
| } | ||
| }). | ||
| then(() => { | ||
| const categoryData = Object.keys(categories).map(name => categories[name]); | ||
| const maxTitleLen = Math.max(...categoryData.map(({title}) => title.length)); | ||
|
|
||
| console.log('\nLighthouse version:', results.lhr.lighthouseVersion); | ||
|
|
||
| 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); | ||
| if (logFile) { | ||
| console.log(`\nSaving results in '${logFile}'...`); | ||
| console.log(`(LightHouse viewer: ${VIEWER_URL})`); | ||
|
|
||
| await printer.write(report, printer.OutputMode.json, logFile); | ||
| } | ||
|
|
||
| const categoryData = Object.keys(categories).map(name => categories[name]); | ||
| const maxTitleLen = Math.max(...categoryData.map(({title}) => title.length)); | ||
|
|
||
| console.log(`\nBrowser version: ${browserVersion}`); | ||
| console.log(`Lighthouse version: ${lhVersion}`); | ||
|
|
||
| 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`); | ||
| }); | ||
|
|
||
| return categories.pwa.score * 100; | ||
| } | ||
|
|
||
| function skipHttpsAudits(config) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is still up for discuss, but just so that we don't forget then: Looks like the
chrome-launcherdev dependency is no longer used anywhere then. So we can remove that right?