Skip to content

Commit

Permalink
feat(options): accept a string for options.runOnly
Browse files Browse the repository at this point in the history
  • Loading branch information
clottman authored Jun 17, 2021
1 parent 6eecf34 commit 4392bc0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion axe.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ declare namespace axe {
};
}
interface RunOptions {
runOnly?: RunOnly | TagValue[] | string[];
runOnly?: RunOnly | TagValue[] | string[] | string;
rules?: RuleObject;
reporter?: ReporterVersion;
resultTypes?: resultGroups[];
Expand Down
39 changes: 34 additions & 5 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,11 +484,27 @@ axe.run(
Alternatively, runOnly can be passed an array of tags:

```js
axe.run({
runOnly: ['wcag2a', 'wcag2aa'];
}, (err, results) => {
// ...
})
axe.run(
{
runOnly: ['wcag2a', 'wcag2aa']
},
(err, results) => {
// ...
}
);
```

If you want to specify just one tag, you can pass in a string.

```js
axe.run(
{
runOnly: 'wcag2a'
},
(err, results) => {
// ...
}
);
```

2. Run only a specified list of Rules
Expand Down Expand Up @@ -521,6 +537,19 @@ axe.run({
})
```

If you want to specify just one rule, you can pass in a string.

```js
axe.run(
{
runOnly: 'ruleId1'
},
(err, results) => {
// ...
}
);
```

3. Run all enabled Rules except for a list of rules

The default operation for axe.run is to run all rules except for rules with the "experimental" tag. If certain rules should be disabled from being run, specify `options` as:
Expand Down
5 changes: 4 additions & 1 deletion lib/core/base/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,10 @@ class Audit {
});
});
// Validate runOnly
if (typeof options.runOnly === 'object') {
if (['object', 'string'].includes(typeof options.runOnly)) {
if (typeof options.runOnly === 'string') {
options.runOnly = [options.runOnly];
}
if (Array.isArray(options.runOnly)) {
const hasTag = options.runOnly.find(value => tags.includes(value));
const hasRule = options.runOnly.find(value => ruleIds.includes(value));
Expand Down
7 changes: 7 additions & 0 deletions test/core/base/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,13 @@ describe('Audit', function() {
assert.deepEqual(out.runOnly.values, ['positive1', 'negative1']);
});

it('allows runOnly as a string as an alternative to an array', function() {
var opt = { runOnly: 'positive1' };
var out = a.normalizeOptions(opt);
assert(out.runOnly.type, 'rule');
assert.deepEqual(out.runOnly.values, ['positive1']);
});

it('throws an error if runOnly contains both rules and tags', function() {
assert.throws(function() {
a.normalizeOptions({
Expand Down

0 comments on commit 4392bc0

Please sign in to comment.