feat(test): DOM environment for deno test (--dom / test.dom config)#35159
Open
bartlomieju wants to merge 2 commits into
Open
feat(test): DOM environment for deno test (--dom / test.dom config)#35159bartlomieju wants to merge 2 commits into
bartlomieju wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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]flagor
"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.