Skip to content

Commit

Permalink
fix(eslint,prettier): new configs
Browse files Browse the repository at this point in the history
  • Loading branch information
forsti0506 committed Sep 4, 2021
1 parent 2d8f418 commit cf50e00
Show file tree
Hide file tree
Showing 27 changed files with 478 additions and 491 deletions.
10 changes: 4 additions & 6 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
{
"parser": "@typescript-eslint/parser", // Specifies the ESLint parser
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended", // Uses the recommended rules from the @typescript-eslint/eslint-plugin
"prettier" // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettie
"prettier", // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier,
"plugin:prettier/recommended"
],
"parserOptions": {
"ecmaVersion": 2020, // Allows for the parsing of modern ECMAScript features
"sourceType": "module" // Allows for the use of imports
},
"settings": {
"angular": {
"version": "detect" // Tells eslint-plugin-react to automatically detect the version of React to use
}
},
"plugins": ["@typescript-eslint"],
"root": true,
"env": {
"node": true,
Expand Down
7 changes: 4 additions & 3 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"semi": true,
"trailingComma": "none",
"trailingComma": "all",
"singleQuote": true,
"printWidth": 90,
"tabWidth": 4
"printWidth": 120,
"tabWidth": 4,
"parser": "typescript"
}
43 changes: 20 additions & 23 deletions bin/a11y-sitechecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { saveResultsToFile } from '../lib/utils/save-results-to-file';
import { setupAxeConfig, setupConfig } from '../lib/utils/setup-config';
import pkg from '../package.json';


program
.version(pkg.version)
.usage('[options] <paths>')
Expand All @@ -23,31 +22,29 @@ program
.parse(process.argv);

(async (): Promise<void> => {
const config = setupConfig(program.opts());
const axeConfig = setupAxeConfig(config);
let retCode = 0;
try {
const results = await entry(config, axeConfig, !program.opts().json);

for (const [i, sitecheckerResult] of results.entries()) {
const config = setupConfig(program.opts());
const axeConfig = setupAxeConfig(config);
let retCode = 0;
try {
const results = await entry(config, axeConfig, !program.opts().json);

await saveResultsToFile(config, sitecheckerResult, i);
for (const [i, sitecheckerResult] of results.entries()) {
await saveResultsToFile(config, sitecheckerResult, i);

if (sitecheckerResult.violations.length >= config.threshold) {
retCode = 2;
}
}
} catch (e: any) {
if (e.message.includes('Threshold not met')) {
if (sitecheckerResult.violations.length >= config.threshold) {
retCode = 2;
} else if (e.message.includes('ERR_NAME_NOT_RESOLVED')) {
retCode = 3;
} else {
retCode = 1;
}

error(e);
}
process.exit(retCode);

} catch (e: any) {
if (e.message.includes('Threshold not met')) {
retCode = 2;
} else if (e.message.includes('ERR_NAME_NOT_RESOLVED')) {
retCode = 3;
} else {
retCode = 1;
}

error(e);
}
process.exit(retCode);
})();
71 changes: 43 additions & 28 deletions lib/a11y-sitechecker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,81 +11,96 @@ describe('a11y-sitechecker', () => {
const mockConfig: Partial<Config> = {};
expect(async () => await entry(mockConfig as Config, {})).toThrowError;
});

test('1 result with name set', async () => {
jest.spyOn(setupConfigMock, 'prepareWorkspace').mockImplementation(() => void 0);
jest.spyOn(analyzeSite, 'analyzeSite').mockImplementation(() => new Promise((resolve) => resolve([])));
jest.spyOn(mergeResults, 'mergeResults').mockImplementation(() => void 0)
jest.spyOn(mergeResults, 'mergeResults').mockImplementation(() => void 0);
const mockConfig: Partial<Config> = {};
mockConfig.name = 'Testinger';
mockConfig.viewports = [{width: 100, height: 200}];
mockConfig.viewports = [{ width: 100, height: 200 }];
mockConfig.json = false;
const results = await entry(mockConfig as Config, {});
expect(results[0].name).toBe('Testinger');
expect(results.length).toBe(1);
});
});

test('Threshold not met', () => {
const mockedResults: Partial<ResultByUrl>[] = [];
const violation: Partial<FullCheckerSingleResult> = {};
jest.spyOn(setupConfigMock, 'prepareWorkspace').mockImplementation(() => void 0);
jest.spyOn(analyzeSite, 'analyzeSite').mockImplementation(() => new Promise((resolve) => resolve(mockedResults as ResultByUrl[])));
jest.spyOn(mergeResults, 'mergeResults').mockImplementation((result, report) => {
report.violations = [violation as FullCheckerSingleResult]});
jest.spyOn(analyzeSite, 'analyzeSite').mockImplementation(
() => new Promise((resolve) => resolve(mockedResults as ResultByUrl[])),
);
jest.spyOn(mergeResults, 'mergeResults').mockImplementation((result, report) => {
report.violations = [violation as FullCheckerSingleResult];
});
const mockConfig: Partial<Config> = {};
mockConfig.name = 'Testinger';
mockConfig.viewports = [{width: 100, height: 200}];
mockConfig.viewports = [{ width: 100, height: 200 }];
mockConfig.json = false;
mockConfig.threshold = 0;
mockConfig.login = undefined;

return entry(mockConfig as Config, {}).then(e => expect(e.length).toBe(10)).catch(e => expect(e.message).toContain('Threshold not met'));
});

return entry(mockConfig as Config, {})
.then((e) => expect(e.length).toBe(10))
.catch((e) => expect(e.message).toContain('Threshold not met'));
});
test('Threshold met', () => {
const mockedResults: Partial<ResultByUrl>[] = [];
const violation: Partial<FullCheckerSingleResult> = {};
jest.spyOn(setupConfigMock, 'prepareWorkspace').mockImplementation(() => void 0);
jest.spyOn(analyzeSite, 'analyzeSite').mockImplementation(() => new Promise((resolve) => resolve(mockedResults as ResultByUrl[])));
jest.spyOn(mergeResults, 'mergeResults').mockImplementation((result, report) => {report.violations = [violation as FullCheckerSingleResult]});
jest.spyOn(analyzeSite, 'analyzeSite').mockImplementation(
() => new Promise((resolve) => resolve(mockedResults as ResultByUrl[])),
);
jest.spyOn(mergeResults, 'mergeResults').mockImplementation((result, report) => {
report.violations = [violation as FullCheckerSingleResult];
});
const mockConfig: Partial<Config> = {};
mockConfig.name = 'Testinger';
mockConfig.viewports = [{width: 100, height: 200}];
mockConfig.viewports = [{ width: 100, height: 200 }];
mockConfig.json = false;
mockConfig.threshold = 2;
mockConfig.login = undefined;
return entry(mockConfig as Config, {}).then(e => expect(e.length).toBe(1));
});

return entry(mockConfig as Config, {}).then((e) => expect(e.length).toBe(1));
});

test('Write to JSON File', () => {
const mockedResults: Partial<ResultByUrl>[] = [];
const violation: Partial<FullCheckerSingleResult> = {};
jest.spyOn(setupConfigMock, 'prepareWorkspace').mockImplementation(() => void 0);
jest.spyOn(analyzeSite, 'analyzeSite').mockImplementation(() => new Promise((resolve) => resolve(mockedResults as ResultByUrl[])));
jest.spyOn(mergeResults, 'mergeResults').mockImplementation((result, report) => {report.violations = [violation as FullCheckerSingleResult]});
jest.spyOn(analyzeSite, 'analyzeSite').mockImplementation(
() => new Promise((resolve) => resolve(mockedResults as ResultByUrl[])),
);
jest.spyOn(mergeResults, 'mergeResults').mockImplementation((result, report) => {
report.violations = [violation as FullCheckerSingleResult];
});
const writeToJson = jest.spyOn(helpers, 'writeToJsonFile').mockImplementation();
const mockConfig: Partial<Config> = {};
mockConfig.name = 'Testinger';
mockConfig.viewports = [{width: 100, height: 200}];
mockConfig.viewports = [{ width: 100, height: 200 }];
mockConfig.json = true;
mockConfig.threshold = 2;
mockConfig.login = undefined;
return entry(mockConfig as Config, {}).then(e => {

return entry(mockConfig as Config, {}).then((e) => {
expect(e.length).toBe(1);
expect(writeToJson).toBeCalledTimes(1);
});
});
});

test('throw error in entry', () => {
const mockConfig: Partial<Config> = {};
mockConfig.name = 'Testinger';
mockConfig.json = true;
mockConfig.threshold = 2;
mockConfig.login = undefined;

return entry(mockConfig as Config, {}).then(e => {
expect(e.length).toBe(10);
}).catch(e => expect(e.message).toContain('Cannot read property'));
});

return entry(mockConfig as Config, {})
.then((e) => {
expect(e.length).toBe(10);
})
.catch((e) => expect(e.message).toContain('Cannot read property'));
});
});
15 changes: 7 additions & 8 deletions lib/a11y-sitechecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import { executeLogin } from './utils/login';
import { mergeResults } from './utils/result-functions';
import { prepareWorkspace } from './utils/setup-config';


export async function entry(
config: Config,
axeSpecs: Spec,
onlyReturn?: boolean,
): Promise<A11ySitecheckerResult[]> {
export async function entry(config: Config, axeSpecs: Spec, onlyReturn?: boolean): Promise<A11ySitecheckerResult[]> {
try {
prepareWorkspace(config);
log(
Expand Down Expand Up @@ -47,8 +42,12 @@ async function checkSite(
width: vp.width,
height: vp.height,
});
await executeLogin( page, config);
const usedLocale = config.axeConfig?.locale ? config.axeConfig?.locale : (config.axeConfig?.localePath ? config.axeConfig?.localePath : 'en')
await executeLogin(page, config);
const usedLocale = config.axeConfig?.locale
? config.axeConfig?.locale
: config.axeConfig?.localePath
? config.axeConfig?.localePath
: 'en';

const result: A11ySitecheckerResult = {
testEngine: undefined,
Expand Down
2 changes: 0 additions & 2 deletions lib/models/analyzed-site.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

export interface AnalyzedSite {
_id: string;
url: string;
filesByDate: FilesByDate[];
}


export interface FilesByDate {
date: Date;
files: string[];
Expand Down
2 changes: 1 addition & 1 deletion lib/models/small-ones.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface ListenerObject {
listeners: Event[];
}
export interface SpanElement {
elementId: string;
elementId: string;
spanId: string;
visible: boolean;
}
8 changes: 7 additions & 1 deletion lib/utils/UniqueSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ function uniqueQuery(aSel: string[], dom: JSDOM.JSDOM): boolean {
return dom.window.document.querySelectorAll(aSel.join('>')).length === 1;
}

function getSelector(aSel: string[], el: Element, sSel: string | undefined, aAttr: string[], dom: JSDOM.JSDOM): boolean {
function getSelector(
aSel: string[],
el: Element,
sSel: string | undefined,
aAttr: string[],
dom: JSDOM.JSDOM,
): boolean {
// 1. Check ID first
// NOTE: ID must be unique amongst all IDs in an HTML5 document.
// https://www.w3.org/TR/html5/dom.html#the-id-attribute
Expand Down
17 changes: 5 additions & 12 deletions lib/utils/accept-cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ export async function acceptCookieConsent(page: Page, config: Config): Promise<v
const frames = page.frames();
let cookieId;
for (const frame of frames) {
debug(
config.debugMode,
'Check frame ' + (frame.name() || frame.url()) + ' for consent button'
);
debug(config.debugMode, 'Check frame ' + (frame.name() || frame.url()) + ' for consent button');
if (config.cookieSelector && config.cookieText) {
cookieId = await frame.evaluate(
(cookieSelector, cookieText, count) => {
const elements = document.querySelectorAll(cookieSelector);
const cookieElements = Array.from(elements).filter((d) =>
RegExp(cookieText, 'i').test(d.textContent.trim())
RegExp(cookieText, 'i').test(d.textContent.trim()),
);
console.log(JSON.stringify(cookieElements.length));
if (cookieElements && cookieElements.length > 0) {
Expand All @@ -34,7 +31,7 @@ export async function acceptCookieConsent(page: Page, config: Config): Promise<v
},
config.cookieSelector,
config.cookieText,
count
count,
);
}

Expand All @@ -44,7 +41,7 @@ export async function acceptCookieConsent(page: Page, config: Config): Promise<v
config.imagesPath,
'consent_screen_' + count + '.png',
config.saveImages,
config.debugMode
config.debugMode,
);
await frame.evaluate((cookieId) => {
const element = document.getElementById(cookieId);
Expand All @@ -53,11 +50,7 @@ export async function acceptCookieConsent(page: Page, config: Config): Promise<v
count++;
break;
} else {
debug(
config.debugMode,
'No cookie element found. Iframe Name or Url: ' +
(frame.name() || frame.url())
);
debug(config.debugMode, 'No cookie element found. Iframe Name or Url: ' + (frame.name() || frame.url()));
}
}
}

0 comments on commit cf50e00

Please sign in to comment.