Docs menu

Guides


Primitives
Forms
Layout
Feedback
Overlays
Navigation
Data Display
AI

Testing

@kernelui-lib/testing checks a rendered UI against theWAI-ARIA Authoring Practicespatterns Kernel is built on, plus Kernel's own promises. Use it in your test suite to verify a screen you composed from Kernel components, or a widget you built yourself. A coding agent can run it too: every failure names the rule, the element and the fix.

npm install -D @kernelui-lib/testing

What it checks

  • Roles and elements. The native element where one exists (<dialog>, <details>, <input type="range">, a real <button> under role="switch") and the ARIA roles where it doesn't.
  • Accessible names. From aria-labelledby, aria-label, <label>, alt, <legend> and content. A field named only by its placeholder fails.
  • State that agrees with itself. Exactly one selected tab, aria-controls and other id references that resolve, aria-expanded matching whether the popup is actually open.
  • Keyboard behaviour. Dispatched key presses, then the focus and selection are read back: arrow keys, Home and End, Escape, roving tabindex with a single Tab stop.
  • Kernel invariants. A field keeps its <label> in the DOM under hideLabel, icons are decorative, and in a browser, clickable parts have user-select: none.

API

  • checkPattern(root, pattern, options?): one APG pattern on root or anything inside it. Patterns: accordion, button, checkbox, combobox, dialog, disclosure, field, grid, hover-card, listbox, menu, radio-group, select, slider, switch, tabs, toggle-button, toolbar and tooltip.
  • checkComponent(root, name, options?): a Kernel component's patterns plus its invariants. name is the React export ("DropdownMenu"), the docs slug or the custom element tag. React output and custom elements pass the same checks, because they render the same DOM.
  • checkPage(root?, options?): finds every Kernel component under root (default document.body) and checks each one, plus page-wide rules: unique ids, resolving references, named controls, no interactive element nested in another.
  • assertPattern, assertComponent and assertPage throw an error listing every issue instead of returning.

Every function is async and returns { passed, issues, skipped, passes, environment }. Errors fail a check. Warnings, for APG recommendations that aren't requirements, fail only with strict: true. Options:act wraps each interaction (pass React Testing Library's),interactions: false skips the keyboard checks,ignore takes rule ids ("tabs/tablist-name") or whole patterns, and settle replaces the wait after each interaction.

{
  passed: false,
  environment: "jsdom",
  issues: [{
    rule: "tooltip/escape-dismisses",
    severity: "error",
    message: "Escape didn't hide the tooltip. WCAG 1.4.13 requires …",
    target: 'div.kernel-Tooltip-content[role=tooltip] "Copy to clipboard"',
    component: "Tooltip",
  }],
  skipped: [{ rule: "dialog/escape", kind: "native", reason: "Escape closes a modal <dialog> natively …" }],
  passes: ["tooltip/content", "tooltip/trigger", "tooltip/shows-on-focus"],
}

Vitest and Jest

It's plain DOM with no test-runner globals, so it works the same in vitest and jest, with jsdom or happy-dom. The main entry ships as ESM and CommonJS, so Jest needs no transform config for it.

// Settings.test.tsx — vitest or jest, with jsdom or happy-dom
import { act, render } from "@testing-library/react";
import { assertComponent, assertPage, checkPattern } from "@kernelui-lib/testing";
import { Settings } from "./Settings";

const options = { act }; // lets React flush between simulated key presses

test("the plan picker is an accessible radio group", async () => {
  const { container } = render(<Settings />);
  await assertComponent(container, "RadioGroup", options);
});

test("every Kernel component on the screen passes", async () => {
  render(<Settings />);
  await assertPage(document.body, options);
});

test("our own tablist follows the APG tabs pattern", async () => {
  const { getByRole } = render(<Settings />);
  const result = await checkPattern(getByRole("tablist"), "tabs", options);
  expect(result.issues).toEqual([]);
});

The custom elements are checked the same way:

import "@kernelui-lib/elements";
import { assertComponent } from "@kernelui-lib/testing";

test("<kernel-tabs>", async () => {
  document.body.innerHTML = `
    <kernel-tabs default-value="a">
      <kernel-tabs-list aria-label="Sections">
        <kernel-tab value="a">Account</kernel-tab>
        <kernel-tab value="b">Billing</kernel-tab>
      </kernel-tabs-list>
      <kernel-tab-panel value="a">…</kernel-tab-panel>
      <kernel-tab-panel value="b">…</kernel-tab-panel>
    </kernel-tabs>`;
  await assertComponent(document.body, "Tabs");
});

Playwright

In a real browser the checks also see the Popover API, modal<dialog> focus and Kernel's CSS. The/playwright helpers add the checks to the page and run them there:

// settings.spec.ts
import { test, expect } from "@playwright/test";
import { assertComponentInPage, checkPageInPage } from "@kernelui-lib/testing/playwright";

test("settings page", async ({ page }) => {
  await page.goto("/settings");
  await assertComponentInPage(page, "#plan", "RadioGroup");

  // Open the dialog first: closed dialogs skip their focus checks.
  await page.getByRole("button", { name: "Edit profile" }).click();
  const result = await checkPageInPage(page, "dialog[open]");
  expect(result.issues.filter((i) => i.severity === "error")).toEqual([]);
});

Or add the browser bundle yourself, which defineswindow.KernelTesting:

import { createRequire } from "node:module";
const require = createRequire(import.meta.url);

await page.addScriptTag({ path: require.resolve("@kernelui-lib/testing/browser") });
const result = await page.evaluate(() =>
  window.KernelTesting.checkPattern(document.querySelector("[role=tablist]"), "tabs"),
);

What a simulated DOM can't check

jsdom and happy-dom have no layout, no Popover API and no modal<dialog> behaviour. Checks that need these are listed in skipped with kind: "environment", and they never pass or fail. Run the same checks in Playwright to cover them.

Synthetic key events can't trigger what the browser itself does either: Space on a <button>, arrow keys in a native radio group, Escape on a <dialog> or apopover="auto". Kernel uses the native element for these instead of rebuilding them, so the check confirms the native element is there and lists the behaviour as kind: "native".

  • A closed dialog or menu skips its open-state checks. Open it before checking.
  • jsdom has no scrollIntoView. Stub it in your test setup if a combobox throws.
  • jsdom has no HTMLDialogElement.prototype.showModal. Stub it (set the open attribute) to render an open Dialog.
  • Interactions change state, like a selected tab or a flipped switch, and restore it where they can. Run checks on a fresh render.