Skip to content

Commit

Permalink
update Accessibility 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 f9da435 commit a9e431d
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 143 deletions.
2 changes: 1 addition & 1 deletion lighthouse-core/audits/accessibility/axe-audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AxeAudit extends Audit {
* @return {!AuditResult}
*/
static audit(artifacts) {
const violations = artifacts.Accessibility.violations || [];
const violations = artifacts.Accessibility.violations;
const rule = violations.find(result => result.id === this.meta.name);

return this.generateAuditResult({
Expand Down
32 changes: 11 additions & 21 deletions lighthouse-core/gather/gatherers/accessibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,10 @@ function runA11yChecks() {
}

class Accessibility extends Gatherer {
static _errorAccessibility(errorString) {
return {
raw: undefined,
value: undefined,
debugString: errorString
};
}

/**
* @param {!Object} options
* @return {!Promise<{violations: !Array}>}
*/
afterPass(options) {
const driver = options.driver;
const expression = `(function () {
Expand All @@ -63,20 +59,14 @@ class Accessibility extends Gatherer {
})()`;

return driver
.evaluateAsync(expression)
.then(returnedValue => {
if (!returnedValue) {
return Accessibility._errorAccessibility('Unable to parse axe results');
}

if (returnedValue.error) {
return Accessibility._errorAccessibility(returnedValue.error);
}
.evaluateAsync(expression)
.then(returnedValue => {
if (!returnedValue || !Array.isArray(returnedValue.violations)) {
throw new Error('Unable to parse axe results' + returnedValue);
}

return returnedValue;
}, _ => {
return Accessibility._errorAccessibility('Axe results timed out');
});
return returnedValue;
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,4 @@ describe('Accessibility: aria-allowed-attr audit', () => {
assert.equal(output.rawValue, false);
assert.equal(output.displayValue, '');
});

it('doesn\'t throw an error when violations is undefined', () => {
const artifacts = {
Accessibility: {
violations: undefined
}
};

const output = Audit.audit(artifacts);
assert.equal(output.description, 'Element aria-* attributes are allowed for this role');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,4 @@ describe('Accessibility: aria-required-attr audit', () => {
assert.equal(output.rawValue, false);
assert.equal(output.displayValue, '');
});

it('doesn\'t throw an error when violations is undefined', () => {
const artifacts = {
Accessibility: {
violations: undefined
}
};

const output = Audit.audit(artifacts);
assert.equal(output.description,
'Elements with ARIA roles have the required aria-* attributes');
});
});
12 changes: 0 additions & 12 deletions lighthouse-core/test/audits/accessibility/aria-valid-attr-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,4 @@ describe('Accessibility: aria-valid-attr audit', () => {
assert.equal(output.rawValue, false);
assert.equal(output.displayValue, '');
});

it('doesn\'t throw an error when violations is undefined', () => {
const artifacts = {
Accessibility: {
violations: undefined
}
};

const output = Audit.audit(artifacts);
assert.equal(output.description,
'Element aria-* attributes are valid and not misspelled or non-existent.');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,4 @@ describe('Accessibility: aria-valid-attr-value audit', () => {
assert.equal(output.rawValue, false);
assert.equal(output.displayValue, '');
});

it('doesn\'t throw an error when violations is undefined', () => {
const artifacts = {
Accessibility: {
violations: undefined
}
};

const output = Audit.audit(artifacts);
assert.equal(output.description, 'Element aria-* attributes have valid values');
});
});
12 changes: 0 additions & 12 deletions lighthouse-core/test/audits/accessibility/color-contrast-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,4 @@ describe('Accessibility: color-contrast audit', () => {
assert.equal(output.rawValue, false);
assert.equal(output.displayValue, '');
});

it('doesn\'t throw an error when violations is undefined', () => {
const artifacts = {
Accessibility: {
violations: undefined
}
};

const output = Audit.audit(artifacts);
assert.equal(output.description,
'Background and foreground colors have a sufficient contrast ratio');
});
});
11 changes: 0 additions & 11 deletions lighthouse-core/test/audits/accessibility/image-alt-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,4 @@ describe('Accessibility: image-alt audit', () => {
assert.equal(output.rawValue, false);
assert.equal(output.displayValue, '');
});

it('doesn\'t throw an error when violations is undefined', () => {
const artifacts = {
Accessibility: {
violations: undefined
}
};

const output = Audit.audit(artifacts);
assert.equal(output.description, 'Every image element has an alt attribute');
});
});
11 changes: 0 additions & 11 deletions lighthouse-core/test/audits/accessibility/label-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,4 @@ describe('Accessibility: label audit', () => {
assert.equal(output.rawValue, false);
assert.equal(output.displayValue, '');
});

it('doesn\'t throw an error when violations is undefined', () => {
const artifacts = {
Accessibility: {
violations: undefined
}
};

const output = Audit.audit(artifacts);
assert.equal(output.description, 'Every form element has a label');
});
});
11 changes: 0 additions & 11 deletions lighthouse-core/test/audits/accessibility/tabindex-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,4 @@ describe('Accessibility: tabindex audit', () => {
assert.equal(output.rawValue, false);
assert.equal(output.displayValue, '');
});

it('doesn\'t throw an error when violations is undefined', () => {
const artifacts = {
Accessibility: {
violations: undefined
}
};

const output = Audit.audit(artifacts);
assert.equal(output.description, 'No element has a `tabindex` attribute greater than 0');
});
});
45 changes: 15 additions & 30 deletions lighthouse-core/test/gather/gatherers/accessibility-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,57 +27,42 @@ describe('Accessibility gatherer', () => {
accessibilityGather = new AccessibilityGather();
});

it('fails gracefully', () => {
it('fails if nothing is returned', () => {
return accessibilityGather.afterPass({
driver: {
evaluateAsync() {
return Promise.resolve();
}
}
}).then(artifact => {
assert.ok(typeof artifact === 'object');
});
}).then(
_ => assert.ok(false),
_ => assert.ok(true));
});

it('handles driver failure', () => {
return accessibilityGather.afterPass({
driver: {
evaluateAsync() {
return Promise.reject('such a fail');
}
}
}).then(artifact => {
assert.ok(artifact.debugString);
});
});

it('propagates error retrieving the results', () => {
const error = 'There was an error.';
it('fails if result has no violations array', () => {
return accessibilityGather.afterPass({
driver: {
evaluateAsync() {
return Promise.resolve({
error
url: 'https://example.com'
});
}
}
}).then(artifact => {
assert.ok(artifact.debugString === error);
});
}).then(
_ => assert.ok(false),
_ => assert.ok(true));
});

it('creates an object for valid results', () => {
it('propagates error retrieving the results', () => {
const error = 'There was an error.';
return accessibilityGather.afterPass({
driver: {
evaluateAsync() {
return Promise.resolve({
name: 'a11y'
});
return Promise.reject(new Error(error));
}
}
}).then(artifact => {
assert.ok(typeof artifact === 'object');
assert.equal(artifact.name, 'a11y');
});
}).then(
_ => assert.ok(false),
err => assert.ok(err.message.includes(error)));
});
});

0 comments on commit a9e431d

Please sign in to comment.