Skip to content

Commit

Permalink
Extract test fixture for Axe
Browse files Browse the repository at this point in the history
  • Loading branch information
textbook committed Jan 8, 2025
1 parent 5f6e631 commit aedb51d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
9 changes: 4 additions & 5 deletions e2e/tests/about.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import AxeBuilder from "@axe-core/playwright";
import { test, expect } from "@playwright/test";
import { test, expect } from "./fixtures.js";

test("can be visited", async ({ page }) => {
await page.goto("/nested/about/path");

await expect(page.getByRole("heading", { level: 1 })).toHaveText("About");
});

test("meets a11y requirements", async ({ page }) => {
test("meets a11y requirements", async ({ axe, page }) => {
await page.goto("/nested/about/path");
const axeBuilder = new AxeBuilder({ page }).withTags("wcag2a");
const { violations } = await axeBuilder.analyze();

const { violations } = await axe.analyze();
expect(violations).toHaveLength(0);
});
20 changes: 20 additions & 0 deletions e2e/tests/fixtures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import AxeBuilder from "@axe-core/playwright";
import { test as base } from "@playwright/test";

/**
* @typedef {import("@playwright/test").PlaywrightTestArgs & import("@playwright/test").PlaywrightTestOptions} T
* @typedef {import("@playwright/test").PlaywrightWorkerArgs & import("@playwright/test").PlaywrightWorkerOptions} W
*/

/**
* @type {import("@playwright/test").TestType<T & { a11yTags: string[]; axe: AxeBuilder }, W>}
*/
export const test = base.extend({
a11yTags: [["wcag2a"], { option: true }],
axe: async ({ a11yTags, page }, use) => {
const axe = new AxeBuilder({ page }).withTags(a11yTags);
await use(axe);
},
});

export { expect } from "@playwright/test";
9 changes: 4 additions & 5 deletions e2e/tests/home.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AxeBuilder from "@axe-core/playwright";
import { test, expect } from "@playwright/test";
import { test, expect } from "./fixtures.js";

test("has title", async ({ page }) => {
await page.goto("/");
Expand All @@ -22,9 +21,9 @@ test("shows server status", async ({ page }) => {
await expect(page.getByText("Server says: Hello, world!")).toBeAttached();
});

test("meets a11y requirements", async ({ page }) => {
test("meets a11y requirements", async ({ axe, page }) => {
await page.goto("/");
const axeBuilder = new AxeBuilder({ page }).withTags("wcag2a");
const { violations } = await axeBuilder.analyze();

const { violations } = await axe.analyze();
expect(violations).toHaveLength(0);
});

0 comments on commit aedb51d

Please sign in to comment.