happy-dom

happy-dom:在无浏览器环境下运行 DOM 测试的快速 JavaScript DOM 实现。适用于“无浏览器测试”、替代 jsdom、在 Vitest 或 Jest 中配置测试环境、进行服务端渲染(SSR)测试以及比较与 jsdom 的性能场景。

仓库最近推送
GitHub Stars
219
Forks
38
Issues / PRs
7

安装方式

先用 Prompt 核对来源与依赖。也可下载安装包。

用 AI 助手安装

复制提示词,让助手先核对固定版本和依赖。

查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "happy-dom":https://skillpi.com/zh/skills/deepskill-0c7598ececebbefcd08eda946c8cbadf8efe1eeaee6c0a5be8e228840fa9a7fd。安装前核对上游来源和版本:https://github.com/TerminalSkills/skills。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。

来源与配置

github.com

配置方式待核实

请查看原始来源中的适用工具、依赖和权限要求。

安装实测待补充

happy-dom:在无浏览器环境下运行 DOM 测试的快速 JavaScript DOM 实现。适用于“无浏览器测试”、替代 jsdom、在 Vitest 或 Jest 中配置测试环境、进行服务端渲染(SSR)测试以及比较与 jsdom 的性能场景。

0 下载

介绍来源: third_party · TerminalSkills · 1.0.0 参考来源

SkillPi 解读

适合谁
翻译待完成,请参阅来源文档。
实测状态
未测试

文件浏览

在原站浏览文件 ↗

原始 SKILL.md

打开原文 ↗
Frontmatter
name: happy-dom
description: >-
  Run DOM tests without a browser using Happy DOM — fast JavaScript DOM
  implementation. Use when someone asks to "test without a browser", "Happy DOM",
  "fast DOM environment for tests", "replace jsdom", "Vitest DOM environment",
  or "server-side DOM". Covers test environment setup, Vitest/Jest integration,
  SSR testing, and performance comparison with jsdom.
license: Apache-2.0
compatibility: "Node.js 18+. Vitest, Jest, or standalone."
metadata:
  author: terminal-skills
  version: "1.0.0"
  category: development
  tags: ["testing", "dom", "happy-dom", "jsdom", "vitest"]

Happy DOM

Overview

Happy DOM is a JavaScript DOM implementation for Node.js — 3-10x faster than jsdom. It provides window, document, HTMLElement, and 1000+ Web APIs without launching a browser. Use it as the test environment for Vitest or Jest when testing UI components, or for server-side DOM manipulation (email templates, HTML generation, scraping).

When to Use

  • Test environment for React/Vue/Svelte component tests (faster than jsdom)
  • Vitest setup — Happy DOM is the recommended environment
  • Server-side HTML generation or manipulation
  • Parsing and extracting data from HTML strings
  • Email template rendering on the server

Instructions

Vitest Integration

npm install -D happy-dom
// vitest.config.ts — Use Happy DOM for all tests
import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    environment: "happy-dom",
  },
});

That's it — all tests now run with Happy DOM providing DOM APIs.

Per-File Environment

// tests/component.test.ts — Override environment for specific tests
// @vitest-environment happy-dom

import { render, screen } from "@testing-library/react";

test("renders heading", () => {
  render(<h1>Hello</h1>);
  expect(screen.getByRole("heading")).toHaveTextContent("Hello");
});

Jest Integration

// jest.config.js
module.exports = {
  testEnvironment: "@happy-dom/jest-environment",
};

Standalone Usage

// render.ts — Server-side DOM manipulation
import { Window } from "happy-dom";

const window = new Window({ url: "https://localhost:3000" });
const document = window.document;

// Parse HTML
document.body.innerHTML = `
  <div class="container">
    <h1>Hello World</h1>
    <ul id="items">
      <li>Item 1</li>
      <li>Item 2</li>
    </ul>
  </div>
`;

// Query and manipulate
const heading = document.querySelector("h1");
console.log(heading?.textContent); // "Hello World"

const items = document.querySelectorAll("#items li");
console.log(items.length); // 2

// Create elements
const newItem = document.createElement("li");
newItem.textContent = "Item 3";
document.getElementById("items")?.appendChild(newItem);

// Serialize back to HTML
console.log(document.body.innerHTML);

// Clean up
await window.happyDOM.close();

Email Template Rendering

// email.ts — Render email templates server-side
import { Window } from "happy-dom";

function renderEmailTemplate(template: string, data: Record<string, string>): string {
  const window = new Window();
  const document = window.document;

  document.body.innerHTML = template;

  // Replace placeholders
  for (const [key, value] of Object.entries(data)) {
    const elements = document.querySelectorAll(`[data-field="${key}"]`);
    elements.forEach((el) => { el.textContent = value; });
  }

  const html = document.body.innerHTML;
  window.happyDOM.close();
  return html;
}

Examples

Example 1: Speed up Vitest test suite

User prompt: "Our Vitest tests using jsdom take 45 seconds. Make them faster."

The agent will switch the test environment to Happy DOM (one config change), verify all tests still pass, and benchmark the improvement (typically 3-5x faster).

Example 2: Parse and extract data from HTML

User prompt: "Parse HTML pages and extract structured data without a browser."

The agent will use Happy DOM to parse HTML strings, query elements with CSS selectors, and return structured data.

Guidelines

  • One config line for Vitest — environment: "happy-dom" in vitest.config.ts
  • 3-10x faster than jsdom — measurable improvement on large test suites
  • 1000+ Web APIs — fetch, FormData, URL, MutationObserver, IntersectionObserver
  • Not 100% browser-identical — some edge cases differ; test critical paths in a real browser
  • window.happyDOM.close() — clean up resources in standalone usage
  • Works with Testing Library — @testing-library/react works unchanged
  • Lighter than jsdom — less memory, faster startup
  • SSR testing — render components server-side and assert on HTML output
  • Per-file override — // @vitest-environment happy-dom for specific tests

当前收录版本

1.0.0

核对来源版本 ↗

安全与评测信息

质量评测

满分100 · 来自 SkillPi LAB 实验室的评测报告。

场景价值92/100

亮点

  • Happy DOM 提供 3-10x 比 jsdom 更快的 DOM 测试性能
  • 覆盖 Vitest/Jest 集成、SSR 测试、服务端 DOM 操作等高频刚需场景
  • 包含完整的端到端示例和实际应用场景说明
触发精准90/100

亮点

  • 触发词覆盖全面:'无浏览器测试'、'Happy DOM'、'测试用的快速 DOM 环境'等
  • 边界清晰,无明显歧义或重叠
结构规范95/100

亮点

  • 结构清晰:Vitest/Jest 集成、独立使用、邮件模板渲染等模块分明
  • frontmatter 完整,包含兼容性、许可证、元数据等信息
  • 提供完整的端到端示例和使用指南
执行可靠90/100

亮点

  • 工具来源明确,配置参数正确
  • 包含资源清理机制(window.happyDOM.close())
  • 覆盖主要异常场景和最佳实践
证据可验 (前四项均值)92/100

取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。

正文静态检测

100 / 100 · skillpi-20261123-no-r3-v1

正文未命中当前规则。

分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。

权限与使用前检查

根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。

  • 安装或获取外部依赖

    使用官方来源并锁定版本,在隔离环境检查安装脚本。当前文档检查不覆盖后续下载的代码。

    主文档提及 · 第 37 行
文档引用的域名或地址

这些地址可能用于文档链接或示例,不是已批准的联网清单。使用前核对实际目的地及发送的数据。

  • localhost

本站展示提示不会授予权限。涉及文件改动、密钥、外部发送或费用时,仍由你使用的工具按需确认。

安全评级与访问范围

safe · 静态证据核验,不代表运行安全保证。

使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。