Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Guides · Test Runner

Write browser DOM tests with Bun and happy-dom

Register Happy DOM's mocked browser APIs in a preload script to test DOM code with bun test.

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.

bun add -d @happy-dom/global-registrator

This module exports a "registrator" that injects the mocked browser APIs into the global scope.

happydom.ts
import { GlobalRegistrator } from "@happy-dom/global-registrator";

GlobalRegistrator.register();

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
[test]
preload = "./happydom.ts"

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

dom.test.ts
import { test, expect } from "bun:test";

test("set button text", () => {
  document.body.innerHTML = `<button>My button</button>`;
  const button = document.querySelector("button");
  expect(button?.innerText).toEqual("My button");
});

With Happy DOM registered, the test passes.

bun test

dom.test.ts:
✓ set button text [0.82ms]

 1 pass
 0 fail
 1 expect() calls
Ran 1 test across 1 file. [125.00ms]

See the Happy DOM repo and DOM testing.