Simulated DOM (JSDOM/HappyDOM) vs Real Browser Engines
Green JSDOM tests ship broken layouts because Node.js DOM shims lack a layout engine, returning zeros for geometry and ignoring stacking contexts and CORS.
About this page
Simulated DOM vs Real Browser Engine Pipeline
Trace where JSDOM stops versus where Blink computes layout geometry and hit-testing.
Both JSDOM and Blink parse markup into Element nodes and attach event listeners.
document.body.innerHTML = '<div class="overlay"></div><button>Pay</button>';- jsdomNodes
- 2
- blinkNodes
- 2
- layoutComputed
- false
Under the Hood#
Blink, Gecko, and WebKit run a multi-stage C++ pipeline from HTML parsing to GPU compositing. Simulated DOM libraries reimplement only the JavaScript object model in Node.js and stub out geometry.
- No CSSOM or Box Tree: JSDOM parses inline styles into strings but never runs cascade resolution or Render Tree Layout, so
offsetWidthandgetBoundingClientRect()return0. - Missing Hit-Testing:
elementFromPoint()and synthetic clicks in JSDOM dispatch DOM events directly on the target node without checkingpointer-events: none,z-indexocclusion, oroverflow: hiddenclipping. - Permissive Network Stack: Node.js
undicior mockedfetch()skips browser origin policies,SameSitecookie jars, and mixed-content blocks.
// JSDOM passes even when an overlay blocks the button
test('submit button geometry', () => {
document.body.innerHTML = `<button style="width:120px">Save</button>`;
const btn = document.querySelector('button')!;
expect(btn.getBoundingClientRect().width).toBe(0); // JSDOM stub
});Problem Genealogy#
- 2010 (JSDOM in Node.js): Eliminated 1.5s-3s per-suite browser startup overhead, cutting pure JS unit test suites from minutes to sub-second runs.
- 2019 (HappyDOM): Traded spec completeness for 2x-4x faster HTML parsing in SSR and component tests, while keeping geometry stubbed at
0. - 2022 (Vitest Browser Mode & Playwright CT): Reintroduced real Chromium/WebKit frames over WebDriver BiDi and CDP, adding ~300ms worker boot cost in exchange for authentic layout and security boundaries.
Production Pitfalls#
- Invisible Occlusion Bugs: A modal backdrop with
z-index: 50covers a submit button in production, yet JSDOMfireEvent.click(btn)succeeds because it never executes hit-testing. - False-Negative ResizeObserver Tests: Components relying on
ResizeObserverorIntersectionObservernever receive real viewport intersections in Node.js unless manually mocked, hiding infinite-loop layout bugs.
Where this connects
Prerequisites
Render Tree & Layout
Real browser engines compute box geometry during layout; simulated DOMs return static zeros.
CSS Stacking Context
Hit-testing and pointer interception depend on stacking context paint order absent in JSDOM.
CORS Preflight and SameSite Cookie Boundaries
Simulated fetch mocks bypass browser CORS preflight and SameSite cookie enforcement.
Next & related
Test Fidelity Spectrum: Unit, Component, Browser & E2E
Choosing between Node unit tests and browser component tests depends on whether layout or hit-testing is under test.
Seen in Architecture Autopsies
Autopsy #07: 412 Green Tests and a Dead Menu Button
JSDOM skips CSS layout, stacking-context hit-testing, and compositor touch routing, passing synthetic clicks directly.