From 511d0730dc800f8184c26e58faeb26dfa8168d86 Mon Sep 17 00:00:00 2001 From: Ben Monro Date: Fri, 12 Jun 2020 07:34:33 -0700 Subject: [PATCH] fix: return violations --- README.md | 34 +++++++++++++++++++++++++--------- index.js | 2 ++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d9ae3cc..718ecfd 100644 --- a/README.md +++ b/README.md @@ -18,20 +18,20 @@ Add the following clientScript in your testcafe config: ``` ```js -import { axeCheck, createReport } from '@testcafe-community/axe'; +import { checkForViolations } from '@testcafe-community/axe'; fixture `TestCafe tests with Axe` .page `http://example.com`; test('Automated accessibility testing', async t => { - const { error, violations } = await axeCheck(t); - await t.expect(violations.length === 0).ok(createReport(violations)); + // do stuff on your page + await checkForViolations(); }); ``` -If any accessibility issues are found, you will see a detailed report generated by the `createReport` function. +If any accessibility issues are found, you will see a detailed report in the error log. -![Accessibility errors](https://github.com/helen-dikareva/@testcafe-community/axe/blob/master/errors.png) +![Accessibility errors](https://github.com/testcafe-community/axe/blob/master/errors.png) ## aXe options @@ -39,8 +39,24 @@ The `@testcafe-community/axe` module allows you to define the `context` and `opt ```js test('Automated accessibility testing', async () => { - const axeContext = { exclude: [['select']] }; - const axeOptions = { rules: { 'html-has-lang': { enabled: false } } }; - const { error, violations } = await axeCheck(t, axeContext, axeOptions); - await t.expect(violations.length === 0).ok(createReport(violations)); + const context = { exclude: [['select']] }; + const options = { rules: { 'html-has-lang': { enabled: false } } }; + + await checkForViolations({context, options}); +}); +``` + +## Additional features + +By default `checkForViolations` does not allow any violations, but if you want more fine grained control +there are a couple options. First, you can pass in `numAllowed` which will only fail the test if the +number of violations exceeds that number. Secondly, `checkViolations` returns the list of violations from +axe-core so you can inspect it if needed. + +```js +test('Automated accessibility testing', async () => { + const {violations} = await checkForViolations({numAllowed:2}); + + // do stuff with violations. }); +``` diff --git a/index.js b/index.js index 518c0a7..e6dd4db 100644 --- a/index.js +++ b/index.js @@ -47,6 +47,8 @@ const checkForViolations = ({numAllowed=0,context,options}) => { const {violations} = await axeCheck(context, options); await t.expect(violations.length <= numAllowed).ok(createReport(violations)); + + return {violations}; } module.exports = {