1
0
mirror of https://github.com/kremalicious/portfolio.git synced 2025-01-03 10:25:00 +01:00
portfolio/scripts/lighthouse.test.js

57 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-12-15 17:51:46 +01:00
import '@babel/polyfill'
2018-09-24 00:13:41 +02:00
import * as chromeLauncher from 'chrome-launcher'
2018-12-15 17:51:46 +01:00
import test from 'ava'
2018-09-24 00:13:41 +02:00
import lighthouse from 'lighthouse'
import { siteMetadata } from '../gatsby-config'
2018-09-14 14:30:48 +02:00
const launchChromeAndRunLighthouse = (
url,
opts = { chromeFlags: ['--headless'] },
config = null
) =>
2018-12-07 12:10:50 +01:00
chromeLauncher
.launch({ chromeFlags: opts.chromeFlags })
.then(async chrome => {
opts.port = chrome.port
const results = await lighthouse(url, opts, config)
await chrome.kill()
return results.lhr
})
2018-09-14 14:30:48 +02:00
let scores
2018-12-07 12:10:50 +01:00
2018-09-14 14:30:48 +02:00
test.before(async () => {
2018-09-24 00:13:41 +02:00
console.log(`Auditing ${siteMetadata.siteUrl}.\n`) // eslint-disable-line no-console
scores = await launchChromeAndRunLighthouse(siteMetadata.siteUrl).then(
2018-09-14 14:30:48 +02:00
({ categories }) => categories
)
})
const logScore = score => `Is ${score * 100}.`
const testOutput = (t, metric) => {
2018-09-24 00:13:41 +02:00
const { score } = scores[metric]
2018-09-14 14:30:48 +02:00
t.log(logScore(score))
return score >= 0.9 ? t.pass() : t.fail()
}
test('Performance Score above 90', t => {
testOutput(t, 'performance')
})
test('PWA Score above 90', t => {
testOutput(t, 'pwa')
})
test('Accessibility Score above 90', t => {
testOutput(t, 'accessibility')
})
test('Best Practices Score above 90', t => {
testOutput(t, 'best-practices')
})
test('SEO Score above 90', t => {
testOutput(t, 'seo')
})