Skip to main content
Use Happy DOM to write browser tests with Bun’s test runner. Happy DOM implements mocked versions of browser APIs like document and location.
Install @happy-dom/global-registrator.
terminal

This module exports a “registrator” that injects the mocked browser APIs into the global scope.
happydom.ts

This file needs to run before any of your test files. That’s a job for Bun’s built-in preload option. Create a bunfig.toml file in the root of your project (if it doesn’t already exist) and add the following lines. The ./happydom.ts file should contain the registration code from the previous step.
bunfig.toml

Now bun test executes happydom.ts before your test files, so you can write tests that use browser APIs.
dom.test.ts

With Happy DOM registered, the test passes.
terminal

See the Happy DOM repo and DOM testing.