Skip to content

Custom filename for inject axe #150

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -76,6 +76,21 @@ beforeEach(() => {
})
```

The `injectAxe` function receives an optional argument `injectOptions` of type `InjectOptions`.

This `injectOptions` object can have a property `axeCorePath` of type `string`, which allows the user to specify the file from which `axe-core` will be injected.

If `axeCorePath` is not provided, the function will try to resolve the path to `axe-core/axe.min.js` using the `require.resolve` function, if it is available.

If `require.resolve` is not available, the default path `node_modules/axe-core/axe.min.js` will be used.

```js
beforeEach(() => {
cy.visit('http://localhost:9000')
cy.injectAxe({ axeCorePath: '<path-to-axe-core>' })
})
```

### cy.configureAxe

#### Purpose
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -21,11 +21,16 @@ export interface Options extends axe.RunOptions {
includedImpacts?: string[];
}

export const injectAxe = () => {
export interface InjectOptions {
axeCorePath?: string;
}

export const injectAxe = (injectOptions?: InjectOptions) => {
const fileName =
typeof require?.resolve === 'function'
injectOptions?.axeCorePath ||
(typeof require?.resolve === 'function'
? require.resolve('axe-core/axe.min.js')
: 'node_modules/axe-core/axe.min.js';
: 'node_modules/axe-core/axe.min.js');
cy.readFile<string>(fileName).then((source) =>
cy.window({ log: false }).then((window) => {
window.eval(source);