Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/core/public/finish-run.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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