Skip to content

Commit

Permalink
fix: changed fs/promises to fs (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaig-0911 committed Feb 2, 2024
1 parent 73d09cc commit 53758a5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
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

0 comments on commit 53758a5

Please sign in to comment.