-
Notifications
You must be signed in to change notification settings - Fork 779
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): cleanup globals if set from context #2387
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please include a test to prevent regression.
I came here just to say that ☝️ We had discussed updating the |
I tried updating the example, but I would have to do some convoluted stuff to make it break. For example, just trying to recreate the DOM in the I also can't test this in the browser (you can't null |
It's not that bad. Here's what I did:
Updated JSDOM example/* eslint-env mocha */
const axe = require('axe-core');
const { JSDOM } = require('jsdom');
const assert = require('assert');
const config = {
rules: {
// `color-contrast` does not work with JSDOM.
'color-contrast': { enabled: false },
// In this example, we are not concerned with the following rules:
'landmark-one-main': { enabled: false },
'page-has-heading-one': { enabled: false },
region: { enabled: false }
}
};
const createDOM = (working = true) => {
const { window } = new JSDOM(`<!DOCTYPE html>
<html lang="en">
<head>
<title>JSDOM Example</title>
</head>
<body>
${
working
? `<div id="working">
<label for="has-label">Label for this text field.</label>
<input type="text" id="has-label">
</div>`
: `<div id="broken">
<p>Not a label</p><input type="text" id="no-label">
</div>`
}
</body>
</html>`);
return window;
};
describe('axe', () => {
it('should report that good HTML is good', async () => {
const window = createDOM(true);
const result = await axe.run(window.document.documentElement, config);
assert.equal(result.violations.length, 0, 'Violations is not empty');
});
it('should report that bad HTML is bad', async () => {
const window = createDOM(false);
const result = await axe.run(window.document.documentElement, config);
assert.equal(result.violations.length, 1, 'Violations.length is not 1');
});
}); |
Except we determined in our last discussion of this file being both an example and a test file, that it should remain an example. So I couldn't change the main package.json file to use the axe file from the repo, so had to edit https://github.com/dequelabs/axe-core/blob/develop/doc/examples/test-examples.js#L14-L20 as a workaround so we could leave it as an example and still test the examples using the local copy of axe. |
If you don't want to use the example as the test case, I'm fine with that. However, I agree with Wilco that this needs to be tested. Would you rather create a separate JSDOM test elsewhere? |
I guess we'll have to so this problem of example/test doesn't prevent us from testing fixes we need to |
@@ -0,0 +1,54 @@ | |||
var axe = require('../../'); | |||
var jsdom = require('jsdom'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we can use "modern JS" here, I really think we should. Thoughts on refactoring this to make it look like "today's JavaScript"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd love to but we can't. The entire test
dir is dictated by the eslint file in there that disallows any modern javascript... It's a bit of a pain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't sound like a "we can't", it just sounds like some ESLint settings need to be modified. We could add test/node/.eslintrc
and override the rules added there, or move that file into the directory(ies) that those rules are relevant for.
I don't consider this blocking tho. If it's too much work to mess with the ESLint stuff, I understand. We can always improve on this later.
test/node/jsdom.js
Outdated
rules: { 'color-contrast': { enabled: false } } | ||
}) | ||
.then(function(results) { | ||
assert.equal(results.violations.length, 2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why check that there are 2 violations? This number is subject to change any time we add/remove rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, doesn't matter, just check there is a result.
assert.equal(results.violations.length, 2); | ||
done(); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without a .catch
, this could cause an unhandled rejection and crash the process.
test/node/jsdom.js
Outdated
rules: { 'color-contrast': { enabled: false } } | ||
}) | ||
.then(function(results) { | ||
assert.equal(results.violations.length, 2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, doesn't matter, just check there is a result.
test/node/jsdom.js
Outdated
|
||
var dom = new jsdom.JSDOM(domStr); | ||
|
||
axe |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you return the promise, you can chain it, instead of nesting another promise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, if we chain and return the promise, we don't have to add a .catch()
or mess with done
. Mocha has had support for promises for awhile now.
Co-authored-by: Stephen Mathieson <[email protected]>
Co-authored-by: Stephen Mathieson <[email protected]>
As discussed in the issue, just needed to clean up the set globals as jsdom creates new globals when called again.
Closes issue: #2347
Reviewer checks
Required fields, to be filled out by PR reviewer(s)