From cfda25ef9565622d61f64f1c5f1d1bacb811add5 Mon Sep 17 00:00:00 2001 From: drewlee <andlee@linkedin.com> Date: Wed, 1 Feb 2023 11:50:32 -0800 Subject: [PATCH 1/2] Fixes currentRouteName middleware reporter test failures --- .../setup-middleware-reporter.ts | 26 ++++++++++++- tests/unit/get-current-route-name-test.ts | 37 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/unit/get-current-route-name-test.ts diff --git a/addon-test-support/setup-middleware-reporter.ts b/addon-test-support/setup-middleware-reporter.ts index ccf6bff8..9692f11c 100644 --- a/addon-test-support/setup-middleware-reporter.ts +++ b/addon-test-support/setup-middleware-reporter.ts @@ -38,6 +38,30 @@ let currentViolationsMap: Map<string, Result> | undefined = undefined; let currentUrls: Set<string> | undefined; let currentRouteNames: Set<string> | undefined; +/** + * Utility to retrieve the route name corresponding to the current test. Absorbs the emitted + * assertion error if route name is `null`, resulting in an empty string return value. + * + * @param getFn Function to use to derive the route name. + * @returns Route name or empty string. + */ +export function _getCurrentRouteName(getFn = currentRouteName): string { + let routeName = ''; + + try { + routeName = getFn(); + } catch (error: unknown) { + if ( + error instanceof Error && + !/currentRouteName (\w|\s)+ string/.test(error.message) + ) { + throw error; + } + } + + return routeName; +} + /** * 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. @@ -73,7 +97,7 @@ export async function middlewareReporter(axeResults: AxeResults) { } currentUrls!.add(currentURL()); - currentRouteNames!.add(currentRouteName()); + currentRouteNames!.add(_getCurrentRouteName()); axeResults.violations.forEach((violation) => { let rule = currentViolationsMap!.get(violation.id); diff --git a/tests/unit/get-current-route-name-test.ts b/tests/unit/get-current-route-name-test.ts new file mode 100644 index 00000000..d25412f4 --- /dev/null +++ b/tests/unit/get-current-route-name-test.ts @@ -0,0 +1,37 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; +import { _getCurrentRouteName } from 'ember-a11y-testing/test-support/setup-middleware-reporter'; + +module('Unit | Utils | getCurrentRouteName', function (hooks) { + setupTest(hooks); + + test('gets the route name for the current test', function (assert) { + function mockCurrentRouteName(): string { + return 'index'; + } + + const result = _getCurrentRouteName(mockCurrentRouteName); + + assert.strictEqual(result, 'index'); + }); + + test('absorbs `currentRouteName` error when route name is null', function (assert) { + function currentRouteNameMock(): string { + throw new Error('currentRouteName shoudl be a string'); + } + + const result = _getCurrentRouteName(currentRouteNameMock); + + assert.strictEqual(result, ''); + }); + + test('bubbles up all other emitted errors', function (assert) { + function mockCurrentRouteName(): string { + throw new Error('Catastrophic error!'); + } + + assert.throws(() => { + _getCurrentRouteName(mockCurrentRouteName); + }, /Catastrophic error!/); + }); +}); From 0d30079098104ea24a71f2e0497222311cb2a162 Mon Sep 17 00:00:00 2001 From: drewlee <andlee@linkedin.com> Date: Wed, 1 Feb 2023 15:22:03 -0800 Subject: [PATCH 2/2] Test name typo fix --- tests/unit/get-current-route-name-test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/get-current-route-name-test.ts b/tests/unit/get-current-route-name-test.ts index d25412f4..dd06e1c7 100644 --- a/tests/unit/get-current-route-name-test.ts +++ b/tests/unit/get-current-route-name-test.ts @@ -2,7 +2,7 @@ import { module, test } from 'qunit'; import { setupTest } from 'ember-qunit'; import { _getCurrentRouteName } from 'ember-a11y-testing/test-support/setup-middleware-reporter'; -module('Unit | Utils | getCurrentRouteName', function (hooks) { +module('Unit | Utils | _getCurrentRouteName', function (hooks) { setupTest(hooks); test('gets the route name for the current test', function (assert) {