Skip to content

Commit

Permalink
feat: add options for injectAxe
Browse files Browse the repository at this point in the history
  • Loading branch information
dkryaklin committed Jan 12, 2023
1 parent b6391e6 commit 3f50bdd
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 3f50bdd

Please sign in to comment.