Skip to content

Commit

Permalink
feat: Allow to use custom axe-core location (component-driven#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkryaklin authored Jan 17, 2023
1 parent 75cac7b commit 91c72d2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 91c72d2

Please sign in to comment.