Skip to content

Commit 76bd448

Browse files
dmealingclaude
andcommitted
test(client/web): fix happy-dom global-document setup (18 pre-existing fails → 0)
The client/web test suite had 18 pre-existing failures when run from the client/web/ root: every `@testing-library/dom` `screen.*` call threw "a global document has to be available". Root cause: Bun only honours the bunfig.toml in the cwd it is invoked from, so the per-package preloads that install a JSDOM `document` never fired when `bun test` was run from client/web/. Fix: - Added client/web/bunfig.toml that preloads the existing per-package JSDOM setup (reused from packages/react/ so that `jsdom` resolves through that package's node_modules). - Made the per-package test/setup.ts files idempotent so the per-test-file `import "./setup.js"` does not replace globalThis.document after `@testing-library/dom`'s `screen` module has already captured it (which had been silently rebinding `screen` to an orphaned, empty document body). Verified: `bun test` from client/web/ now reports 84 pass + 0 fail (was 66 pass + 18 fail). Each package's `bun test` continues to pass when run from its own directory (react 12, tanstack 29, runtime-web 30, angular 13). Purely test-infrastructure — no runtime behaviour change. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 7b47338 commit 76bd448

3 files changed

Lines changed: 31 additions & 6 deletions

File tree

client/web/bunfig.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[test]
2+
# Preload JSDOM setup so component tests have a real `document` available the
3+
# moment `@testing-library/dom`'s `screen` module is first evaluated. The
4+
# per-package bunfig.toml files cover running `bun test` from inside an
5+
# individual package; this root-level config covers running it from
6+
# client/web/.
7+
#
8+
# We reuse the react package's setup file because it already pulls JSDOM from
9+
# its own node_modules (Bun's isolated install does not hoist jsdom up to
10+
# client/web/, so a root-level setup.ts could not import it directly).
11+
preload = ["./packages/react/test/setup.ts"]
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { JSDOM } from "jsdom";
22

3-
const dom = new JSDOM("<!DOCTYPE html><html><body></body></html>");
4-
global.document = dom.window.document as any;
5-
global.window = dom.window as any;
3+
// Idempotent: if a global document has already been registered (e.g. by the
4+
// client/web root bunfig.toml preload), reuse it. Replacing globalThis.document
5+
// after `@testing-library/dom`'s `screen` module is evaluated leaves `screen`
6+
// bound to the previous, now-orphaned document.body — which makes every
7+
// `screen.*` query look at an empty body.
8+
if (typeof globalThis.document === "undefined") {
9+
const dom = new JSDOM("<!DOCTYPE html><html><body></body></html>");
10+
global.document = dom.window.document as any;
11+
global.window = dom.window as any;
12+
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { JSDOM } from "jsdom";
22

3-
const dom = new JSDOM("<!DOCTYPE html><html><body></body></html>");
4-
global.document = dom.window.document as any;
5-
global.window = dom.window as any;
3+
// Idempotent: if a global document has already been registered (e.g. by the
4+
// client/web root bunfig.toml preload), reuse it. Replacing globalThis.document
5+
// after `@testing-library/dom`'s `screen` module is evaluated leaves `screen`
6+
// bound to the previous, now-orphaned document.body — which makes every
7+
// `screen.*` query look at an empty body.
8+
if (typeof globalThis.document === "undefined") {
9+
const dom = new JSDOM("<!DOCTYPE html><html><body></body></html>");
10+
global.document = dom.window.document as any;
11+
global.window = dom.window as any;
12+
}

0 commit comments

Comments
 (0)