Giter Site home page Giter Site logo

Resolved path for `axe-core/axe.min.js` is incorrect if the Cypress Config is in a sub-directory when using Cypress v10+ about cypress-axe HOT 17 OPEN

aturingmachine avatar aturingmachine commented on June 15, 2024 19
Resolved path for `axe-core/axe.min.js` is incorrect if the Cypress Config is in a sub-directory when using Cypress v10+

from cypress-axe.

Comments (17)

9j0hn50n avatar 9j0hn50n commented on June 15, 2024 4

Lovely, thank you @dkryaklin!

I had tried ../../../node_modules/axe-core/axe.min.js but still got the mentioned error.

But ./node_modules/axe-core/axe.min.js did the trick.

Thanks again.

from cypress-axe.

ismay avatar ismay commented on June 15, 2024 1

If that fixes it for you it would be good to submit your patch as a PR.

from cypress-axe.

AriPerkkio avatar AriPerkkio commented on June 15, 2024 1

This seems to be due to require.resolve returning the path relative to the configuration file, whereas when running in Cypress Versions <10 require.resolve returns the path relative to the directory the initial command was invoked.

This all sounds exactly as the upstream issue: cypress-io/cypress#22689.
It makes no sense that all plugins in the ecosystem start patching themselves due to a bug in Cypress. Code coverage plugin is also seeing this: cypress-io/code-coverage#586.

Work-around for now: cypress-io/cypress#22689 (comment)

from cypress-axe.

ldeveber avatar ldeveber commented on June 15, 2024

We are also experiencing this issue

from cypress-axe.

jonkoops avatar jonkoops commented on June 15, 2024

This might be a duplicate of #84 and #73 if a preprocessor is configured.

from cypress-axe.

ldeveber avatar ldeveber commented on June 15, 2024

Fun fact: it only throws this error with cypress run for us. When using cypress open it seems to work fine, but we can't use that on the build servers :(

This is using the same config file for both commands.

from cypress-axe.

christianmartinnies avatar christianmartinnies commented on June 15, 2024

Facing the same issue, both when cypress runand cypress open.

Environment:
node 16.16.0
yarn 3.2.2

devDependencies:

"axe-core": "^4.4.3",
"cypress": "^10.5.0",
"cypress-axe": "^1.0.0"

from cypress-axe.

ldeveber avatar ldeveber commented on June 15, 2024

still does not work with cypress 10.6

from cypress-axe.

rdylanwalker avatar rdylanwalker commented on June 15, 2024

We are experiencing this issue on Cypress 10.3. We have a cypress.config.ts in project root. In <projectRoot>/cypress/configurations we have several custom configurations that extend the main configuration (via import). We override with the Cypress --config-file command line. When running tests with cypress-axe we experience the same error as above.

from cypress-axe.

ismay avatar ismay commented on June 15, 2024

I'm experiencing this as well, see CI logs here: https://github.com/ismay/portfolio/actions/runs/3067878096/jobs/4954656842#step:3:266

Looking at the resolved path, it seems like that path is correct relative to my config file (which is in ./.cypress): cy.readFile("../node_modules/axe-core/axe.min.js").

However, judging from the error it seems like cy.readFile("../node_modules/axe-core/axe.min.js") is resolving from the root of the project. So it ends up looking one level too high. It tries to find the file at /home/runner/work/portfolio/node_modules/axe-core/axe.min.js, but it should be looking for /home/runner/work/portfolio/portfolio/node_modules/axe-core/axe.min.js

The cypress docs mention that readFile resolves relative to the config file: https://docs.cypress.io/api/commands/readfile#Arguments, at least that's how I read it: A path to a file within the project root (the directory that contains the Cypress configuration file).. But that seems to be incorrect.

Judging from this issue the base path is actually hardcoded to the project root: cypress-io/cypress#2673 (see the related issues as well). So if that's correct, to fix this I think we have to make sure that we're resolving from the root of the project here: https://github.com/component-driven/cypress-axe/blob/master/src/index.ts#L26. See the docs here: https://nodejs.org/api/modules.html#requireresolverequest-options. Maybe https://github.com/sindresorhus/pkg-dir would be useful to resolve this.

from cypress-axe.

artvichi avatar artvichi commented on June 15, 2024

Here is a patch you can use to fix it with the patch-package
Updated just this file node_modules/cypress-axe/dist/index.js

 exports.injectAxe = function () {
-    var fileName = typeof (require === null || require === void 0 ? void 0 : require.resolve) === 'function'
-        ? require.resolve('axe-core/axe.min.js')
-        : 'node_modules/axe-core/axe.min.js';
+    var fileName = 'node_modules/axe-core/axe.min.js';
+
     cy.readFile(fileName).then(function (source) {
         return cy.window({ log: false }).then(function (window) {
             window.eval(source);

from cypress-axe.

Szymon-dziewonski avatar Szymon-dziewonski commented on June 15, 2024

@artvichi this won't work for workspaces when axe-core i installed as dependency of cypress-axe in workspace root, thats how its working on yarn.3

When install using workspace scoping packages:

- project `a` node_modules (here's always will be installed axe-core)
--- workspace `one` node_modules (cypress-axe when install with `yarn workspace @a/one add -D cypress-axe)

if install normally cd workspace; yarn add -D cypress-axe then package will be installed on root node_modules either way resolve of axe-core fail in

exports.injectAxe = function () {
    var fileName = typeof (require === null || require === void 0 ? void 0 : require.resolve) === 'function'
        ? require.resolve('axe-core/axe.min.js')
        : 'node_modules/axe-core/axe.min.js';
    cy.readFile(fileName).then(function (source) {
        return cy.window({ log: false }).then(function (window) {
            window.eval(source);
        });
    });
};

Problem only occurs on vite because webpack resolves properly with require.resolve even if package is installed in different node_modules

EDIT: plugin should be compiled to commonjs AND esm, also should use cypress task to read/reasolve package via createRequire so vite would be able to resolve package same way as webpack.

from cypress-axe.

dkryaklin avatar dkryaklin commented on June 15, 2024

This PR address this issue as well #150

from cypress-axe.

pdimova avatar pdimova commented on June 15, 2024

Any solution one year later?

Edit: To the person who marked this comment as spam - it is 2023 and this is still a persisting issue in this library.

from cypress-axe.

9j0hn50n avatar 9j0hn50n commented on June 15, 2024

Hi,

I'm still seeing issues with this after upgrading cypress-axe version to 1.3.0 and axe-core to 4.6.3.

This is what I see in terminal when running my tests:

AssertionError: Timed out retrying after 2500ms: `cy.readFile("../../../../node_modules/axe-core/axe.min.js")` failed because the file does not exist at the following path:

`/node_modules/axe-core/axe.min.js`

https://on.cypress.io/readfile
      at ../../../node_modules/cypress-axe/dist/index.js.exports.injectAxe (webpack:////Users/blah/blah/blah/node_modules/cypress-axe/dist/index.js:20:0)

The file does exist in /node_modules/axe-core/axe.min.js

I've made the following change in my custom Cypress function that tests a11y (as indicated here):

Cypress.Commands.add('checkAccessibility', () => {
  const terminalLog = (violations) => {
    cy.task(
      'log',
      `${violations.length} accessibility violation${violations.length === 1 ? '' : 's'
      } ${violations.length === 1 ? 'was' : 'were'} detected`,
    );
    const violationData = violations.map(
      ({
        id, impact, description, nodes,
      }) => ({
        id,
        impact,
        description,
        nodes: nodes.length,
      }),
    );
    cy.task('table', violationData);
  };
  cy.injectAxe({ axeCorePath: '../../../../node_modules/axe-core/axe.min.js'})
    .checkA11y(null, null, terminalLog, true, {
      runOnly: {
        values: ['wcag2aa'],
      },
    });
});

My config files are nested as follows:

my-app/
├─ node_modules/
├─ tests/
│  ├─ e2e/
│  │  ├─ configs/
│  │  │  ├─ common.config.js
│  │  │  ├─ ui.config.js
│  │  │  ├─ visual.config.js
│  │  ├─ tests/
│  │  │  ├─ ui/
│  │  │  ├─ visual/
├─ .gitignore
├─ package.json
├─ README.md

Can someone please advise?

Many thanks!

from cypress-axe.

dkryaklin avatar dkryaklin commented on June 15, 2024

@9j0hn50n Hi,

Not sure why you are using ../../../../node_modules/axe-core/axe.min.js path. I would suggest trying

../../../node_modules/axe-core/axe.min.js
or
./node_modules/axe-core/axe.min.js

from cypress-axe.

AshMcConnell avatar AshMcConnell commented on June 15, 2024

I have a similar, but slightly bizarre issue. When I try to injectAxe in an afterEach it works fine, however if the beforeEach or test fails it can't fine the file axe-core/axe.min.js. Does anyone know why that would be? Here is my checkAxe custom command: -

Cypress.Commands.add('checkAxe', (wait?: number) => {
    if (wait) {
        cy.wait(wait);
    }
    cy.injectAxe();
    cy.configureAxe({
        rules: exceptions.map(rule => ({id: rule, enabled: false})),
        standards: accessibilityStandards
    });

    cy.window({log: false})
        .then((win: any) => {

            const context = {
                include: [win.document],
                exclude: exclusions,
            };

            const rules = exceptions.reduce((result, exception) => {
                result[exception] = {enabled: false};
                return result;
            }, {});

            return cy.checkA11y(context, <any>{runOnly: accessibilityStandards, rules});
        });
});

image

from cypress-axe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.