Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: changed fs/promises to fs in helpers.js #639

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/common/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,22 @@ describe('Your Module', () => {
delete process.env.SA11Y_AUTO_FILTER_LIST_PACKAGE_NAME;
});

it('should return empty array if SA11Y_CUSTOM_RULES env variable is not set', async () => {
it('should return empty array if SA11Y_CUSTOM_RULES env variable is not set', () => {
process.env.SA11Y_CUSTOM_RULES = '';
const result = await useCustomRules();
const result = useCustomRules();
expect(result).toEqual([]);
});

it('should return empty array if reading file encounters an error', async () => {
it('should return empty array if reading file encounters an error', () => {
process.env.SA11Y_CUSTOM_RULES = 'packages/common/testMocks/WrongFile.json';
const result = await useCustomRules();
const result = useCustomRules();
expect(result).toEqual([]);
});

it('should return rules array if file is read successfully', async () => {
it('should return rules array if file is read successfully', () => {
process.env.SA11Y_CUSTOM_RULES = 'packages/common/testMocks/sa11y-custom-rules.json';
const mockRules = ['rule1', 'rule2'];
const result = await useCustomRules();
const result = useCustomRules();
expect(result).toEqual(mockRules);
});
});
6 changes: 3 additions & 3 deletions packages/common/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Logging is enabled only when environment variable `SA11Y_DEBUG` is set.
*/

import * as fs from 'fs/promises';
import * as fs from 'fs';
export function log(...args: unknown[]): void {
// Probably not worth it to mock and test console logging for this helper util
/* istanbul ignore next */
Expand All @@ -34,12 +34,12 @@ export function useFilesToBeExempted(): string[] {
return [];
}

export async function useCustomRules(): Promise<string[]> {
export function useCustomRules(): string[] {
const filePath = process.env.SA11Y_CUSTOM_RULES ?? '';
if (filePath !== '') {
try {
// Read the file asynchronously
const data = await fs.readFile(filePath, 'utf-8');
const data = fs.readFileSync(filePath, 'utf-8');
const { rules } = JSON.parse(data) as { rules: string[] };
// Access the rules array
return rules;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest/src/automatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export async function automaticCheck(opts: AutoCheckOpts = defaultAutoCheckOpts)
// Create a DOM walker filtering only elements (skipping text, comment nodes etc)
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_ELEMENT);
let currNode = walker.firstChild();
const customRules = await useCustomRules();
const customRules = useCustomRules();
try {
while (currNode !== null) {
// TODO (spike): Use a logger lib with log levels selectable at runtime
Expand Down
Loading