Skip to content

Commit 73883e9

Browse files
authored
Merge pull request #20 from DesselBane/feat/add-custom-result-handler
feat: add custom result handler
2 parents f34773b + 1a11476 commit 73883e9

File tree

4 files changed

+51
-27
lines changed

4 files changed

+51
-27
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,28 @@ if (process.env.NODE_ENV !== 'production') {
3636
|Key|Description|Default|Required|
3737
|---|---|---|---|
3838
|`clearConsoleOnUpdate`|Clears the console each time `vue-axe` runs|`true`|`false`|
39-
|`config`|Provide your Axe-core configuration: [API Name: axe.configure](https://github.com/dequelabs/axe-core/blob/master/doc/API.md#api-name-axeconfigure)| |`false`|
39+
|`customResultHandler`|Handle the results from an `axe.run()`. This may be needed for automated tests.|`undefined`|`false`|
40+
|`config`|Provide your Axe-core configuration: https://github.com/dequelabs/axe-core/blob/master/doc/API.md#api-name-axeconfigure| |`false`|
4041
|`runOptions`|Provide your Axe-core runtime options: [API Name: axe.run](https://github.com/dequelabs/axe-core/blob/master/doc/API.md#options-parameter)|`{ reporter: 'v2', resultTypes: ['violations'] }`|`false`|
4142

43+
#### Custom Result Handler
44+
45+
The `customResultHandler` config property expects a callback like the `axe.run()` callback ([see documentation](https://github.com/dequelabs/axe-core/blob/master/doc/API.md#parameters-axerun)). It will be triggered after each call to `axe.run()`.
46+
47+
```javascript
48+
import Vue from 'vue'
49+
50+
if (process.env.NODE_ENV !== 'production') {
51+
const VueAxe = require('vue-axe')
52+
Vue.use(VueAxe, {
53+
customResultHandler: (error, results) => {
54+
results.violations.forEach(violation => console.log(violation))
55+
}
56+
// ...
57+
})
58+
}
59+
```
60+
4261
## Install in Nuxt.js
4362
Create plugin file `plugins/axe.js`
4463
```javascript

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@
5959
"vue-template-compiler": "^2.6.11"
6060
},
6161
"dependencies": {
62-
"axe-core": "^3.4.1"
62+
"axe-core": "3.5.3"
6363
}
6464
}

src/utils.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,41 @@ export function checkAndReport (options, node) {
2222
console.clear()
2323
}
2424

25-
results.violations = results.violations.filter(result => {
26-
result.nodes = result.nodes.filter(node => {
27-
let key = node.target.toString() + result.id
28-
let retVal = (!cache[key])
29-
cache[key] = key
30-
return retVal
31-
})
32-
return (!!result.nodes.length)
33-
})
25+
options.customResultHandler ? options.customResultHandler(error, results) : standardResultHandler(error, results)
3426

35-
if (results.violations.length) {
36-
console.group('%cNew aXe issues', STYLE.head)
37-
results.violations.forEach(result => {
38-
let styl = IMPACT.hasOwnProperty(result.impact) ? IMPACT[result.impact] : IMPACT.minor
39-
console.groupCollapsed('%c%s: %c%s %s', STYLE[styl], result.impact, STYLE.defaultReset, result.help, result.helpUrl)
40-
result.nodes.forEach(function (node) {
41-
failureSummary(node, 'any')
42-
failureSummary(node, 'none')
43-
})
44-
console.groupEnd()
45-
})
46-
console.groupEnd()
47-
}
4827
deferred.resolve()
4928

5029
lastNotification = JSON.stringify(results.violations)
5130
})
5231
return deferred.promise
5332
}
5433

34+
const standardResultHandler = function (errorInfo, results) {
35+
results.violations = results.violations.filter(result => {
36+
result.nodes = result.nodes.filter(node => {
37+
let key = node.target.toString() + result.id
38+
let retVal = (!cache[key])
39+
cache[key] = key
40+
return retVal
41+
})
42+
return (!!result.nodes.length)
43+
})
44+
45+
if (results.violations.length) {
46+
console.group('%cNew aXe issues', STYLE.head)
47+
results.violations.forEach(result => {
48+
let styl = IMPACT.hasOwnProperty(result.impact) ? IMPACT[result.impact] : IMPACT.minor
49+
console.groupCollapsed('%c%s: %c%s %s', STYLE[styl], result.impact, STYLE.defaultReset, result.help, result.helpUrl)
50+
result.nodes.forEach(function (node) {
51+
failureSummary(node, 'any')
52+
failureSummary(node, 'none')
53+
})
54+
console.groupEnd()
55+
})
56+
console.groupEnd()
57+
}
58+
}
59+
5560
export function resetCache () {
5661
cache = {}
5762
}

0 commit comments

Comments
 (0)