Skip to content

feat(test): DOM environment for deno test (--dom / test.dom config)#35159

Open
bartlomieju wants to merge 2 commits into
mainfrom
feat/test-dom-environment
Open

feat(test): DOM environment for deno test (--dom / test.dom config)#35159
bartlomieju wants to merge 2 commits into
mainfrom
feat/test-dom-environment

Conversation

@bartlomieju

Copy link
Copy Markdown
Member

Vitest lets users run tests in a simulated DOM via its jsdom and happy-dom
environments. Deno had no equivalent: users had to install a DOM library
themselves and wire its globals up by hand. This adds an experimental DOM
environment to deno test, enabled with the --dom[=happy-dom|jsdom] flag
or "test": { "dom": true | "happy-dom" | "jsdom" } in the config file.

When enabled, every test worker evaluates a generated preload module that
imports the DOM library from npm, creates a window and copies its globals
(document, HTMLElement, location, ...) onto globalThis. Keys that Deno
already provides (fetch, URL, streams, timers, crypto, ...) keep their
native implementations; only DOM integration classes (Event and subclasses,
EventTarget, DOMException, FormData, location, history) are taken from the
library so that events constructed in test code can be dispatched on DOM
nodes. The library version is baked into the binary (happy-dom 20.10.2,
jsdom 29.1.1) and is installed on demand; declaring the package in the
import map or package.json overrides it. Since each test file runs in a
fresh isolate, no teardown or global restoration is needed.

Depending on the DOM library, tests need --allow-env (happy-dom) and
--allow-read (jsdom) for the library to load, and type checking DOM globals
requires adding "dom" to compilerOptions.lib. The spec tests use small
mocks of the happy-dom and jsdom packages in the test registry; the real
libraries were verified manually, including @testing-library/dom on top of
both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant