Skip to content

Commit

Permalink
update DateNowUse gatherer to use Errors for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Feb 4, 2017
1 parent 5bd18e9 commit 1a54269
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 65 deletions.
14 changes: 1 addition & 13 deletions lighthouse-core/audits/dobetterweb/no-datenow.js
Expand Up @@ -48,21 +48,9 @@ class NoDateNowAudit extends Audit {
* @return {!AuditResult}
*/
static audit(artifacts) {
if (artifacts.DateNowUse.value === -1) {
let debugString = 'Unknown error with the DateNowUse gatherer';
if (artifacts.DateNowUse.debugString) {
debugString = artifacts.DateNowUse.debugString;
}

return NoDateNowAudit.generateAuditResult({
rawValue: -1,
debugString
});
}

let debugString;
// Filter usage from other hosts and keep eval'd code.
const results = artifacts.DateNowUse.usage.filter(err => {
const results = artifacts.DateNowUse.filter(err => {
if (err.isEval) {
return !!err.url;
}
Expand Down
14 changes: 4 additions & 10 deletions lighthouse-core/gather/gatherers/dobetterweb/datenow.js
Expand Up @@ -29,17 +29,11 @@ class DateNowUse extends Gatherer {
this.collectUsage = options.driver.captureFunctionCallSites('Date.now');
}

/**
* @return {!Promise<!Array<{url: string, line: number, col: number}>>}
*/
afterPass() {
return this.collectUsage().then(dateNowUses => {
return {
usage: dateNowUses
};
}, e => {
return {
value: -1,
debugString: e.message
};
});
return this.collectUsage();
}
}

Expand Down
65 changes: 23 additions & 42 deletions lighthouse-core/test/audits/dobetterweb/no-datenow-test.js
Expand Up @@ -23,18 +23,9 @@ const URL = 'https://example.com';
/* eslint-env mocha */

describe('Page does not use Date.now()', () => {
it('fails when gatherer returns error', () => {
const debugString = 'interesting debug string';
const auditResult = DateNowUseAudit.audit({
DateNowUse: {value: -1, debugString}
});
assert.equal(auditResult.rawValue, -1);
assert.equal(auditResult.debugString, debugString);
});

it('passes when Date.now() is not used', () => {
const auditResult = DateNowUseAudit.audit({
DateNowUse: {usage: []},
DateNowUse: [],
URL: {finalUrl: URL},
});
assert.equal(auditResult.rawValue, true);
Expand All @@ -43,12 +34,10 @@ describe('Page does not use Date.now()', () => {

it('passes when Date.now() is used on a different origin', () => {
const auditResult = DateNowUseAudit.audit({
DateNowUse: {
usage: [
{url: 'http://different.com/two', line: 2, col: 2},
{url: 'http://example2.com/two', line: 2, col: 22}
]
},
DateNowUse: [
{url: 'http://different.com/two', line: 2, col: 2},
{url: 'http://example2.com/two', line: 2, col: 22}
],
URL: {finalUrl: URL},
});
assert.equal(auditResult.rawValue, true);
Expand All @@ -57,13 +46,11 @@ describe('Page does not use Date.now()', () => {

it('fails when Date.now() is used on the origin', () => {
const auditResult = DateNowUseAudit.audit({
DateNowUse: {
usage: [
{url: 'http://example.com/one', line: 1, col: 1},
{url: 'http://example.com/two', line: 10, col: 1},
{url: 'http://example2.com/two', line: 2, col: 22}
]
},
DateNowUse: [
{url: 'http://example.com/one', line: 1, col: 1},
{url: 'http://example.com/two', line: 10, col: 1},
{url: 'http://example2.com/two', line: 2, col: 22}
],
URL: {finalUrl: URL},
});
assert.equal(auditResult.rawValue, false);
Expand All @@ -76,12 +63,10 @@ describe('Page does not use Date.now()', () => {

it('only passes when has url property', () => {
const auditResult = DateNowUseAudit.audit({
DateNowUse: {
usage: [
{url: 'http://example.com/two', line: 10, col: 1},
{url: 'http://example2.com/two', line: 2, col: 22}
]
},
DateNowUse: [
{url: 'http://example.com/two', line: 10, col: 1},
{url: 'http://example2.com/two', line: 2, col: 22}
],
URL: {finalUrl: URL},
});

Expand All @@ -91,13 +76,11 @@ describe('Page does not use Date.now()', () => {

it('fails when console.time() is used in eval()', () => {
const auditResult = DateNowUseAudit.audit({
DateNowUse: {
usage: [
{url: 'http://example.com/one', line: 1, col: 1, isEval: false},
{url: 'module.exports (blah/handler.js:5:18)', line: 5, col: 18, isEval: true},
{url: 'module.exports (blah/handler.js:3:18)', line: 3, col: 18, isEval: true}
]
},
DateNowUse: [
{url: 'http://example.com/one', line: 1, col: 1, isEval: false},
{url: 'module.exports (blah/handler.js:5:18)', line: 5, col: 18, isEval: true},
{url: 'module.exports (blah/handler.js:3:18)', line: 3, col: 18, isEval: true}
],
URL: {finalUrl: URL}
});
assert.equal(auditResult.rawValue, false);
Expand All @@ -106,12 +89,10 @@ describe('Page does not use Date.now()', () => {

it('includes results when there is no .url', () => {
const auditResult = DateNowUseAudit.audit({
DateNowUse: {
usage: [
{line: 10, col: 1},
{line: 2, col: 22}
]
},
DateNowUse: [
{line: 10, col: 1},
{line: 2, col: 22}
],
URL: {finalUrl: URL},
});

Expand Down

0 comments on commit 1a54269

Please sign in to comment.