Skip to content

Commit

Permalink
Merge f6cb5ea into cd2a76f
Browse files Browse the repository at this point in the history
  • Loading branch information
dependabot[bot] committed Feb 12, 2023
2 parents cd2a76f + f6cb5ea commit 7879bc2
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 38 deletions.
2 changes: 1 addition & 1 deletion packages/lighthouse/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"dependencies": {
"@browserless/errors": "^9.8.4",
"debug-logfmt": "~1.0.4",
"lighthouse": "~9.6.8",
"lighthouse": "~10.0.0",
"p-retry": "~4.6.1",
"p-timeout": "~4.1.0",
"serialize-error": "~8.1.0"
Expand Down
14 changes: 14 additions & 0 deletions packages/lighthouse/src/get-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

const preset = name =>
name
? import(`lighthouse/core/config/${name}-config.js`)
.then(mod => mod.default)
.catch(() => undefined)
: undefined

module.exports = async ({ preset: presetName, ...settings } = {}) => {
const config = (await preset(presetName)) || { extends: 'lighthouse:default' }
if (Object.keys(settings).length > 0) config.settings = { ...config.settings, ...settings }
return config
}
18 changes: 6 additions & 12 deletions packages/lighthouse/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@ const pTimeout = require('p-timeout')
const pRetry = require('p-retry')

const lighthouse = require('./lighthouse')
const getConfig = require('./get-config')

const { AbortError } = pRetry

const getConfig = ({ preset: presetName, ...settings } = {}) => {
const baseConfig = presetName
? require(`lighthouse/lighthouse-core/config/${presetName}-config.js`)
: { extends: 'lighthouse:default' }

return { ...baseConfig, settings: { ...baseConfig.settings, ...settings } }
}

// See https://github.com/GoogleChrome/lighthouse/blob/master/docs/readme.md#configuration
const getFlags = (
browser,
Expand All @@ -41,14 +34,17 @@ module.exports = async (
} = {}
) => {
const browserlessPromise = getBrowserless()
const config = getConfig(opts)
let isRejected = false

async function run () {
const browserless = await browserlessPromise
const browser = await browserless.browser()
const flags = await getFlags(browser, { disableStorageReset, logLevel, output })
const { value, reason, isFulfilled } = await lighthouse({ url, flags, config })
const { value, reason, isFulfilled } = await lighthouse({
config: await getConfig(opts),
flags,
url
})
if (isFulfilled) return value
throw ensureError(reason)
}
Expand All @@ -72,5 +68,3 @@ module.exports = async (

return result
}

module.exports.getConfig = getConfig
2 changes: 1 addition & 1 deletion packages/lighthouse/src/lighthouse.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const lighthouse = require('lighthouse/core/index.cjs')
const { serializeError } = require('serialize-error')
const lighthouse = require('lighthouse')

module.exports = async ({ url, flags, config }) => {
try {
Expand Down
51 changes: 28 additions & 23 deletions packages/lighthouse/test/get-config.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
'use strict'

const { generateConfig } = require('lighthouse')
const { readdirSync } = require('fs')
const path = require('path')
const test = require('ava')

const { getConfig } = require('..')
const getConfig = require('../src/get-config')

const defaultConfig = generateConfig()
const configDir = path.resolve(__dirname, '../node_modules/lighthouse/lighthouse-core/config')
const configDir = path.resolve(__dirname, '../node_modules/lighthouse/core/config')
const files = readdirSync(configDir)

const excludesFiles = [
'budget.js',
'config-helpers.js',
'config-plugin.js',
'config.js',
'constants.js',
'metrics-to-audits.js'
]

const configs = files
.filter(n => !excludesFiles.includes(n))
.filter(n => n.endsWith('-config.js'))
.map(n => n.replace('.js', '').replace('-config', ''))

test('load default by default', t => {
const config = generateConfig(getConfig())
t.deepEqual(JSON.stringify(config), JSON.stringify(defaultConfig))
test('load default by default', async t => {
const config = await getConfig()
t.snapshot(config)
})

configs.forEach(preset => {
test(`preset \`${preset}\``, t => {
const config = generateConfig(getConfig({ preset }))
const configPath = `lighthouse/lighthouse-core/config/${preset}-config.js`
const baseConfig = generateConfig(require(configPath))
test('invalid preset', async t => {
const config = await getConfig({ preset: 'notexist' })
t.snapshot(config)
})

t.deepEqual(JSON.stringify(config), JSON.stringify(baseConfig))
test('invalid preset with settings', async t => {
const config = await getConfig({
preset: 'notexist',
skipAudits: ['uses-http2', 'bf-cache']
})
t.snapshot(config)
})

configs.forEach(preset => {
test(`preset \`${preset}\``, async t => {
const configPath = path.resolve(configDir, `${preset}-config.js`)
const config = await import(configPath).then(mod => mod.default)
t.deepEqual(await getConfig({ preset }), config)
})
})

test('preset with settings', async t => {
const config = await getConfig({ preset: 'lr-desktop', skipAudits: ['uses-http2'] })
t.deepEqual(config.settings.skipAudits, ['uses-http2'])
})
11 changes: 10 additions & 1 deletion packages/lighthouse/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ test('default configuration', async t => {
const url = 'https://example.com'
const report = await lighthouse(url)
t.true(report.audits['screenshot-thumbnails'].details.items.length > 0)
t.snapshot(report.configSettings)
})

test('passing custom configuration', async t => {
test('customize default configuration', async t => {
const url = 'https://kikobeats.com'
const report = await lighthouse(url, { onlyAudits: ['accessibility'] })
t.deepEqual(report.configSettings.onlyAudits, ['accessibility'])
t.snapshot(report.configSettings)
})

test('specifying custom different configuration', async t => {
const url = 'https://kikobeats.com'
const report = await lighthouse(url, { preset: 'lr-desktop' })
t.snapshot(report.configSettings)
})

test('passing a different serializer', async t => {
Expand All @@ -23,6 +31,7 @@ test('passing a different serializer', async t => {
output: 'html'
})
t.true(report.startsWith('<!--'))
t.snapshot(report.configSettings)
})

test('handle timeout', async t => {
Expand Down
35 changes: 35 additions & 0 deletions packages/lighthouse/test/snapshots/get-config.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Snapshot report for `test/get-config.js`

The actual snapshot is saved in `get-config.js.snap`.

Generated by [AVA](https://avajs.dev).

## load default by default

> Snapshot 1
{
extends: 'lighthouse:default',
}

## invalid preset

> Snapshot 1
{
extends: 'lighthouse:default',
}

## invalid preset with settings

> Snapshot 1
{
extends: 'lighthouse:default',
settings: {
skipAudits: [
'uses-http2',
'bf-cache',
],
},
}
Binary file not shown.
166 changes: 166 additions & 0 deletions packages/lighthouse/test/snapshots/index.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# Snapshot report for `test/index.js`

The actual snapshot is saved in `index.js.snap`.

Generated by [AVA](https://avajs.dev).

## specifying custom different configuration

> Snapshot 1
{
additionalTraceCategories: null,
auditMode: false,
blankPage: 'about:blank',
blockedUrlPatterns: null,
budgets: null,
channel: 'node',
cpuQuietThresholdMs: 1000,
debugNavigation: false,
disableFullPageScreenshot: false,
disableStorageReset: true,
emulatedUserAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
extraHeaders: null,
formFactor: 'desktop',
gatherMode: false,
locale: 'en-US',
maxWaitForFcp: 15000,
maxWaitForLoad: 35000,
networkQuietThresholdMs: 1000,
onlyAudits: null,
onlyCategories: null,
output: 'json',
pauseAfterFcpMs: 1000,
pauseAfterLoadMs: 1000,
precomputedLanternData: null,
screenEmulation: {
deviceScaleFactor: 1,
disabled: false,
height: 940,
mobile: false,
width: 1350,
},
skipAboutBlank: false,
skipAudits: [
'uses-http2',
'bf-cache',
],
throttling: {
cpuSlowdownMultiplier: 1,
downloadThroughputKbps: 0,
requestLatencyMs: 0,
rttMs: 40,
throughputKbps: 10240,
uploadThroughputKbps: 0,
},
throttlingMethod: 'simulate',
usePassiveGathering: false,
}

## default configuration

> Snapshot 1
{
additionalTraceCategories: null,
auditMode: false,
blankPage: 'about:blank',
blockedUrlPatterns: null,
budgets: null,
channel: 'node',
cpuQuietThresholdMs: 1000,
debugNavigation: false,
disableFullPageScreenshot: false,
disableStorageReset: true,
emulatedUserAgent: 'Mozilla/5.0 (Linux; Android 11; moto g power (2022)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Mobile Safari/537.36',
extraHeaders: null,
formFactor: 'mobile',
gatherMode: false,
locale: 'en-US',
maxWaitForFcp: 30000,
maxWaitForLoad: 45000,
networkQuietThresholdMs: 1000,
onlyAudits: null,
onlyCategories: null,
output: 'json',
pauseAfterFcpMs: 1000,
pauseAfterLoadMs: 1000,
precomputedLanternData: null,
screenEmulation: {
deviceScaleFactor: 1.75,
disabled: false,
height: 823,
mobile: true,
width: 412,
},
skipAboutBlank: false,
skipAudits: null,
throttling: {
cpuSlowdownMultiplier: 4,
downloadThroughputKbps: 1474.5600000000002,
requestLatencyMs: 562.5,
rttMs: 150,
throughputKbps: 1638.4,
uploadThroughputKbps: 675,
},
throttlingMethod: 'simulate',
usePassiveGathering: false,
}

## customize default configuration

> Snapshot 1
{
additionalTraceCategories: null,
auditMode: false,
blankPage: 'about:blank',
blockedUrlPatterns: null,
budgets: null,
channel: 'node',
cpuQuietThresholdMs: 1000,
debugNavigation: false,
disableFullPageScreenshot: false,
disableStorageReset: true,
emulatedUserAgent: 'Mozilla/5.0 (Linux; Android 11; moto g power (2022)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Mobile Safari/537.36',
extraHeaders: null,
formFactor: 'mobile',
gatherMode: false,
locale: 'en-US',
maxWaitForFcp: 30000,
maxWaitForLoad: 45000,
networkQuietThresholdMs: 1000,
onlyAudits: [
'accessibility',
],
onlyCategories: null,
output: 'json',
pauseAfterFcpMs: 1000,
pauseAfterLoadMs: 1000,
precomputedLanternData: null,
screenEmulation: {
deviceScaleFactor: 1.75,
disabled: false,
height: 823,
mobile: true,
width: 412,
},
skipAboutBlank: false,
skipAudits: null,
throttling: {
cpuSlowdownMultiplier: 4,
downloadThroughputKbps: 1474.5600000000002,
requestLatencyMs: 562.5,
rttMs: 150,
throughputKbps: 1638.4,
uploadThroughputKbps: 675,
},
throttlingMethod: 'simulate',
usePassiveGathering: false,
}

## passing a different serializer

> Snapshot 1
undefined
Binary file added packages/lighthouse/test/snapshots/index.js.snap
Binary file not shown.

0 comments on commit 7879bc2

Please sign in to comment.