This library re-exports everything from the DOM Testing Library (@testing-library/dom). See the documentation to see what goodies you can use.


import { render } from '@testing-library/preact'
const { results } = render(<YourComponent />, { options })


containerThe HTML element the component is mounted to.baseElement
baseElementThe root HTML element to which the container is appended to.document.body
queriesQueries to bind to the baseElement. See getQueriesForElement.null
hydrateUsed when the component has already been mounted and requires a rerender. Not needed for most people. The rerender function passed back to you does this already.false
wrapperA parent component to wrap YourComponent.null


containerThe HTML element the component is mounted to.
baseElementThe root HTML element to which the container is appended to.
debugLogs the baseElement using prettyDom.
unmountUnmounts the component from the container.
rerenderCalls render again passing in the original arguments and sets hydrate to true.
asFragmentReturns the innerHTML of the container.
...queriesReturns all query functions to be used on the baseElement. If you pass in query arguments than this will be those, otherwise all.


Unmounts the component from the container and destroys the container.

๐Ÿ“ When you import anything from the library, this automatically runs after each test. If you'd like to disable this then set process.env.PTL_SKIP_AUTO_CLEANUP to true when running your tests.

import { render, cleanup } from '@testing-library/preact'
afterEach(() => {
}) // Default on import: runs it after each test.
render(<YourComponent />)
cleanup() // Or like this for more control.


Just a convenience export that points to preact/test-utils/act. All renders and events being fired are wrapped in act, so you don't really need this. It's responsible for flushing all effects and rerenders after invoking it.

๐Ÿ“ If you'd love to learn more, checkout this explanation. Even though it's for React, it gives you an idea of why it's needed.


Passes it to the @testing-library/dom fireEvent. It's also wrapped in act so you don't need to worry about doing it.

๐Ÿ“ Keep in mind mainly when using h / Preact.createElement that React uses a Synthetic event system, and Preact uses the browser's native addEventListener for event handling. This means events like onChange and onDoubleClick in React, are onInput and onDblClick in Preact. The double click example will give you an idea of how to test using those functions.

Example 1

const cb = jest.fn();
function Counter() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>{count}</button>;
const { container: { firstChild: buttonNode }, } = render(<Counter />);
// Clear the first call to useEffect that the initial render triggers.
// Fire event Option 1.
// Fire event Option 2.
new Event('MouseEvent', {
bubbles: true,
cancelable: true,
button: 0,

Example 2

const handler = jest.fn()
const {
container: { firstChild: input },
} = render(<input type="text" onInput={handler} />)
fireEvent.input(input, { target: { value: 'a' } })

Example 3

const ref = createRef()
const spy = jest.fn()
h(elementType, {
onDblClick: spy,
Last updated on by Matan Borenkraout