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

Adjusting time of data gathering in middleware reporter #268

Merged
merged 4 commits into from
Jun 11, 2021
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
43 changes: 36 additions & 7 deletions addon-test-support/setup-middleware-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@ import { AxeResults, Result } from 'axe-core';
import { setCustomReporter } from './reporter';
import { DEBUG } from '@glimmer/env';

export interface TestMetadata {
testName?: string;
setupTypes: string[];
usedHelpers: string[];
[key: string]: any;

readonly isRendering: boolean;
readonly isApplication: boolean;
}

interface AxeTestResult {
moduleName: string;
testName: string;
testMetaData: TestMetadata;
urls: string[] | Set<string>;
routes: string[] | Set<string>;
helpers: string[];
Expand All @@ -23,9 +34,16 @@ export const TEST_SUITE_RESULTS: AxeTestResult[] = [];

let currentTestResult: AxeTestResult | undefined = undefined;
let currentViolationsMap: Map<string, Result> | undefined = undefined;
let currentUrls = new Set<string>();
let currentRouteNames = new Set<string>();
let currentUrls: Set<string> | undefined;
let currentRouteNames: Set<string> | undefined;

/**
* A custom reporter that is invoked once per failed a11yAudit call. This can be called
* multiple times per test, and the results are accumulated until testDone.
*
* @param axeResults The axe results for each a11yAudit.
* @returns Early returns if no violations are found.
*/
export async function middlewareReporter(axeResults: AxeResults) {
if (axeResults.violations.length === 0) {
return;
Expand All @@ -40,18 +58,21 @@ export async function middlewareReporter(axeResults: AxeResults) {
currentTestResult = {
moduleName: module.name,
testName,
testMetaData,
urls: [],
routes: [],
helpers: testMetaData.usedHelpers,
helpers: [],
stack,
violations: [],
};

currentViolationsMap = new Map();
currentUrls = new Set();
currentRouteNames = new Set();
}

currentUrls.add(currentURL());
currentRouteNames.add(currentRouteName());
currentUrls!.add(currentURL());
currentRouteNames!.add(currentRouteName());

axeResults.violations.forEach((violation) => {
let rule = currentViolationsMap!.get(violation.id);
Expand All @@ -64,15 +85,23 @@ export async function middlewareReporter(axeResults: AxeResults) {
});
}

/**
* Invoked once per test. Accumulates the results into a set of results used for
* reporting via the middleware reporter.
*/
export function pushTestResult() {
if (typeof currentTestResult !== 'undefined') {
currentTestResult.violations = [...currentViolationsMap!.values()];
currentTestResult.urls = [...currentUrls.values()];
currentTestResult.routes = [...currentRouteNames.values()];
currentTestResult.urls = [...currentUrls!.values()];
currentTestResult.routes = [...currentRouteNames!.values()];
currentTestResult.helpers = currentTestResult.testMetaData.usedHelpers;

TEST_SUITE_RESULTS.push(currentTestResult);

currentTestResult = undefined;
currentViolationsMap = undefined;
currentUrls = undefined;
currentRouteNames = undefined;
}
}

Expand Down
Loading