Skip to content

Commit

Permalink
feat: add ability to filter files from automated checks (#613)
Browse files Browse the repository at this point in the history
* feat: add ability to filter files from automated checks st framework level

* test: added few tests for helper.js

* test: added tests for helper.ts

* fix: concat the useFilesToBeExempted() and autoCheckOpts.filesFilter

* fix: modified a test in automatic.test.ts

* fix: updated setup.ts

* fix: changed helpers.ts to fix the lint issue

* Update helpers.ts

* Update automatic.test.ts

* Update setup.ts

---------

Co-authored-by: Navateja Alagam <navateja215@gmail.com>
  • Loading branch information
jaig-0911 and navateja-alagam authored Nov 29, 2023
1 parent b444195 commit a0f52d8
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 3 deletions.
69 changes: 69 additions & 0 deletions packages/common/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2020, 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 { useFilesToBeExempted, log } from '../src/helpers';

describe('Your Module', () => {
afterEach(() => {
jest.resetAllMocks();
});

it('should log messages when SA11Y_DEBUG is set', () => {
const originalEnv = process.env.SA11Y_DEBUG;
process.env.SA11Y_DEBUG = 'true';

const consoleSpy = jest.spyOn(console, 'log');
log('Test Log Message');

expect(consoleSpy).toHaveBeenCalledWith('♿[Sa11y]', 'Test Log Message');

process.env.SA11Y_DEBUG = originalEnv; // Restore original environment variable
});

it('should return an empty array if packageName is not set', () => {
const result = useFilesToBeExempted();

expect(result).toEqual([]);
});

it('should return an empty array if SA11Y_AUTO_FILTER_LIST_PACKAGE_REQUIREMENT is not set', () => {
process.env.SA11Y_AUTO_FILTER_LIST_PACKAGE_NAME = 'somePackage';
process.env.SA11Y_AUTO_FILTER_LIST_PACKAGE_REQUIREMENT = '';

const result = useFilesToBeExempted();

expect(result).toEqual([]);

// Cleanup
delete process.env.SA11Y_AUTO_FILTER_LIST_PACKAGE_REQUIREMENT;
delete process.env.SA11Y_AUTO_FILTER_LIST_PACKAGE_NAME;
});

it('should use the package if packageName is set', () => {
const packageName = '../testMocks/packageTestHelper.ts';
process.env.SA11Y_AUTO_FILTER_LIST_PACKAGE_NAME = packageName;

const result = useFilesToBeExempted();

expect(result).toEqual(['file1', 'file2']);

// Cleanup
delete process.env.SA11Y_AUTO_FILTER_LIST_PACKAGE_NAME;
});

it('no package found if packageName is set', () => {
const packageName = '../testMocks/packageTestHelperWrong.ts';

process.env.SA11Y_AUTO_FILTER_LIST_PACKAGE_NAME = packageName;

const result = useFilesToBeExempted();

expect(result).toEqual([]);
// Cleanup
delete process.env.SA11Y_AUTO_FILTER_LIST_PACKAGE_NAME;
});
});
18 changes: 18 additions & 0 deletions packages/common/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,26 @@
* Convenience wrapper to prefix a standard header for console log messages.
* Logging is enabled only when environment variable `SA11Y_DEBUG` is set.
*/

export function log(...args: unknown[]): void {
// Probably not worth it to mock and test console logging for this helper util
/* istanbul ignore next */
if (process.env.SA11Y_DEBUG) console.log('♿[Sa11y]', ...args);
}

export function useFilesToBeExempted(): string[] {
const packageName: string =
process.env.SA11Y_AUTO_FILTER_LIST_PACKAGE_NAME ?? 'sa11y-jest-automated-check-file-exclusion';
let getFilesToBeExempted: () => string[];
if (packageName !== '') {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
getFilesToBeExempted = require(packageName) as () => string[];
const filesToBeExempted = getFilesToBeExempted();
return filesToBeExempted;
} catch (error) {
console.log('Package not found : ', packageName);
}
}
return [];
}
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 } from './helpers';
export { log, useFilesToBeExempted } from './helpers';
9 changes: 9 additions & 0 deletions packages/common/testMocks/packageTestHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2023, 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
*/
const getFilesToBeExempted = () => ['file1', 'file2'];

module.exports = getFilesToBeExempted;
4 changes: 3 additions & 1 deletion packages/jest/__tests__/automatic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
domWithNoA11yIssuesChildCount,
domWithDescendancyA11yIssues,
} from '@sa11y/test-utils';
import * as Sa11yCommon from '@sa11y/common';
import { expect, jest } from '@jest/globals';

describe('automatic checks registration', () => {
Expand Down Expand Up @@ -90,6 +91,7 @@ describe('automatic checks registration', () => {
it('should set run only files option when specified', () => {
const testFiles = 'foo,bar';
process.env.SA11Y_AUTO_FILTER = testFiles;
jest.spyOn(Sa11yCommon, 'useFilesToBeExempted').mockReturnValueOnce(['file1', 'file2']);
setup();
expect(registerAutomaticMock).toHaveBeenCalledWith({
// TODO (debug): Values seem to be carrying over from previous test
Expand All @@ -98,7 +100,7 @@ describe('automatic checks registration', () => {
runAfterEach: true,
cleanupAfterEach: true,
consolidateResults: true,
filesFilter: testFiles.split(','),
filesFilter: ['foo', 'bar', 'file1', 'file2'],
});
});
});
Expand Down
6 changes: 5 additions & 1 deletion packages/jest/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { toBeAccessible } from './matcher';
import { A11yConfig } from '@sa11y/common';
import { A11yConfig, useFilesToBeExempted } from '@sa11y/common';
import {
AutoCheckOpts,
registerSa11yAutomaticChecks,
Expand Down Expand Up @@ -100,6 +100,10 @@ export function setup(opts: Sa11yOpts = defaultSa11yOpts): void {
autoCheckOpts.cleanupAfterEach ||= !!process.env.SA11Y_CLEANUP;
if (process.env.SA11Y_AUTO_FILTER?.trim().length)
autoCheckOpts.filesFilter ||= process.env.SA11Y_AUTO_FILTER.split(',');
const exemptedFiles = useFilesToBeExempted();
if (exemptedFiles.length !== 0) {
autoCheckOpts.filesFilter = (autoCheckOpts.filesFilter ?? []).concat(exemptedFiles);
}
registerSa11yAutomaticChecks(autoCheckOpts);
}

Expand Down

0 comments on commit a0f52d8

Please sign in to comment.