Skip to content

Commit

Permalink
feat(reporter): return one result of each type instead of zero when r…
Browse files Browse the repository at this point in the history
…esultTypes is used (#604)

Fixes #603
  • Loading branch information
dylanb authored and WilcoFiers committed Nov 6, 2017
1 parent 15065f7 commit 216a83b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
5 changes: 3 additions & 2 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,13 +440,14 @@ Additionally, there are a number or properties that allow configuration of diffe
6. Only process certain types of results
The `resultTypes` option can be used to limit the result types that aXe will process, aggregate, and send to the reporter. This can be useful for improving performance on very large or complicated pages when you are only interested in certain types of results.
The `resultTypes` option can be used to limit the result types that aXe will process, aggregate, and send to the reporter. This can be useful for improving performance on very large or complicated pages when you are only interested in certain types of results. The option will return 1 result of each type if that rule has at least one of that type of result instead of returning all of that type of result. The caller can use this information to inform the user of the existence of that type of result if appropriate.
```javascript
{
resultTypes: ['violations', 'incomplete', 'inapplicable']
}
```
This example will only process the specified result types: "violations", "incomplete", and "inapplicable". Notably, it will not process "passes". On a series of extremely large pages, this could improve performance considerably.
This example will only process the specified result types: "violations", "incomplete", and "inapplicable". Notably, it will only process the first pass for each rule that has a pass. On a series of extremely large pages, this could improve performance considerably.
##### Callback Parameter
Expand Down
8 changes: 6 additions & 2 deletions lib/core/reporters/helpers/process-aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ helpers.processAggregate = function (results, options) {

resultKeys.forEach(function (key) {
if (options.resultTypes && !options.resultTypes.includes(key)) {
delete resultObject[key];
return;
// If the user asks us to, truncate certain finding types to maximum one finding
(resultObject[key] || []).forEach(function (ruleResult) {
if (Array.isArray(ruleResult.nodes) && ruleResult.nodes.length > 0) {
ruleResult.nodes = [ruleResult.nodes[0]];
}
});
}
resultObject[key] = (resultObject[key] || []).map(function (ruleResult) {
ruleResult = Object.assign({}, ruleResult);
Expand Down
52 changes: 48 additions & 4 deletions test/core/reporters/helpers/process-aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ describe('helpers.processAggregate', function () {
}],
all: [],
none: []
}, {
result: 'passed',
node: {
element: document.createElement('div'),
selector: 'main > .thing',
source: '<div class=\"thing\">Thing</div>',
xpath: '/main/div[@class="thing"]'
},
any: [{
id: 'passed-rule',
relatedNodes: [{
element: document.createElement('div'),
selector: 'footer > .thing',
source: '<div class=\"thing\">Thing</div>',
xpath: '/footer/div[@class="thing"]',
}]
}],
all: [],
none: []
}],
inapplicable: [],
incomplete: [],
Expand All @@ -51,6 +70,26 @@ describe('helpers.processAggregate', function () {
}],
all: [],
none: []
}, {
result: 'failed',
node: {
selector: '#dopell',
source: '<input id=\"dopell\"/>',
xpath: '/header/input[@id="dopell"]',
fromFrame: true
},
any: [{
id: 'failed-rule',
relatedNodes: [{
element: document.createElement('input'),
selector: '#dopell',
source: '<input id=\"dopell\"/>',
xpath: '/header/input[@id="dopell"]',
fromFrame: true
}]
}],
all: [],
none: []
}],
inapplicable: [],
passes: [],
Expand Down Expand Up @@ -96,12 +135,17 @@ describe('helpers.processAggregate', function () {

describe('`resultTypes` option', function () {

it('should remove non-specified result types from the `resultObject`', function () {
var resultObject = helpers.processAggregate(results, { resultTypes: ['passes', 'violations'] });
it('should reduce the unwanted result types to 1 in the `resultObject`', function () {
var resultObject = helpers.processAggregate(results, { resultTypes: ['violations'] });
assert.isDefined(resultObject.passes);
assert.equal(resultObject.passes[0].nodes.length, 1);
assert.isDefined(resultObject.violations);
assert.isUndefined(resultObject.incomplete);
assert.isUndefined(resultObject.inapplicable);
assert.equal(resultObject.violations[0].nodes.length, 2);
resultObject = helpers.processAggregate(results, { resultTypes: ['passes'] });
assert.equal(resultObject.passes[0].nodes.length, 2);
assert.equal(resultObject.violations[0].nodes.length, 1);
assert.isDefined(resultObject.incomplete);
assert.isDefined(resultObject.inapplicable);
});
});

Expand Down

0 comments on commit 216a83b

Please sign in to comment.