Skip to content

Commit

Permalink
feat(calculate-results): Add better failure messages (#80)
Browse files Browse the repository at this point in the history
Provide a meaningful reason for the failure
  • Loading branch information
andreasonny83 authored Oct 3, 2020
1 parent 6fb9d9f commit 3ffcfdb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 25 deletions.
44 changes: 19 additions & 25 deletions bin/cli
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ const chalk = require('chalk');
const updateNotifier = require('update-notifier');
const pkg = require('../package.json');

const { scoreReducer } = require('../lib/helpers');
const { getScores, getChromeFlags } = require('../lib/config');
const { getChromeFlags } = require('../lib/config');
const lighthouseReporter = require('../lib/lighthouse-reporter');
const analyzeScore = require('../lib/score-analyzer');
const analyzeBudgets = require('../lib/budgets-analyzer');
const { calculateResults } = require('../lib/calculate-results');

const spinner = ora({
color: 'yellow',
Expand Down Expand Up @@ -192,7 +190,7 @@ Promise.resolve()

if (!silent) {
for (const category in categoryReport) {
if (!categoryReport[category]) {
if (typeof categoryReport[category] === 'undefined') {
continue;
}

Expand All @@ -213,30 +211,26 @@ Promise.resolve()
return { categoryReport, budgetsReport };
})
.then(({ categoryReport, budgetsReport }) => {
let thresholds = scoreReducer(flags, getScores());

thresholds =
Object.keys(thresholds).length === 0
? {
score: 100,
}
: thresholds;

if (thresholds && Object.keys(thresholds).length !== 0) {
const isScorePassing = analyzeScore(categoryReport, thresholds);
const areBudgetsPassing = analyzeBudgets(budgetsReport, failOnBudgets);

if (isScorePassing && areBudgetsPassing) {
console.log(chalk.green('All checks are passing. 🎉\n'));
return process.exit(0);
}
const result = calculateResults(flags, categoryReport, budgetsReport, failOnBudgets);

if (result.passed) {
console.log(chalk.green('\nAll checks are passing. 🎉\n'));
return process.exit(0);
}

console.log(chalk.red('\nFailed. ❌'));

if (result.score === false) {
throw new Error('Target score not reached.');
}
if (result.budget === false) {
throw new Error('Target budget not reached.');
}

console.log(chalk.red('Failed. ❌\n'));
throw new Error('lighthouse-ci test failed.');
})
.catch(err => {
.catch((err) => {
spinner.stop();
console.log(chalk.red(err));
console.log(chalk.red(err), '\n');
return process.exit(1);
});
46 changes: 46 additions & 0 deletions lib/calculate-results.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright (c) 2018-2020 AndreaSonny <[email protected]> (https://github.com/andreasonny83)
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/

const { scoreReducer } = require('./helpers');
const analyzeScore = require('./score-analyzer');
const analyzeBudgets = require('./budgets-analyzer');
const { getScores } = require('./config');

const calculateResults = (flags, categoryReport, budgetsReport, failOnBudgets) => {
let thresholds = scoreReducer(flags, getScores());
thresholds =
Object.keys(thresholds).length === 0
? {
score: 100,
}
: thresholds;

if (thresholds && Object.keys(thresholds).length !== 0) {
const isScorePassing = analyzeScore(categoryReport, thresholds);
const areBudgetsPassing = analyzeBudgets(budgetsReport, failOnBudgets);

if (isScorePassing && areBudgetsPassing) {
return {
passed: true,
};
}

return {
passed: false,
score: isScorePassing,
budget: areBudgetsPassing,
};
}

return {
passed: false,
};
};

module.exports = {
calculateResults,
};

0 comments on commit 3ffcfdb

Please sign in to comment.