98 lines
2.7 KiB
JavaScript
98 lines
2.7 KiB
JavaScript
const lighthouse = require('lighthouse');
|
|
const puppeteer = require('puppeteer');
|
|
const { URL } = require('url');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// Configuration for Lighthouse
|
|
const LIGHTHOUSE_CONFIG = {
|
|
extends: 'lighthouse:default',
|
|
settings: {
|
|
onlyCategories: ['performance', 'accessibility', 'best-practices', 'seo'],
|
|
formFactor: 'desktop',
|
|
throttlingMethod: 'simulate',
|
|
},
|
|
};
|
|
|
|
// Set threshold values for each category
|
|
const THRESHOLDS = {
|
|
performance: 90,
|
|
accessibility: 90,
|
|
'best-practices': 90,
|
|
seo: 90,
|
|
};
|
|
|
|
async function runLighthouse(url) {
|
|
// Launch a headless browser
|
|
const browser = await puppeteer.launch({ headless: true });
|
|
|
|
// Create a new Lighthouse runner
|
|
const { lhr } = await lighthouse(url, {
|
|
port: (new URL(browser.wsEndpoint())).port,
|
|
output: 'json',
|
|
logLevel: 'info',
|
|
}, LIGHTHOUSE_CONFIG);
|
|
|
|
await browser.close();
|
|
|
|
// Create directory for reports if it doesn't exist
|
|
const reportsDir = path.join(__dirname, 'reports');
|
|
if (!fs.existsSync(reportsDir)) {
|
|
fs.mkdirSync(reportsDir);
|
|
}
|
|
|
|
// Save the report
|
|
const reportPath = path.join(reportsDir, `lighthouse-report-${Date.now()}.json`);
|
|
fs.writeFileSync(reportPath, JSON.stringify(lhr, null, 2));
|
|
|
|
console.log('\nLighthouse Results:');
|
|
console.log('--------------------');
|
|
|
|
// Process and display results
|
|
let allPassed = true;
|
|
Object.keys(THRESHOLDS).forEach(category => {
|
|
const score = Math.round(lhr.categories[category].score * 100);
|
|
const threshold = THRESHOLDS[category];
|
|
const passed = score >= threshold;
|
|
|
|
if (!passed) {
|
|
allPassed = false;
|
|
}
|
|
|
|
console.log(`${category}: ${score}/100 - ${passed ? 'PASS' : 'FAIL'} (Threshold: ${threshold})`);
|
|
});
|
|
|
|
// Display audits that failed
|
|
console.log('\nFailed Audits:');
|
|
console.log('--------------');
|
|
let hasFailedAudits = false;
|
|
|
|
Object.values(lhr.audits).forEach(audit => {
|
|
if (audit.score !== null && audit.score < 0.9) {
|
|
hasFailedAudits = true;
|
|
console.log(`- ${audit.title}: ${Math.round(audit.score * 100)}/100`);
|
|
console.log(` ${audit.description}`);
|
|
}
|
|
});
|
|
|
|
if (!hasFailedAudits) {
|
|
console.log('No significant audit failures!');
|
|
}
|
|
|
|
console.log(`\nDetailed report saved to: ${reportPath}`);
|
|
|
|
return allPassed;
|
|
}
|
|
|
|
// URL to test - this should be your local server address
|
|
const url = process.argv[2] || 'http://localhost:8080';
|
|
|
|
runLighthouse(url)
|
|
.then(passed => {
|
|
console.log(`\nOverall: ${passed ? 'PASSED' : 'FAILED'}`);
|
|
process.exit(passed ? 0 : 1);
|
|
})
|
|
.catch(error => {
|
|
console.error('Error running Lighthouse:', error);
|
|
process.exit(1);
|
|
});
|