API Reference
Complete reference for the @stencil/vitest testing utilities.
Render Function
render(VNode, options?)
Renders a Stencil component for testing.
import { render, h } from '@stencil/vitest';
const result = await render(<my-component name="World" />);
Options
| Option | Type | Default | Description |
|---|---|---|---|
waitForReady | boolean | true | Wait for component hydration before returning |
spyOn | SpyOnOptions | - | Configure spies for methods, props, and lifecycle hooks |
waitForReady
By default, render() waits for components to be fully hydrated before returning. It detects when Stencil applies the hydrated flag (class or attribute) to your component, respecting your stencil.config settings.
// Default behavior - waits for hydration
const { root } = await render(<my-component />);
// Skip hydration wait (useful for pre-ready states)
const { root } = await render(<my-component />, { waitForReady: false });
spyOn
Configure spies for component methods, props, and lifecycle hooks. See the Mocking & Spying guide for comprehensive documentation.
const { root, spies } = await render(<my-component />, {
spyOn: {
methods: ['handleClick'],
props: ['variant'],
lifecycle: ['componentDidLoad'],
},
});
expect(spies?.methods.handleClick).toHaveBeenCalled();
Return Values
| Property | Type | Description |
|---|---|---|
root | HTMLElement | The rendered component element |
waitForChanges | () => Promise<void> | Wait for component re-renders |
setProps | (props: object) => Promise<void> | Update props and wait for changes |
unmount | () => void | Remove the component from the DOM |
spyOnEvent | (eventName: string) => EventSpy | Create a spy for custom events |
spies | ComponentSpies | undefined | Spy objects when using spyOn option |
Example
import { render, h } from '@stencil/vitest';
const { root, waitForChanges, setProps, unmount, spyOnEvent } = await render(
<my-component name="World" />
);
// Access the element
expect(root.textContent).toContain('World');
// Update props directly
root.name = 'Stencil';
await waitForChanges();
// Or update props via setProps
await setProps({ name: 'Vitest' });
// Spy on events
const eventSpy = spyOnEvent('myEvent');
root.click();
expect(eventSpy).toHaveReceivedEvent();
// Unmount component
unmount();
Custom Matchers
Class Matchers
toHaveClass(className)
Checks if an element has a specific CSS class:
expect(element).toHaveClass('active');
expect(element).toHaveClass('btn-primary');
expect(element).not.toHaveClass('disabled');
toHaveClasses(classNames)
Checks if an element has all specified CSS classes (partial match):
expect(element).toHaveClasses(['btn', 'btn-primary']);
expect(element).toHaveClasses(['active', 'visible']);
toMatchClasses(classNames)
Checks if an element has exactly the specified CSS classes (exact match):
expect(element).toMatchClasses(['btn', 'btn-primary']);
// Fails if element has additional classes
Attribute Matchers
toHaveAttribute(name, value?)
Checks if an element has a specific attribute, optionally with a specific value:
expect(element).toHaveAttribute('aria-label', 'Close');
expect(element).toHaveAttribute('disabled');
expect(element).not.toHaveAttribute('hidden');
toEqualAttribute(name, value)
Checks if an element has an attribute with an exact value:
expect(element).toEqualAttribute('type', 'button');
expect(element).toEqualAttribute('aria-label', 'Submit form');
toEqualAttributes(attributes)
Checks multiple attributes and their values at once:
expect(element).toEqualAttributes({
type: 'button',
disabled: true,
'aria-label': 'Submit',
});