-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathlighthouseBudget.js
113 lines (92 loc) · 2.31 KB
/
lighthouseBudget.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* eslint-disable no-console */
const fs = require('fs');
const getCategoryScores = data => {
const { categories } = data;
return {
ally: categories.accessibility.score * 100,
bestPractises: categories['best-practices'].score * 100,
seo: categories.seo.score * 100,
};
};
const isAboveThreshold = (scoreValue, budgetValue) => {
if (scoreValue >= budgetValue) {
return true;
}
return false;
};
const logRow = (category, scoreValue, budgetValue, isPassing) => ({
category,
scoreValue,
budgetValue,
status: isPassing ? 'Pass' : 'Fail',
});
const compareToBudget = (categories, scoreResult, scoreBudget) => {
let result = true;
const logArray = [];
// eslint-disable-next-line consistent-return
categories.forEach(prop => {
const isPassing = isAboveThreshold(scoreResult[prop], scoreBudget[prop]);
const passLog = logRow(
prop,
scoreResult[prop],
scoreBudget[prop],
isPassing,
);
logArray.push(passLog);
if (!isPassing) result = false;
});
console.table(logArray);
return result;
};
const readReport = path => {
console.log('Reading the report');
const rawdata = fs.readFileSync(path);
const result = JSON.parse(rawdata);
if (result) {
console.log(result.finalUrl);
}
return result;
};
const exitResult = isPassing => {
process.on('exit', code => console.log(`Exiting with code ${code}`));
if (!isPassing) {
console.log('Lighthouse tests failed. See log table for details');
process.exit(1);
}
console.log('All tests passed!');
process.exit(0);
};
// START OF SCRIPT
const budget = {
ally: 90,
bestPractises: 90,
seo: 100,
};
const testableProperties = ['ally', 'bestPractises', 'seo'];
const run = () => {
const report = readReport('simorgh.report.json');
const extractedCategories = getCategoryScores(report);
const result = compareToBudget(
testableProperties,
extractedCategories,
budget,
);
exitResult(result);
};
module.exports = {
getCategoryScores,
isAboveThreshold,
logRow,
compareToBudget,
readReport,
run,
exitResult,
};
// Script run
// A 'run' argument need to be passed in for the script to work
// This was done to make the script unit testable without spliting
// it to different files.
const args = process.argv.slice(2);
if (args[0] === 'run') {
run();
}