As a prerequisite to using Testing Library you will need to install Happy Dom. (see Bun’s Happy DOM guide for more information).
terminal
Next you should install the Testing Library packages you are planning on using. For example, if you are setting up testing for React your installs may look like this. You will also need to install
@testing-library/jest-dom
to get matchers working later.
terminal
Next you will need to create a preload script for Happy DOM and for Testing Library. For more details about the Happy DOM setup script see Bun’s Happy DOM guide.
For Testing Library, you will want to extend Bun’s
expect
function with Testing Library’s matchers. Optionally, to better match the behavior of test-runners like Jest, you may want to run cleanup after each test.
Next, add these preload scripts to your
bunfig.toml
(you can also have everything in a single preload.ts
script if you prefer).
bunfig.toml
If you are using TypeScript you will also need to make use of declaration merging in order to get the new matcher types to show up in your editor. To do this, create a type declaration file that extends
Matchers
like this.
You should now be able to use Testing Library in your tests
Refer to the Testing Library docs, Happy DOM repo and Docs > Test runner > DOM for complete documentation on writing browser tests with Bun.