Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 38 ยท Testing and Tooling

Testing Slots, Keyboard Input and Controllers in Lit

Go beyond basic component tests: check slotted content, simulate keyboard input, test reactive controllers with a fake host and fake timers, and keep tests reliable.

The basic tests covered rendering and clicks. Real components also have slots, keyboard behavior, timers and reusable controllers. Each of these has a small trick that keeps tests simple and stable. We use the same mini test style as before: create an element, wait for it, act, assert.

Testing slotted content

Slotted nodes stay in the light DOM, so the host element keeps them as normal children. What lives in the shadow tree is the <slot> element, and it can tell you which nodes it received through assignedNodes() or assignedElements().

Asserting slot assignment

A good slot test checks three things: content goes to the right slot, the default slot receives the rest, and the fallback shows when nothing is assigned.

Simulating keyboard input

For widgets such as tabs or menus, test keys the way a user would press them: dispatch a KeyboardEvent on the element that has focus. Make it bubbles: true so handlers on ancestors fire.

Arrow key navigation

Notice the assertions read aria-selected, the thing assistive technology uses. That tests behavior and accessibility at the same time (more on this in accessible widgets).

Testing a controller with a fake host

A reactive controller does not need a DOM. It only needs a host with addController and requestUpdate. You can build one in three lines and call the lifecycle yourself. That makes controller tests fast and fully deterministic.

JS Controller with fake host and fake timer

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The trick is injecting the dependency: the controller takes a scheduler parameter that defaults to the real timers. Tests pass a fake, so nothing waits one real second. The same idea works for fetch, Date and Math.random.

When a test needs timers on a real component, libraries such as Sinon's fake timers can replace setTimeout for the duration of a test. Always restore them afterward, or other tests in the file will hang.

Keeping tests reliable

  • Await everything. If a test finishes before an update, errors appear in a different test.
  • Clean up fixtures. Leftover elements keep timers and listeners alive across tests.
  • No fixed sleeps. Waiting 500 ms is slow and still flaky. Wait for a condition, such as an event or updateComplete.
  • One idea per test. A failing test name should tell you what broke.
  • Test accessibility cheaply. Libraries like axe-core can scan a fixture for common problems in one call; run them on the rendered shadow DOM in your suite.

The next lesson covers building and publishing a component library.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where do slotted children of a component live in the DOM?

    Slotted content remains the host's light DOM children. The slot element in the shadow tree merely renders them.
  2. 2. Which method lists the elements a slot currently displays?

    assignedElements() returns the light DOM elements assigned to the slot; it is empty when fallback content shows.
  3. 3. Why does the controller in this lesson accept a scheduler argument?

    Dependency injection lets tests replace real timers with deterministic fakes.
  4. 4. Why prefer waiting for a condition over a fixed sleep?

    A fixed delay is either too short (flaky) or too long (slow). Waiting for the actual event or update is exact.

Summary

  • Query slot assignment with assignedElements() and test the fallback.
  • Dispatch KeyboardEvents on the focused element and assert on ARIA state.
  • Test controllers with a fake host and an injected scheduler.
  • Await updates, clean up fixtures and avoid fixed sleeps.

Next: Building and Publishing Components