Skip to content

Commit

Permalink
Ensure default options do not mutate
Browse files Browse the repository at this point in the history
Lodash's merge function mutates the first object,
this was setting defaults where we didn't want them.

This change makes sure the merging is happening to a new object, so that tests do not mutate each other.
  • Loading branch information
NickColley committed Feb 13, 2018
1 parent 53cefa9 commit 60412a5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
18 changes: 17 additions & 1 deletion __snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,21 @@ Received:
\\"Form elements must have labels (label)\\"
Try fixing it with this help: https://dequeuniversity.com/rules/axe/2.6/label?application=axeAPI"
Try fixing it with this help: https://dequeuniversity.com/rules/axe/2.6/label?application=axeAPI
────────
Expected the HTML found at $('body > a[href$=\\"#link-name\\"]') to have no violations:
<a href=\\"#link-name\\"></a>
Expected the HTML found at $('body > a[href$=\\"#link-name-2\\"]') to have no violations:
<a href=\\"#link-name-2\\"></a>
Received:
\\"Links must have discernible text (link-name)\\"
Try fixing it with this help: https://dequeuniversity.com/rules/axe/2.6/link-name?application=axeAPI"
`;
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function configureAxe (defaultOptions = {}) {
// aXe requires real Nodes so we need to inject into Jests' jsdom document.
document.body.innerHTML = html

const options = merge(defaultOptions, additionalOptions)
const options = merge({}, defaultOptions, additionalOptions)

return new Promise((resolve, reject) => {
axeCore.run(document.body, options, (err, results) => {
Expand Down
29 changes: 29 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,35 @@ describe('jest-axe', () => {
axe('Hello, World')
}).toThrow('html parameter ("Hello, World") has no elements')
})

it('should not mutate previous options', async () => {
let results = await axe(failingHtmlExample, {
rules: {
'link-name': { enabled: false }
}
})
let violations = results.violations
expect(violations.length).toBe(0)

const configuredAxe = configureAxe({
rules: {
'link-name': { enabled: false }
}
})

results = await configuredAxe(failingHtmlExample, {
rules: {
'link-name': { enabled: false }
}
})
violations = results.violations
expect(violations.length).toBe(0)

results = await axe(failingHtmlExample)
const violation = results.violations[0]
expect(violation.id).toBe('link-name')
expect(violation.description).toBe('Ensures links have discernible text')
})
})
describe('toHaveNoViolations', () => {
const failingAxeResults = {
Expand Down

0 comments on commit 60412a5

Please sign in to comment.