First, install Happy DOM (see Bun’s Happy DOM guide).
terminal
Next, install the Testing Library packages you plan to use, plus
@testing-library/jest-dom for the matchers used later. For React:
terminal
Next, create preload scripts for Happy DOM and for Testing Library.
For Testing Library, extend Bun’s
expect function with Testing Library’s matchers. Optionally, run cleanup after each test to better match the behavior of test runners like Jest.
Next, add these preload scripts to your
bunfig.toml. You can also put everything in a single preload.ts script.
bunfig.toml
If you use TypeScript, you also need declaration merging so the new matcher types show up in your editor. Create a type declaration file that extends
Matchers.
Now you can use Testing Library in your tests.
See the Testing Library docs, the Happy DOM repo, and DOM testing.