From 6cd04b2964c35d10b0b38797e43390ed21dbbb85 Mon Sep 17 00:00:00 2001 From: jaig-0911 <63843872+jaig-0911@users.noreply.github.com> Date: Wed, 29 May 2024 19:21:24 +0530 Subject: [PATCH] fix: axe-core dependency in preset-rule (#689) * fix: axe-core dependency in preset-rule * fix: changed customRules implementation --------- Co-authored-by: Navateja Alagam --- packages/browser-lib/src/index.ts | 5 +-- packages/common/__tests__/helpers.test.ts | 16 +++++++++- packages/common/src/helpers.ts | 25 +++++++++++++++ packages/common/src/index.ts | 2 +- packages/jest/__tests__/automatic.test.ts | 1 - packages/jest/src/setup.ts | 6 ++-- .../__tests__/registerCustomRules.test.ts | 32 ------------------- packages/preset-rules/src/customRules.ts | 30 ----------------- packages/preset-rules/src/index.ts | 5 ++- 9 files changed, 51 insertions(+), 71 deletions(-) delete mode 100644 packages/preset-rules/__tests__/registerCustomRules.test.ts delete mode 100644 packages/preset-rules/src/customRules.ts diff --git a/packages/browser-lib/src/index.ts b/packages/browser-lib/src/index.ts index bd450f44..bb20e8c5 100644 --- a/packages/browser-lib/src/index.ts +++ b/packages/browser-lib/src/index.ts @@ -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'; @@ -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); diff --git a/packages/common/__tests__/helpers.test.ts b/packages/common/__tests__/helpers.test.ts index 0ffe1ac5..b781e6ba 100644 --- a/packages/common/__tests__/helpers.test.ts +++ b/packages/common/__tests__/helpers.test.ts @@ -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(() => { @@ -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; + 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], + }); + }); }); diff --git a/packages/common/src/helpers.ts b/packages/common/src/helpers.ts index d0c7a251..cb72bc36 100644 --- a/packages/common/src/helpers.ts +++ b/packages/common/src/helpers.ts @@ -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 { @@ -68,3 +69,27 @@ export const processFiles = ( } }); }; + +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); +}; diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index 632676f4..93966610 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -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'; diff --git a/packages/jest/__tests__/automatic.test.ts b/packages/jest/__tests__/automatic.test.ts index 6502750c..b7f93a8c 100644 --- a/packages/jest/__tests__/automatic.test.ts +++ b/packages/jest/__tests__/automatic.test.ts @@ -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; diff --git a/packages/jest/src/setup.ts b/packages/jest/src/setup.ts index c08fb1f9..557a09b5 100644 --- a/packages/jest/src/setup.ts +++ b/packages/jest/src/setup.ts @@ -6,7 +6,7 @@ */ import { toBeAccessible } from './matcher'; -import { A11yConfig, useFilesToBeExempted } from '@sa11y/common'; +import { A11yConfig, useFilesToBeExempted, registerCustomRules } from '@sa11y/common'; import { AutoCheckOpts, registerSa11yAutomaticChecks, @@ -14,7 +14,7 @@ import { 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 @@ -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; diff --git a/packages/preset-rules/__tests__/registerCustomRules.test.ts b/packages/preset-rules/__tests__/registerCustomRules.test.ts deleted file mode 100644 index a6e298e6..00000000 --- a/packages/preset-rules/__tests__/registerCustomRules.test.ts +++ /dev/null @@ -1,32 +0,0 @@ -/* eslint-disable eslint-comments/disable-enable-pair */ -/* eslint-disable @typescript-eslint/no-unsafe-assignment */ -/* - * Copyright (c) 2024, salesforce.com, inc. - * All rights reserved. - * SPDX-License-Identifier: BSD-3-Clause - * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause - */ -import axe from 'axe-core'; -import { registerCustomRules } from '../src/customRules'; -import checkData from '../src/custom-rules/checks'; -import rulesData from '../src/custom-rules/rules'; -import changes from '../src/custom-rules/changes'; - -jest.mock('axe-core'); - -describe('registerCustomRules', () => { - const mockConfigure = axe.configure as jest.MockedFunction; - - afterEach(() => { - jest.clearAllMocks(); - }); - - it('should configure axe with new checks and rules', () => { - registerCustomRules(); - const changedRules = changes?.rules ?? []; - expect(mockConfigure).toHaveBeenCalledWith({ - rules: [...changedRules, ...rulesData], - checks: [...checkData], - }); - }); -}); diff --git a/packages/preset-rules/src/customRules.ts b/packages/preset-rules/src/customRules.ts deleted file mode 100644 index 563f2f8f..00000000 --- a/packages/preset-rules/src/customRules.ts +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2024, salesforce.com, inc. - * All rights reserved. - * SPDX-License-Identifier: BSD-3-Clause - * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause - */ -import axe from 'axe-core'; -import checkData from './custom-rules/checks'; -import rulesData from './custom-rules/rules'; -import changes from './custom-rules/changes'; - -export function registerCustomRules(): void { - const newChecks: axe.Check[] = []; - const newRules: axe.Rule[] = []; - - // Read and parse existing rule changes - const { rules } = changes; - const newRulesData = rulesData; - const newChecksData = checkData; - - 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); -} diff --git a/packages/preset-rules/src/index.ts b/packages/preset-rules/src/index.ts index 5e2c927b..723756c6 100644 --- a/packages/preset-rules/src/index.ts +++ b/packages/preset-rules/src/index.ts @@ -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 };