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): cleanup globals if set from context #2387

Merged
merged 6 commits into from
Jul 20, 2020
Merged

fix(run): cleanup globals if set from context #2387

merged 6 commits into from
Jul 20, 2020

Conversation

straker
Copy link
Contributor

@straker straker commented Jul 15, 2020

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)

  • Follows the commit message policy, appropriate for next version
  • Code is reviewed for security

@straker straker requested a review from a team as a code owner July 15, 2020 21:23
Copy link
Contributor

@WilcoFiers WilcoFiers left a 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.

@stephenmathieson
Copy link
Member

Please include a test to prevent regression.

I came here just to say that ☝️

We had discussed updating the jsdom example in order to accomplish this, as the examples are run by CI.

@straker
Copy link
Contributor Author

straker commented Jul 16, 2020

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 it gives me an error ReferenceError: Node is not defined. The main problem is that since the examples are using axe from 3.3.1, we can't do a whole lot of major changes to the file. I could probably get it to break the with example provided in this issue, but it doesn't fit very well in a public facing example of how to use JSOM.

I also can't test this in the browser (you can't null window or document) so we can only test in node.

@stephenmathieson
Copy link
Member

I tried updating the example, but I would have to do some convoluted stuff to make it break.

It's not that bad. Here's what I did:

npm run build
cd doc/examples/jsdom
# Use `"axe-core": "file://../../../"` in package.json, then
npm install jsdom@latest --save-dev
npm install
npm test
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');
  });
});

@straker
Copy link
Contributor Author

straker commented Jul 16, 2020

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.

@stephenmathieson
Copy link
Member

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?

@straker
Copy link
Contributor Author

straker commented Jul 16, 2020

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');
Copy link
Member

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"?

Copy link
Contributor Author

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.

Copy link
Member

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.

rules: { 'color-contrast': { enabled: false } }
})
.then(function(results) {
assert.equal(results.violations.length, 2);
Copy link
Member

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.

Copy link
Contributor

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();
});
});
Copy link
Member

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.

rules: { 'color-contrast': { enabled: false } }
})
.then(function(results) {
assert.equal(results.violations.length, 2);
Copy link
Contributor

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.


var dom = new jsdom.JSDOM(domStr);

axe
Copy link
Contributor

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.

Copy link
Member

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.

test/node/jsdom.js Outdated Show resolved Hide resolved
test/node/jsdom.js Outdated Show resolved Hide resolved
straker and others added 2 commits July 17, 2020 10:47
Co-authored-by: Stephen Mathieson <[email protected]>
Co-authored-by: Stephen Mathieson <[email protected]>
@straker straker merged commit d5b6931 into develop Jul 20, 2020
@straker straker deleted the jsdom branch July 20, 2020 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants