Skip to content

Commit

Permalink
fix: axe-core dependency in preset-rule (#689)
Browse files Browse the repository at this point in the history
* fix: axe-core dependency in preset-rule

* fix: changed customRules implementation

---------

Co-authored-by: Navateja Alagam <navateja215@gmail.com>
  • Loading branch information
jaig-0911 and navateja-alagam committed May 29, 2024
1 parent 353ccd0 commit 6cd04b2
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 71 deletions.
5 changes: 3 additions & 2 deletions packages/browser-lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import * as axe from 'axe-core';
import { exceptionListFilter, appendWcag } from '@sa11y/format';
import { defaultRuleset, registerCustomRules } from '@sa11y/preset-rules';
import { registerCustomRules } from '@sa11y/common';
import { defaultRuleset, changesData, rulesData, checkData } from '@sa11y/preset-rules';
export { base, extended, full } from '@sa11y/preset-rules';
export const namespace = 'sa11y';

Expand All @@ -32,7 +33,7 @@ export async function checkAccessibility(
// which could then result in getting rid of the rollup typescript plugins.

// To register custom rules
registerCustomRules();
registerCustomRules(changesData, rulesData, checkData);

const results = await axe.run(scope || document, rules);
const filteredResults = exceptionListFilter(results.violations, exceptionList);
Expand Down
16 changes: 15 additions & 1 deletion packages/common/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/

import path from 'path';
import { useFilesToBeExempted, log, useCustomRules, processFiles } from '../src/helpers';
import { useFilesToBeExempted, log, useCustomRules, processFiles, registerCustomRules } from '../src/helpers';
import axe from 'axe-core';
jest.mock('axe-core');

describe('Your Module', () => {
afterEach(() => {
Expand Down Expand Up @@ -93,4 +95,16 @@ describe('Your Module', () => {
processFiles<{ key: string }>(directoryPath, targetArray, '.json', JSON.parse);
expect(targetArray).toEqual([{ key: 'value' }]);
});

it('register custom Rules', () => {
const mockConfigure = axe.configure as jest.MockedFunction<typeof axe.configure>;

Check warning on line 100 in packages/common/__tests__/helpers.test.ts

View workflow job for this annotation

GitHub Actions / Lint, Build, Test - Node 16.x on ubuntu-latest

Caution: `axe` also has a named export `configure`. Check if you meant to write `import {configure} from 'axe-core'` instead

Check warning on line 100 in packages/common/__tests__/helpers.test.ts

View workflow job for this annotation

GitHub Actions / Lint, Build, Test - Node 18.x on ubuntu-latest

Caution: `axe` also has a named export `configure`. Check if you meant to write `import {configure} from 'axe-core'` instead

Check warning on line 100 in packages/common/__tests__/helpers.test.ts

View workflow job for this annotation

GitHub Actions / Lint, Build, Test - Node 20.x on ubuntu-latest

Caution: `axe` also has a named export `configure`. Check if you meant to write `import {configure} from 'axe-core'` instead
const mockRules = [{ id: 'rule1' }] as axe.Rule[];
const mockChecks = [{ id: 'check1' }] as axe.Check[];
const mockChanges = { rules: [{ id: 'rule2' }] } as { rules: axe.Rule[] };
registerCustomRules(mockChanges, mockRules, mockChecks);
expect(mockConfigure).toHaveBeenCalledWith({
rules: [...mockChanges.rules, ...mockRules],
checks: [...mockChecks],
});
});
});
25 changes: 25 additions & 0 deletions packages/common/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
const sa11yAutoFilterListDefaultPackageName = 'sa11y-jest-automated-check-file-exclusion';

import axe from 'axe-core';
import * as fs from 'fs';
import path from 'path';
export function log(...args: unknown[]): void {
Expand Down Expand Up @@ -68,3 +69,27 @@ export const processFiles = <T>(
}
});
};

export const registerCustomRules = (
changesData: { rules: axe.Rule[] },
rulesData: axe.Rule[],
checksData: axe.Check[]
): void => {
const newChecks: axe.Check[] = [];
const newRules: axe.Rule[] = [];

// Read and parse existing rule changes
const { rules } = changesData;
const newRulesData = rulesData;
const newChecksData = checksData;

if (rules && Array.isArray(rules)) {
newRules.push(...rules);
}
newRules.push(...newRulesData);
newChecks.push(...newChecksData);

// Configure axe with the new checks and rules
const spec: axe.Spec = { rules: newRules, checks: newChecks };
axe.configure(spec);

Check warning on line 94 in packages/common/src/helpers.ts

View workflow job for this annotation

GitHub Actions / Lint, Build, Test - Node 16.x on ubuntu-latest

Caution: `axe` also has a named export `configure`. Check if you meant to write `import {configure} from 'axe-core'` instead

Check warning on line 94 in packages/common/src/helpers.ts

View workflow job for this annotation

GitHub Actions / Lint, Build, Test - Node 18.x on ubuntu-latest

Caution: `axe` also has a named export `configure`. Check if you meant to write `import {configure} from 'axe-core'` instead

Check warning on line 94 in packages/common/src/helpers.ts

View workflow job for this annotation

GitHub Actions / Lint, Build, Test - Node 20.x on ubuntu-latest

Caution: `axe` also has a named export `configure`. Check if you meant to write `import {configure} from 'axe-core'` instead
};
2 changes: 1 addition & 1 deletion packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
export { A11yConfig, AxeResults, axeRuntimeExceptionMsgPrefix, axeVersion, getAxeRules, getViolations } from './axe';
export { WdioAssertFunction, WdioOptions } from './wdio';
export { errMsgHeader, ExceptionList } from './format';
export { log, useFilesToBeExempted, useCustomRules, processFiles } from './helpers';
export { log, useFilesToBeExempted, useCustomRules, processFiles, registerCustomRules } from './helpers';
1 change: 0 additions & 1 deletion packages/jest/__tests__/automatic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
} from '@sa11y/test-utils';
import * as Sa11yCommon from '@sa11y/common';
import { expect, jest } from '@jest/globals';
import { registerCustomRules } from '@sa11y/preset-rules';

describe('automatic checks registration', () => {
const prevEnv = process.env;
Expand Down
6 changes: 3 additions & 3 deletions packages/jest/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
*/

import { toBeAccessible } from './matcher';
import { A11yConfig, useFilesToBeExempted } from '@sa11y/common';
import { A11yConfig, useFilesToBeExempted, registerCustomRules } from '@sa11y/common';
import {
AutoCheckOpts,
registerSa11yAutomaticChecks,
getOriginalDocumentBodyHtml,
setOriginalDocumentBodyHtml,
} from './automatic';
import { expect } from '@jest/globals';
import { registerCustomRules } from '@sa11y/preset-rules';
import { changesData, rulesData, checkData } from '@sa11y/preset-rules';

export const disabledRules = [
// Descendancy checks that would fail at unit/component level, but pass at page level
Expand Down Expand Up @@ -93,7 +93,7 @@ export function setup(opts: Sa11yOpts = defaultSa11yOpts): void {
}
}
registerSa11yMatcher();
registerCustomRules();
registerCustomRules(changesData, rulesData, checkData);
// Set defaults from env vars
const autoCheckOpts = opts.autoCheckOpts;
autoCheckOpts.runAfterEach ||= !!process.env.SA11Y_AUTO;
Expand Down
32 changes: 0 additions & 32 deletions packages/preset-rules/__tests__/registerCustomRules.test.ts

This file was deleted.

30 changes: 0 additions & 30 deletions packages/preset-rules/src/customRules.ts

This file was deleted.

5 changes: 4 additions & 1 deletion packages/preset-rules/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ export { extended } from './extended';
export { base } from './base';
export { full, excludedRules } from './full';
export { WcagMetadata } from './wcag';
export { registerCustomRules } from './customRules';
import changesData from './custom-rules/changes';
import rulesData from './custom-rules/rules';
import checkData from './custom-rules/checks';
export { changesData, rulesData, checkData };

0 comments on commit 6cd04b2

Please sign in to comment.