diff --git a/README.md b/README.md index 27e0af1..c54a1b6 100644 --- a/README.md +++ b/README.md @@ -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: '' }) +}) +``` + ### cy.configureAxe #### Purpose diff --git a/src/index.ts b/src/index.ts index 89d9981..6fbd163 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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(fileName).then((source) => cy.window({ log: false }).then((window) => { window.eval(source);