Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NaN% in CLI report #763

Merged
merged 2 commits into from
Oct 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lighthouse-cli/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function formatScore(score, suffix) {
if (typeof score !== 'number') {
return `${purple}${score}${reset}`;
}

let colorChoice = red;
if (score > 45) {
colorChoice = yellow;
Expand Down Expand Up @@ -123,7 +124,7 @@ function createOutput(results, outputMode) {
let score = (item.overall * 100).toFixed(0);

if (item.name) {
output += `${bold}${item.name}${reset}: ${formatScore(Number(score), '%')}\n`;
output += `${bold}${item.name}${reset}: ${item.scored ? formatScore(score, '%') : ''}\n`;
}

item.subItems.forEach(subitem => {
Expand Down
5 changes: 5 additions & 0 deletions lighthouse-cli/test/cli/printer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ describe('Printer', () => {
// Just check there's no HTML / JSON there.
assert.throws(_ => JSON.parse(prettyOutput));
assert.equal(/<!doctype/gim.test(prettyOutput), false);

const hasScoreOnNonScoredItem = /Using modern offline features.*?(0%|NaN)/.test(prettyOutput);
const hasAggregationPresent = prettyOutput.includes('Using modern offline features');
assert.equal(hasScoreOnNonScoredItem, false, 'non-scored item was scored');
assert.equal(hasAggregationPresent, true, 'non-scored aggregation item is not there');
});

it('creates HTML for results', () => {
Expand Down
34 changes: 34 additions & 0 deletions lighthouse-cli/test/fixtures/sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@
"category": "JavaScript",
"description": "Page contains some content when its scripts are not available"
},
"appcache-manifest": {
"score": true,
"displayValue": "",
"rawValue": true,
"name": "appcache-manifest",
"category": "Offline",
"description": "Site is not using Application Cache",
"helpText": "Application Cache has been <a href=\"https://html.spec.whatwg.org/multipage/browsers.html#offline\" target=\"_blank\">deprecated</a> by <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers\" target=\"_blank\">Service Workers</a>. Consider implementing an offline solution using the <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/Cache\" target=\"_blank\">Cache Storage API</a>."
},
"no-websql": {
"score": true,
"displayValue": "",
"rawValue": true,
"name": "no-websql",
"category": "Offline",
"description": "Site is not using WebSQL DB.",
"helpText": "Web SQL Database is <a href=\"https://dev.w3.org/html5/webdatabase/\" target=\"_blank\">deprecated</a>. Consider implementing an offline solution using <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB\" target=\"_blank\">IndexedDB</a>."
},
"first-meaningful-paint": {
"score": 100,
"displayValue": "615.8ms",
Expand Down Expand Up @@ -610,6 +628,22 @@
]
}
]
},
{
"name": "Do Better Web",
"description": "We've compiled some recommendations for modernizing your web app and avoiding performance pitfalls.",
"scored": false,
"categorizable": true,
"score": [
{
"overall": null,
"name": "Using modern offline features",
"subItems": [
"appcache-manifest",
"no-websql"
]
}
]
}
]
}
15 changes: 8 additions & 7 deletions lighthouse-core/aggregator/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@ class Aggregate {
*/
static _getTotalWeight(expected) {
const expectedNames = Object.keys(expected);
let weight = expectedNames.reduce((last, e) => last + expected[e].weight, 0);
if (weight === 0) {
weight = 1;
}

return weight;
let totalWeight = expectedNames.reduce((last, e) => last + (expected[e].weight || 0), 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion (expected[e].weight || 0)is a bit hard to read. maybe add a comment that you default to 0

return totalWeight;
}

/**
Expand Down Expand Up @@ -158,9 +154,10 @@ class Aggregate {
Aggregate._remapResultsByName(
Aggregate._filterResultsByAuditNames(results, item.audits)
);
const maxScore = Aggregate._getTotalWeight(item.audits);

const subItems = [];
let overallScore = 0;
let maxScore = 1;

// Step through each item in the expected results, and add them
// to the overall score and add each to the subItems list.
Expand Down Expand Up @@ -197,6 +194,10 @@ class Aggregate {
e);
});

if (aggregationIsScored) {
maxScore = Aggregate._getTotalWeight(item.audits);
}

return {
overall: (overallScore / maxScore),
name: item.name,
Expand Down
13 changes: 1 addition & 12 deletions lighthouse-core/test/aggregator/aggregate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,7 @@ describe('Aggregate', () => {
const a = {};

const weight = Aggregate._getTotalWeight(a);
return assert.equal(weight, 1);
});

it('returns a weight of at least 1', () => {
const a = {
x: {
weight: 0
}
};

const weight = Aggregate._getTotalWeight(a);
return assert.equal(weight, 1);
return assert.equal(weight, 0);
});

it('generates the correct total weight', () => {
Expand Down