UNPKG

intern-a11y

Version:

Intern-a11y. An accessibility testing helper for Intern.

82 lines 3.68 kB
"use strict"; var fs_1 = require("fs"); var common_1 = require("../common"); var _axe_1 = require("./_axe"); function createChecker(options) { return function () { options = options || {}; var axePath = require.resolve('axe-core/axe.min'); var axeScript = fs_1.readFileSync(axePath, { encoding: 'utf8' }); var axeContext = options.context; var config = options.config; var axeConfig = null; if (config) { axeConfig = { branding: config.branding, reporter: config.reporter, rules: config.rules }; if (config.checks) { axeConfig.checks = config.checks.map(function (check) { return { id: check.id, evaluate: check.evaluate.toString(), after: check.after ? check.after.toString() : undefined, options: check.options, matches: check.matches, enabled: check.enabled }; }); } } return this.parent .getExecuteAsyncTimeout() .then(function (timeout) { return this.parent .setExecuteAsyncTimeout(30000) .execute(axeScript, []) .executeAsync("return (function (config, context, done) {\n\t\t\t\t\t\tif (config) {\n\t\t\t\t\t\t\tif (config.checks) {\n\t\t\t\t\t\t\t\tconfig.checks.forEach(function (check) {\n\t\t\t\t\t\t\t\t\teval('check.evaluate = ' + check.evaluate);\n\t\t\t\t\t\t\t\t\tif (check.after) {\n\t\t\t\t\t\t\t\t\t\teval('check.after = ' + check.after);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\taxe.configure(config);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!context) {\n\t\t\t\t\t\t\tcontext = document;\n\t\t\t\t\t\t}\n\t\t\t\t\t\taxe.a11yCheck(context, function(results) {\n\t\t\t\t\t\t\tdone(results);\n\t\t\t\t\t\t});\n\t\t\t\t\t}).apply(this, arguments)", [axeConfig, axeContext]) .then(function (results) { var a11yResults = _axe_1.toA11yResults(results); var numViolations = (results.violations && results.violations.length) || 0; var error; if (numViolations === 1) { error = new common_1.A11yError('1 a11y violation was logged', a11yResults); } if (numViolations > 1) { error = new common_1.A11yError(numViolations + ' a11y violations were logged', a11yResults); } if (error) { throw error; } return a11yResults; }) .then(function (results) { return this.parent .setExecuteAsyncTimeout(timeout) .then(function () { return results; }); }, function (error) { return this.parent .setExecuteAsyncTimeout(timeout) .then(function () { throw error; }); }); }); }; } exports.createChecker = createChecker; function check(options) { if (options.remote == null) { return Promise.reject(new Error('A remote is required when calling check()')); } var chain = options.remote.get(options.source); if (options.waitFor) { chain = chain.sleep(options.waitFor); } return chain.then(createChecker(options)); } exports.check = check; //# sourceMappingURL=axe.js.map