component-driven / cypress-axe

Test accessibility with axe-core in Cypress
MIT License
621 stars 86 forks source link

Doesn't work with Yarn PnP #118

Open Dabolus opened 2 years ago

Dabolus commented 2 years ago

The injectAxe command currently directly looks for the node_modules folder to get axe-core source code. This - however - makes it incompatible with Yarn Plug'n'Play, since it doesn't rely on node_modules.

I worked it around by overwriting the injectAxe command in this way:

Cypress.Commands.overwrite('injectAxe', () => {
  cy.task('getAxeSource').then(axeSource =>
    cy.window({ log: false }).then(window => {
      const script = window.document.createElement('script');
      script.innerHTML = axeSource;
      window.document.head.appendChild(script);
    }),
  );
});

And adding this task to my Cypress plugins:

const axe = require('axe-core/axe.js');

module.exports = (on, config) => {
  on('task', {
    getAxeSource: () => axe.source,
  });
};

In this way, the task (that runs in Node) handles the require logic, while the command (that runs in the browser) gets the source from the task and injects it. This would probably also solve https://github.com/component-driven/cypress-axe/issues/73, which is somewhat related.

Let me know if you think this solution makes sense to you. If so, I will open a PR.

MatteoPieroni commented 2 years ago

Thank you @Dabolus , this fix does work for me as well where the regular import didn't, due to using NX to manage the tests

thomasobrien99 commented 2 years ago

Thanks @Dabolus, looks like this solution is also going to work for us