UNPKG

@wordpress/jest-puppeteer-axe

Version:
8 lines (7 loc) 5.73 kB
{ "version": 3, "sources": ["../src/index.ts"], "sourcesContent": ["/**\n * External dependencies\n */\nimport AxePuppeteer from '@axe-core/puppeteer';\nimport type { Page } from 'puppeteer-core';\nimport type { RunOptions, Spec, Result, NodeResult } from 'axe-core';\n\n/**\n * Formats the list of violations object returned by Axe analysis.\n *\n * @param {Array<Result>} violations The array with the errors found by Axe.\n *\n * @return {string} The user friendly message to display when the matcher fails.\n */\nfunction formatViolations( violations: Array< Result > ): string {\n\treturn violations\n\t\t.map( ( { help, helpUrl, id, nodes } ) => {\n\t\t\tlet output =\n\t\t\t\t`Rule: \"${ id }\" (${ help })\\n` +\n\t\t\t\t`Help: ${ helpUrl }\\n` +\n\t\t\t\t'Affected Nodes:\\n';\n\n\t\t\tnodes.forEach( ( node: NodeResult ) => {\n\t\t\t\tif ( node.any.length ) {\n\t\t\t\t\toutput += ` ${ node.target }\\n`;\n\t\t\t\t\toutput += ' Fix ANY of the following:\\n';\n\t\t\t\t\tnode.any.forEach( ( item ) => {\n\t\t\t\t\t\toutput += ` - ${ item.message }\\n`;\n\t\t\t\t\t} );\n\t\t\t\t}\n\n\t\t\t\tif ( node.all.length ) {\n\t\t\t\t\toutput += ` ${ node.target }\\n`;\n\t\t\t\t\toutput += ' Fix ALL of the following:\\n';\n\t\t\t\t\tnode.all.forEach( ( item ) => {\n\t\t\t\t\t\toutput += ` - ${ item.message }.\\n`;\n\t\t\t\t\t} );\n\t\t\t\t}\n\n\t\t\t\tif ( node.none.length ) {\n\t\t\t\t\toutput += ` ${ node.target }\\n`;\n\t\t\t\t\toutput += ' Fix ALL of the following:\\n';\n\t\t\t\t\tnode.none.forEach( ( item ) => {\n\t\t\t\t\t\toutput += ` - ${ item.message }.\\n`;\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t} );\n\t\t\treturn output;\n\t\t} )\n\t\t.join( '\\n' );\n}\n\ninterface AxeTestParams {\n\t/**\n\t * CSS selector(s) to include in analysis.\n\t */\n\tinclude?: string | string[];\n\t/**\n\t * CSS selector(s) to exclude from analysis.\n\t */\n\texclude?: string | string[];\n\t/**\n\t * List of Axe rules to skip from verification.\n\t */\n\tdisabledRules?: string[];\n\t/**\n\t * Options to configure how Axe run operates.\n\t * @see https://github.com/dequelabs/axe-core/blob/HEAD/doc/API.md#options-parameter\n\t */\n\toptions?: RunOptions;\n\t/**\n\t * Axe configuration object.\n\t * @see https://github.com/dequelabs/axe-core/blob/HEAD/doc/API.md#api-name-axeconfigure\n\t */\n\tconfig?: Spec;\n}\n\n/**\n * Defines async matcher to check whether a given Puppeteer's page instance passes Axe accessibility tests.\n *\n * @see https://www.deque.com/axe/\n * It is possible to pass optional Axe API options to perform customized check.\n * @see https://github.com/dequelabs/axe-core-npm/tree/develop/packages/puppeteer\n *\n * @param {import('@jest/expect').MatcherContext} this Matcher context from Jest.\n * @param {Page} page Puppeteer's page instance.\n * @param {AxeTestParams} params Optional params that allow better control over Axe API.\n * @return A matcher object with two keys `pass` and `message`.\n */\nasync function toPassAxeTests(\n\tthis: jest.MatcherContext,\n\tpage: Page,\n\t{ include, exclude, disabledRules, options, config }: AxeTestParams = {}\n) {\n\tconst axe = new AxePuppeteer( page );\n\n\tif ( include ) {\n\t\taxe.include( include );\n\t}\n\n\tif ( exclude ) {\n\t\taxe.exclude( exclude );\n\t}\n\n\tif ( options ) {\n\t\taxe.options( options );\n\t}\n\n\tif ( disabledRules ) {\n\t\taxe.disableRules( disabledRules );\n\t}\n\n\tif ( config ) {\n\t\taxe.configure( config );\n\t}\n\n\tconst { violations } = await axe.analyze();\n\n\tconst pass = violations.length === 0;\n\tconst message = pass\n\t\t? () => {\n\t\t\t\treturn (\n\t\t\t\t\tthis.utils.matcherHint( '.not.toPassAxeTests' ) +\n\t\t\t\t\t'\\n\\n' +\n\t\t\t\t\t'Expected page to contain accessibility check violations.\\n' +\n\t\t\t\t\t'No violations found.'\n\t\t\t\t);\n\t\t }\n\t\t: () => {\n\t\t\t\treturn (\n\t\t\t\t\tthis.utils.matcherHint( '.toPassAxeTests' ) +\n\t\t\t\t\t'\\n\\n' +\n\t\t\t\t\t'Expected page to pass Axe accessibility tests.\\n' +\n\t\t\t\t\t'Violations found:\\n' +\n\t\t\t\t\tthis.utils.RECEIVED_COLOR( formatViolations( violations ) )\n\t\t\t\t);\n\t\t };\n\n\treturn {\n\t\tmessage,\n\t\tpass,\n\t};\n}\n\nexpect.extend( {\n\ttoPassAxeTests,\n} );\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAGA,uBAAyB;AAWzB,SAAS,iBAAkB,YAAsC;AAChE,SAAO,WACL,IAAK,CAAE,EAAE,MAAM,SAAS,IAAI,MAAM,MAAO;AACzC,QAAI,SACH,UAAW,EAAG,MAAO,IAAK;AAAA,QAChB,OAAQ;AAAA;AAAA;AAGnB,UAAM,QAAS,CAAE,SAAsB;AACtC,UAAK,KAAK,IAAI,QAAS;AACtB,kBAAU,KAAM,KAAK,MAAO;AAAA;AAC5B,kBAAU;AACV,aAAK,IAAI,QAAS,CAAE,SAAU;AAC7B,oBAAU,SAAU,KAAK,OAAQ;AAAA;AAAA,QAClC,CAAE;AAAA,MACH;AAEA,UAAK,KAAK,IAAI,QAAS;AACtB,kBAAU,KAAM,KAAK,MAAO;AAAA;AAC5B,kBAAU;AACV,aAAK,IAAI,QAAS,CAAE,SAAU;AAC7B,oBAAU,WAAY,KAAK,OAAQ;AAAA;AAAA,QACpC,CAAE;AAAA,MACH;AAEA,UAAK,KAAK,KAAK,QAAS;AACvB,kBAAU,KAAM,KAAK,MAAO;AAAA;AAC5B,kBAAU;AACV,aAAK,KAAK,QAAS,CAAE,SAAU;AAC9B,oBAAU,WAAY,KAAK,OAAQ;AAAA;AAAA,QACpC,CAAE;AAAA,MACH;AAAA,IACD,CAAE;AACF,WAAO;AAAA,EACR,CAAE,EACD,KAAM,IAAK;AACd;AAuCA,eAAe,eAEd,MACA,EAAE,SAAS,SAAS,e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