Created
October 30, 2018 16:59
-
-
Save radiocontrolled/6c76ae315ea3f4912fe6e878552b8ab8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const lighthouse = require('lighthouse'); | |
const chromeLauncher = require('chrome-launcher'); | |
// run this with | |
// node lighthouseTest.js --accessibility=0.79 --seo=2 | |
// it will fail on SEO | |
const args = process.argv.slice(2); | |
const requiredThresholds = [ | |
{ | |
title: 'Accessibility', | |
score: args[0].split(/\=/)[1], | |
}, | |
{ | |
title: 'SEO', | |
score: args[1].split(/\=/)[1], | |
}, | |
]; | |
function launchChromeAndRunLighthouse(url, opts, config = null) { | |
return chromeLauncher | |
.launch({ chromeFlags: opts.chromeFlags }) | |
.then(chrome => { | |
opts.port = chrome.port; | |
return lighthouse(url, opts, config).then(results => { | |
// use results.lhr for the JS-consumeable output | |
// https://github.com/GoogleChrome/lighthouse/blob/master/typings/lhr.d.ts | |
// use results.report for the HTML/JSON/CSV output as a string | |
// use results.artifacts for the trace/screenshots/other specific case you need (rarer) | |
return chrome.kill().then(() => results.lhr); | |
}); | |
}); | |
} | |
const opts = { | |
chromeFlags: ['--show-paint-rects'], | |
}; | |
function getScoresPerCategory(reportCategories, url) { | |
const categoriesArray = Object.keys(reportCategories).map( | |
i => reportCategories[i], | |
); | |
const results = categoriesArray.map(({ title, score }) => ({ title, score })); | |
return { results, url }; | |
} | |
function threshold(resultsArray, thresholds) { | |
resultsArray.forEach(result => { | |
thresholds.forEach(thres => { | |
if (result.title === thres.title) { | |
if (result.score < thres.score) { | |
console.log( | |
`${result.title} fails with ${ | |
result.score | |
}, reuquired threshold was ${thres.score}`, | |
); | |
process.exit(1); | |
} | |
} | |
}); | |
}); | |
} | |
const url = 'https://www.bbc.co.uk/news'; | |
launchChromeAndRunLighthouse(url, opts).then(results => { | |
// console.log('results.categories', results.categories); | |
const scores = getScoresPerCategory(results.categories, url); | |
console.log(threshold(scores.results, requiredThresholds)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment