Scripts2026年7月27日·1 分钟阅读

Happy DOM — Fast Headless Browser Implementation for Testing

A lightweight JavaScript DOM implementation that lets you run and test web code server-side without a full browser, optimized for speed with Vitest, Jest, and SSR frameworks.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Happy DOM
直接安装命令
npx -y tokrepo@latest install cca219b2-89bf-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

Happy DOM is a JavaScript implementation of a web browser without its graphical interface. It provides a fast, standards-compliant DOM that runs in Node.js, Bun, or Deno, making it ideal for server-side rendering, testing, and any scenario where you need a DOM without launching a real browser.

What Happy DOM Does

  • Implements the DOM API (Document, Element, Event, Window) in pure JavaScript
  • Supports Web Components, Custom Elements, and Shadow DOM
  • Provides Fetch API, Mutation Observer, and Tree Walker implementations
  • Integrates as a test environment for Vitest, Jest, and Bun test
  • Enables server-side rendering for React, Vue, Angular, Svelte, and Lit

Architecture Overview

Happy DOM implements the WHATWG DOM specification as a set of JavaScript classes that run in any JS runtime. The Window object bootstraps the environment; Document, Element, and Event classes follow the spec closely. Unlike jsdom, Happy DOM is built for performance: it avoids expensive C++ bindings and runs as pure JS, which makes it faster to start and lighter on memory.

Self-Hosting & Configuration

  • Install via npm: npm install happy-dom
  • For Vitest: set environment: "happy-dom" in vitest.config.ts
  • For Jest: use testEnvironment: "@happy-dom/jest-environment"
  • For SSR: create a Window instance and render your framework into it
  • Configure via the Window constructor options (url, width, height, settings)

Key Features

  • Significantly faster than jsdom for test suite execution
  • Pure JavaScript with no native dependencies or compilation steps
  • Declarative Shadow DOM support for modern web component testing
  • Fetch API with request interception capabilities
  • Active maintenance with support from Sentry and Canonical

Comparison with Similar Tools

  • jsdom — the established headless DOM; Happy DOM trades some API coverage for speed
  • Playwright/Puppeteer — launch real browsers; Happy DOM runs in-process without a browser binary
  • linkedom — minimal DOM implementation; Happy DOM provides broader API coverage
  • Miniflare (workerd) — Workers-specific runtime; Happy DOM is a general-purpose DOM
  • Deno DOM — Deno-native DOM bindings; Happy DOM works across Node, Bun, and Deno

FAQ

Q: Is Happy DOM a drop-in replacement for jsdom? A: For most test suites, yes. Some jsdom-specific APIs may differ, but standard DOM operations work the same way.

Q: How much faster is it compared to jsdom? A: Benchmarks vary, but test suites commonly see 2-5x faster execution, especially for large component libraries.

Q: Does it support CSS? A: Happy DOM parses stylesheets and supports computed styles. It does not perform visual layout or rendering.

Q: Can I use it for web scraping? A: Yes. Pass HTML into the Window and query the DOM. For sites requiring JavaScript execution, a full browser tool is more appropriate.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产