Skip to content

Commit

Permalink
fix(run,finishRun): don't mutate options, set default reporter to v1 (#…
Browse files Browse the repository at this point in the history
…3088)

* fix(run,finishRun): don't mutate options, set default reporter to v1

* normalize options in runOnly

* fix test
  • Loading branch information
straker committed Jul 23, 2021
1 parent 63b6c7b commit 90f0b27
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 8 deletions.
9 changes: 8 additions & 1 deletion lib/core/public/finish-run.js
Expand Up @@ -3,10 +3,17 @@ import {
mergeResults,
publishMetaData,
finalizeRuleResult,
DqElement
DqElement,
clone
} from '../utils';

export default function finishRun(partialResults, options = {}) {
options = clone(options);

// normalize the runOnly option for the output of reporters toolOptions
axe._audit.normalizeOptions(options);
options.reporter = options.reporter ?? axe._audit?.reporter ?? 'v1';

setFrameSpec(partialResults);
let results = mergeResults(partialResults);
results = axe._audit.after(results, options);
Expand Down
3 changes: 3 additions & 0 deletions lib/core/public/run/normalize-run-params.js
@@ -1,3 +1,5 @@
import { clone } from '../../utils';

/**
* Normalize the optional params of axe.run()
* @param {object} context
Expand Down Expand Up @@ -36,6 +38,7 @@ export default function normalizeRunParams([context, options, callback]) {
throw typeErr;
}

options = clone(options);
options.reporter = options.reporter ?? axe._audit?.reporter ?? 'v1';
return { context, options, callback };
}
Expand Down
46 changes: 45 additions & 1 deletion test/core/public/finish-run.js
Expand Up @@ -21,6 +21,20 @@ describe('axe.finishRun', function() {
.catch(done);
});

it('does not mutate the options object', function(done) {
var options = {};
axe
.runPartial(options)
.then(function(result) {
return axe.finishRun([result], options);
})
.then(function() {
assert.deepEqual(options, {});
done();
})
.catch(done);
});

it('uses option.reporter to create the report', function(done) {
axe
.runPartial()
Expand All @@ -40,6 +54,35 @@ describe('axe.finishRun', function() {
.catch(done);
});

it('defaults options.reporter to v1', function(done) {
axe
.runPartial()
.then(function(partialResult) {
return axe.finishRun([partialResult]);
})
.then(function(results) {
assert.equal(results.toolOptions.reporter, 'v1');
done();
})
.catch(done);
});

it('normalizes the runOnly option in the reporter', function(done) {
axe
.runPartial()
.then(function(partialResult) {
return axe.finishRun([partialResult], { runOnly: 'region' });
})
.then(function(results) {
assert.deepEqual(results.toolOptions.runOnly, {
type: 'rule',
values: ['region']
});
done();
})
.catch(done);
});

it('can report violations results', function(done) {
fixture.innerHTML = '<div aria-label="foo"></div>';
axe
Expand Down Expand Up @@ -241,7 +284,8 @@ describe('axe.finishRun', function() {
.then(function() {
assert.lengthOf(axe._audit.after.args, 1);
assert.deepEqual(axe._audit.after.args[0][1], {
runOnly: 'duplicate-id'
runOnly: { type: 'rule', values: ['duplicate-id'] },
reporter: 'v1'
});
spy.restore();
done();
Expand Down
13 changes: 12 additions & 1 deletion test/core/public/run-partial.js
Expand Up @@ -35,7 +35,7 @@ describe('axe.runPartial', function() {

it('normalizes the options argument', function(done) {
axe
.runPartial(/* no context */{ runOnly: 'image-alt' })
.runPartial(/* no context */ { runOnly: 'image-alt' })
.then(function(partialResult) {
assert.lengthOf(partialResult.results, 1);
assert.equal(partialResult.results[0].id, 'image-alt');
Expand All @@ -44,6 +44,17 @@ describe('axe.runPartial', function() {
.catch(done);
});

it('does not mutate the options object', function(done) {
var options = {};
axe
.runPartial(options)
.then(function() {
assert.deepEqual(options, {});
done();
})
.catch(done);
});

describe('result', function() {
var partialResult;
before(function(done) {
Expand Down
8 changes: 8 additions & 0 deletions test/core/public/run.js
Expand Up @@ -70,6 +70,14 @@ describe('axe.run', function() {
axe.run(document, noop);
});

it('does not mutate the options object', function(done) {
var options = {};
axe.run(options, function() {
assert.deepEqual(options, {});
done();
});
});

it('works with performance logging enabled', function(done) {
axe.run(document, { performanceTimer: true }, function(err, result) {
assert.isObject(result);
Expand Down
6 changes: 1 addition & 5 deletions test/testutils.js
Expand Up @@ -475,11 +475,7 @@ testUtils.runPartialRecursive = function runPartialRecursive(
options = options || {};
win = win || window;
var axe = win.axe;

// axe.utils.getFrameContexts mutates
// https://github.com/dequelabs/axe-core/issues/3045
var contextCopy = axe.utils.clone(context);
var frameContexts = axe.utils.getFrameContexts(contextCopy);
var frameContexts = axe.utils.getFrameContexts(context);
var promiseResults = [axe.runPartial(context, options)];

frameContexts.forEach(function(c) {
Expand Down

0 comments on commit 90f0b27

Please sign in to comment.