Skip to main content
Bun includes built-in support for Web APIs, making it easy to test DOM-related code without additional setup.

Built-in DOM APIs

Bun provides these Web APIs out of the box:
  • fetch
  • Request, Response, Headers
  • URL, URLSearchParams
  • Blob, File
  • FormData
  • TextEncoder, TextDecoder
  • ReadableStream, WritableStream, TransformStream
  • WebSocket
  • EventTarget, Event, CustomEvent
  • AbortController, AbortSignal
  • structuredClone

Testing with happy-dom

For full DOM support, use happy-dom:
Create a test setup file:
#test/setup.ts
Configure in bunfig.toml:
Now you can use DOM APIs in tests:

Testing React components

Test React components with @testing-library/react:
Setup file:
#test/setup.ts
Test a component:

Testing with jsdom

Alternatively, use jsdom:
Setup file:
#test/setup.ts

Testing Web Components

Testing event handling

Testing async DOM updates

Cleanup

Clean up DOM between tests:

Accessing window properties

Form testing