diff --git a/README.md b/README.md index 11cc45e..461b88f 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,9 @@ The `verbose` key is a boolean to whether to print the message `No accessibility ##### reporter (optional) -A class instance that implements the `Reporter` interface or values `default` and `v2`. Custom reporter instances can be supplied to override default reporting behaviour dictated by `DefaultTerminalReporter` set by the value `default`. `v2` is the new TerminalReporter inspired by the reports from [jest-axe](https://github.com/nickcolley/jest-axe). +A class instance that implements the `Reporter` interface or values `default`, `v2` and `html`. Custom reporter instances can be supplied to override default reporting behaviour dictated by `DefaultTerminalReporter` set by the value `default`. `v2` is the new TerminalReporter inspired by the reports from [jest-axe](https://github.com/nickcolley/jest-axe). `html` reporter will generate external HTML file. + +Note! `html` reporter will disable printed to logs violations. ##### skipFailures (optional, defaults to false) diff --git a/src/index.ts b/src/index.ts index 377fbf5..fd07420 100644 --- a/src/index.ts +++ b/src/index.ts @@ -103,21 +103,17 @@ export const checkA11y = async ( context: ElementContext | undefined = undefined, axeOptions: AxeOptions | undefined = undefined, skipFailures: boolean = false, - reporter: Reporter | 'default' | 'v2' = 'default', + reporter: Reporter | 'default' | 'html' | 'v2' = 'default', options: Options | undefined = undefined ): Promise => { const violations = await getViolations(page, context, axeOptions?.axeOptions) - if (violations.length > 0) { - await createHtmlReport({ results: { violations }, options } as CreateReport) - } else console.log("There were no violations to save in report"); - const impactedViolations = getImpactedViolations( violations, axeOptions?.includedImpacts, ) - let reporterWithOptions: Promise | Reporter | void + let reporterWithOptions: Promise | Reporter | void | any if (reporter === 'default') { reporterWithOptions = new DefaultTerminalReporter( @@ -127,13 +123,17 @@ export const checkA11y = async ( ) } else if (reporter === 'v2') { reporterWithOptions = new TerminalReporterV2(axeOptions?.verbose ?? false) + } else if (reporter === 'html') { + if (violations.length > 0) { + await createHtmlReport({ results: { violations }, options } as CreateReport) + } else console.log("There were no violations to save in report"); } else { reporterWithOptions = reporter } - await reportViolations(impactedViolations, reporterWithOptions) + if (reporter !== 'html') await reportViolations(impactedViolations, reporterWithOptions) - if (reporter !== 'v2') testResultDependsOnViolations(impactedViolations, skipFailures) + if (reporter === 'v2' || reporter !== 'html') testResultDependsOnViolations(impactedViolations, skipFailures) } export { DefaultTerminalReporter } diff --git a/test/a11y.spec.ts b/test/a11y.spec.ts index fb28855..b97cbb1 100644 --- a/test/a11y.spec.ts +++ b/test/a11y.spec.ts @@ -188,7 +188,7 @@ describe('Playwright web page accessibility test using generated html report wit }, }, }, - true, 'default', + false, 'html', { outputDirPath: 'results', outputDir: 'accessibility',