happy-dom
Happy DOMを使って、ブラウザなしで高速にDOMテストを実行し、jsdomの代替やVitest/Jestとの連携、SSRテストなど、様々なテスト環境構築を支援するSkill。
📜 元の英語説明(参考)
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.
🇯🇵 日本人クリエイター向け解説
Happy DOMを使って、ブラウザなしで高速にDOMテストを実行し、jsdomの代替やVitest/Jestとの連携、SSRテストなど、様々なテスト環境構築を支援するSkill。
※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。
下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o happy-dom.zip https://jpskill.com/download/14970.zip && unzip -o happy-dom.zip && rm happy-dom.zip
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/14970.zip -OutFile "$d\happy-dom.zip"; Expand-Archive "$d\happy-dom.zip" -DestinationPath $d -Force; ri "$d\happy-dom.zip"
完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。
💾 手動でダウンロードしたい(コマンドが難しい人向け)
- 1. 下の青いボタンを押して
happy-dom.zipをダウンロード - 2. ZIPファイルをダブルクリックで解凍 →
happy-domフォルダができる - 3. そのフォルダを
C:\Users\あなたの名前\.claude\skills\(Win)または~/.claude/skills/(Mac)へ移動 - 4. Claude Code を再起動
⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。
🎯 このSkillでできること
下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。
📦 インストール方法 (3ステップ)
- 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
- 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
- 3. 展開してできたフォルダを、ホームフォルダの
.claude/skills/に置く- · macOS / Linux:
~/.claude/skills/ - · Windows:
%USERPROFILE%\.claude\skills\
- · macOS / Linux:
Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。
詳しい使い方ガイドを見る →- 最終更新
- 2026-05-18
- 取得日時
- 2026-05-18
- 同梱ファイル
- 1
📖 Skill本文(日本語訳)
※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。
Happy DOM
概要
Happy DOM は Node.js 向けの JavaScript DOM 実装です。jsdom よりも 3〜10 倍高速です。ブラウザを起動せずに、window、document、HTMLElement、および 1000 以上の Web API を提供します。UI コンポーネントをテストする際に Vitest または Jest のテスト環境として使用したり、サーバーサイドの DOM 操作(メールテンプレート、HTML 生成、スクレイピング)に使用したりできます。
どのような時に使うか
- React/Vue/Svelte コンポーネントテストのテスト環境(jsdom よりも高速)
- Vitest のセットアップ — Happy DOM が推奨環境です
- サーバーサイドの HTML 生成または操作
- HTML 文字列の解析とデータ抽出
- サーバー上でのメールテンプレートのレンダリング
手順
Vitest との統合
npm install -D happy-dom
// vitest.config.ts — すべてのテストで Happy DOM を使用
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
environment: "happy-dom",
},
});
これだけです。これで、すべてのテストが DOM API を提供する Happy DOM で実行されます。
ファイルごとの環境
// tests/component.test.ts — 特定のテストの環境をオーバーライド
// @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 との統合
// jest.config.js
module.exports = {
testEnvironment: "@happy-dom/jest-environment",
};
スタンドアロンでの使用
// render.ts — サーバーサイドの DOM 操作
import { Window } from "happy-dom";
const window = new Window({ url: "https://localhost:3000" });
const document = window.document;
// HTML を解析
document.body.innerHTML = `
<div class="container">
<h1>Hello World</h1>
<ul id="items">
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
`;
// クエリと操作
const heading = document.querySelector("h1");
console.log(heading?.textContent); // "Hello World"
const items = document.querySelectorAll("#items li");
console.log(items.length); // 2
// 要素を作成
const newItem = document.createElement("li");
newItem.textContent = "Item 3";
document.getElementById("items")?.appendChild(newItem);
// HTML にシリアライズして戻す
console.log(document.body.innerHTML);
// クリーンアップ
await window.happyDOM.close();
メールテンプレートのレンダリング
// email.ts — サーバーサイドでメールテンプレートをレンダリング
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;
// プレースホルダーを置換
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;
}
例
例 1: Vitest テストスイートの高速化
ユーザープロンプト: 「jsdom を使用した Vitest テストに 45 秒かかります。もっと速くしてください。」
エージェントはテスト環境を Happy DOM に切り替え(1 つの設定変更)、すべてのテストが引き続き合格することを確認し、改善をベンチマークします(通常は 3〜5 倍高速)。
例 2: HTML からデータを解析して抽出する
ユーザープロンプト: 「ブラウザなしで HTML ページを解析し、構造化されたデータを抽出します。」
エージェントは Happy DOM を使用して HTML 文字列を解析し、CSS セレクターで要素をクエリし、構造化されたデータを返します。
ガイドライン
- Vitest の設定は 1 行 — vitest.config.ts の
environment: "happy-dom" - jsdom より 3〜10 倍高速 — 大規模なテストスイートで測定可能な改善
- 1000 以上の Web API —
fetch、FormData、URL、MutationObserver、IntersectionObserver - 100% ブラウザと同一ではない — 一部のエッジケースは異なります。重要なパスは実際のブラウザでテストしてください
window.happyDOM.close()— スタンドアロンで使用する場合はリソースをクリーンアップ- Testing Library で動作 —
@testing-library/reactは変更なしで動作 - jsdom より軽量 — メモリ消費量が少なく、起動が高速
- SSR テスト — コンポーネントをサーバーサイドでレンダリングし、HTML 出力をアサート
- ファイルごとのオーバーライド — 特定のテストには
// @vitest-environment happy-dom
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開
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/reactworks 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-domfor specific tests