Skip to content

API Overview

SymbolPurposeExecution modeCommon gotcha
withinCreates scoped query APISyncRequired get* methods throw AssayQueryError
queryInShadow / queryPart / getSlottedCrosses custom-element boundariesSyncOpen shadow roots are required
fire* / dispatchDispatches platform event instancesSyncDoes not reproduce browser default behavior
waitUntil / retry / waitForEventWaits for conditions, assertions, or eventsAsyncUse a signal or timeout for bounded waits
delay / nextTickSchedules timers or microtasksAsyncPrefer nextTick() for microtask-scheduled work

Package Entry Point

ImportPurpose
@vielzeug/assayDOM queries, events, wait helpers, errors, and types

Queries

within(root)

Creates a QueryScope for an Element, ShadowRoot, Document, or DocumentFragment.

MethodReturnsUse
get(selector)ElementRequired CSS match; throws AssayQueryError
query(selector)Element | nullOptional CSS match
queryAll(selector)Element[]All CSS matches
getByText(text, selector?)ElementRequired exact trimmed-text match
queryByText(text, selector?)Element | nullOptional exact trimmed-text match
queryAllByText(text, selector?)Element[]All exact trimmed-text matches
getByTestId(id)ElementRequired data-testid match
queryByTestId(id)Element | nullOptional data-testid match
queryAllByTestId(id)Element[]All data-testid matches

Text selectors default to '*'. Required-query failures include the lookup and a bounded view of the scoped DOM.

Shadow and slot helpers

FunctionReturnsDescription
queryInShadow(host, selector)Element | nullFirst match in an open shadow root
queryAllInShadow(host, selector)Element[]All matches in an open shadow root
queryPart(host, part)Element | nullFirst shadow element whose part token matches
getSlotted(host, slotName?)Element[]Direct light-DOM children in a named or default slot

These helpers return null or [] when there is no shadow root. Dynamic test IDs, parts, and slot names are matched as attribute values rather than interpolated into CSS selectors.

Event dispatch

All event helpers synchronously return dispatchEvent()'s boolean result.

ts
import {
  dispatch,
  fireBlur,
  fireChange,
  fireClick,
  fireCustom,
  fireFocus,
  fireInput,
  fireKeyDown,
  fireKeyUp,
  fireSubmit,
} from '@vielzeug/assay';

fireClick(button, { clientX: 20 });
fireInput(input);
fireKeyDown(input, { key: 'Enter' });
fireCustom(element, 'item-added', { detail: { id: '42' } });
dispatch(element, new Event('ready'));
FunctionEvent classDefaults
fireBlur / fireFocusFocusEventPlatform defaults (bubbles: false)
fireChangeEventbubbles: true
fireInputInputEventbubbles: true
fireClickMouseEventbubbles: true, cancelable: true
fireKeyDown / fireKeyUpKeyboardEventbubbles: true, cancelable: true
fireSubmitSubmitEventbubbles: true, cancelable: true
fireCustomCustomEventbubbles: true, cancelable: true, composed: false

fireCustom(target, type, init?) dispatches a CustomEvent with the given type. Assay intentionally does not provide browser-default or fallback pointer/touch simulation.

Async waiting

ts
await waitUntil(() => ready, { interval: 20, signal, timeout: 1000 });
await retry(() => expect(spy).toHaveBeenCalled(), { signal, timeout: 1000 });
await waitForEvent(target, 'ready', { signal, timeout: 1000 });
await delay(100, { signal });
await nextTick();
FunctionSuccess conditionOptions
waitUntil(predicate, options?)Predicate returns truetimeout, interval, signal
retry(assertion, options?)Assertion stops throwingtimeout, interval, signal, message
waitForEvent(target, type, options?)Target emits typetimeout, signal
delay(ms?, options?)Timer elapsessignal
nextTick()Next microtasknone

waitUntil, retry, and waitForEvent reject with AssayTimeoutError when their timeout expires. A supplied abort signal rejects with its reason and removes timers and event listeners.

Types

ts
export interface QueryScope {
  get(selector: string): Element;
  query(selector: string): Element | null;
  queryAll(selector: string): Element[];
  getByText(text: string, selector?: string): Element;
  queryByText(text: string, selector?: string): Element | null;
  queryAllByText(text: string, selector?: string): Element[];
  getByTestId(id: string): Element;
  queryByTestId(id: string): Element | null;
  queryAllByTestId(id: string): Element[];
}

CustomEventInit, DelayOptions, RetryOptions, and WaitOptions are exported option types for event and wait helpers.

Errors

ErrorMeaning
AssayErrorBase class for Assay-originated errors
AssayQueryErrorA required get* query had no match
AssayTimeoutErrorA wait operation reached its timeout

Use instanceof AssayError to narrow any value to the Assay error hierarchy.