diff --git a/lib/core/public/configure.js b/lib/core/public/configure.js index 52d8cd280f..e7f06dbf7a 100644 --- a/lib/core/public/configure.js +++ b/lib/core/public/configure.js @@ -44,14 +44,40 @@ function configureChecksRulesAndBranding(spec) { } if (spec.checks) { + if (!Array.isArray(spec.checks)) { + throw new TypeError('Checks property must be an array'); + } + spec.checks.forEach(function(check) { + if (!check.id) { + throw new TypeError( + // eslint-disable-next-line max-len + `Configured check ${JSON.stringify( + check + )} is invalid. Checks must be an object with at least an id property` + ); + } + audit.addCheck(check); }); } const modifiedRules = []; if (spec.rules) { + if (!Array.isArray(spec.rules)) { + throw new TypeError('Rules property must be an array'); + } + spec.rules.forEach(function(rule) { + if (!rule.id) { + throw new TypeError( + // eslint-disable-next-line max-len + `Configured rule ${JSON.stringify( + rule + )} is invalid. Rules must be an object with at least an id property` + ); + } + modifiedRules.push(rule.id); audit.addRule(rule); }); diff --git a/test/core/public/configure.js b/test/core/public/configure.js index 8b0858a31c..bd0de22286 100644 --- a/test/core/public/configure.js +++ b/test/core/public/configure.js @@ -56,6 +56,30 @@ describe('axe.configure', function() { assert.deepEqual(axe._audit.data.rules.bob.joe, 'joe'); }); + it('should throw error if rules property is invalid', function() { + assert.throws(function() { + axe.configure({ rules: 'hello' }), + TypeError, + /^Rules property must be an array/; + }); + }); + + it('should throw error if rule is invalid', function() { + assert.throws(function() { + axe.configure({ rules: ['hello'] }), + TypeError, + /Configured rule "hello" is invalid/; + }); + }); + + it('should throw error if rule does not have an id', function() { + assert.throws(function() { + axe.configure({ rules: [{ foo: 'bar' }] }), + TypeError, + /Configured rule "{foo:\"bar\"}" is invalid/; + }); + }); + it('should call setBranding when passed options', function() { axe._load({}); axe.configure({ @@ -158,6 +182,30 @@ describe('axe.configure', function() { assert.equal(axe._audit.data.checks.bob.joe, 'joe'); }); + it('should throw error if checks property is invalid', function() { + assert.throws(function() { + axe.configure({ checks: 'hello' }), + TypeError, + /^Checks property must be an array/; + }); + }); + + it('should throw error if check is invalid', function() { + assert.throws(function() { + axe.configure({ checks: ['hello'] }), + TypeError, + /Configured check "hello" is invalid/; + }); + }); + + it('should throw error if check does not have an id', function() { + assert.throws(function() { + axe.configure({ checks: [{ foo: 'bar' }] }), + TypeError, + /Configured check "{foo:\"bar\"}" is invalid/; + }); + }); + it('should allow for the overwriting of checks', function() { axe._load({ data: {