Skip to content
Agents tracked: 506 Releases (7d): 634 Agent status: 3 with issuesSince last limit reset: Claude 15d · Codex 0d For AI agents: Markdown Updated Nov 26, 2026

jsdom

A browser page inside Node.js, for tests.

TestingNPM

GitHubWebsite

1. License

“The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.”

Keep the MIT copyright and license notice with the package files you ship.

github.com/jsdom/jsdom/blob/main/LICENSE.txt

From the package registry and GitHub, checked daily; card checked Nov 11, 2026. Tap a verdict to see its source.How we checkedReport an error

What jsdom does

jsdom is a free JavaScript library that builds a web page in memory inside Node.js, without opening a browser. Tests can fill forms, click buttons and read the page, and scripts can pull text and links out of HTML. It does not draw anything, so sizes and positions on screen are not real.

What people build with it

Front-end tests without a browser

Your test runner checks that a sign-up form shows "Thanks!" after a valid email, in a fake page that starts in milliseconds.

Pull data out of saved web pages

A script reads product names and prices from HTML files you downloaded, using the same selectors as in a browser.

Clean HTML from users on the server

Comments or posts with formatting go through DOMPurify on your server, which needs a page object that jsdom provides.

Edit a piece of HTML in code

Your build step adds attributes to links or removes empty paragraphs in an HTML snippet before saving it.

Check generated emails before sending Idea

A test opens each HTML email your app builds and checks that the unsubscribe link and the right name are there.

Test widgets for the page title or focus Idea

Code that changes the page title, focus or stored settings is checked in automated runs without a real window.

jsdom popularity

83 / 100
Gid Score, #51 overall
167M
downloads a week, npm
21.8K
GitHub stars
615
agent projects use it
jsdom: downloads per weeknpm, jsdom 166,771,354
jsdom: GitHub starsjsdom/jsdom 21,765

Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated

How jsdom is used

  • jsdom is a dependency in 615 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (12.6% of the 4,867 JavaScript/TypeScript ones), #4 of 122 in testing.
  • Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 1,293; those are left out of every share on this page.
  • The most used alternative in our scan is Happy DOM (122 projects).
  • Projects that use it most often also use Vitest (88.3%), Testing Library (67.5%), Vite (68.6%).
  • It is more common in projects set up for Gemini CLI (16.2%) than for OpenClaw (9.0%).

By coding agent

369 projects, 11.3%
323 projects, 10.6%
246 projects, 10.1%
77 projects, 16.2%
70 projects, 14.9%
42 projects, 12.2%
34 projects, 12.2%
30 projects, 9.0%

Often used with

In 88.3% of its projects
In 67.5% of its projects
In 68.6% of its projects
In 89.3% of its projects
In 68.0% of its projects
In 66.8% of its projects
In 49.6% of its projects
In 71.1% of its projects

jsdom in practice

Strengths

HTML is parsed the way a browser parses it

The README says jsdom parses the HTML you give it just like a browser does. That includes the implied <code>&lt;html&gt;</code>, <code>&lt;head&gt;</code> and <code>&lt;body&gt;</code> tags, so a fragment ends up as a full document.

Three levels of script execution

By default, scripts in the page do not run. <code>runScripts: "outside-only"</code> installs fresh copies of the JavaScript globals on <code>window</code>, so you can run code against the page through <code>window.eval</code>. The README says this mode is "turned off by default for performance reasons, but is safe to enable." <code>runScripts: "dangerously"</code> also runs the page's own <code>&lt;script&gt;</code> elements.

Network requests can be intercepted

The <code>resources</code> option accepts undici interceptors and a custom dispatcher. jsdom's <code>requestInterceptor()</code> helper can inspect a request or return a synthetic <code>Response</code>. The README's example uses it to replace the contents of one specific script.

Console output you can capture

A virtual console receives both in-page <code>window.console</code> calls and jsdom's own warnings. By default all of it goes to the Node.js console. You can instead attach listeners per console method, or listen for the <code>"jsdomError"</code> event.

Watch out for

The sandbox is not a security boundary

The maintainers write that code inside a page's <code>&lt;script&gt;</code> elements can, if it tries hard enough, reach the Node.js environment and your machine. Use <code>runScripts: "dangerously"</code> only with code you trust.

No layout or rendering

jsdom acts like a headless browser. <code>pretendToBeVisual: true</code> changes <code>document.hidden</code> and enables <code>requestAnimationFrame</code>. The README stresses that it still does not do any layout or rendering.

Subresources are off, and the default URL breaks relative paths

Scripts, stylesheets, images and iframes do not load unless you set <code>resources: "usable"</code>. The default <code>url</code> is <code>"about:blank"</code>, so resources with relative URLs fail to load until you set a real URL. Images also require the <code>canvas</code> npm package.

Opting in to running page scripts

const dom = new JSDOM(`<body>
  <div id="content"></div>
  <script>document.getElementById("content").append(document.createElement("hr"));</script>
</body>`, { runScripts: "dangerously" });

// The script will be executed and modify the DOM:
console.log(dom.window.document.getElementById("content").children.length); // 1

More about jsdom

Funding
  • Tidelift: https://tidelift.com/funding/github/npm/jsdom

See all

Head to head
Packages around it
  • vitest: Test runner
  • @testing-library: DOM testing queries
  • vite: Build tool
  • @playwright/test: Real-browser tests
  • typescript: Typed JavaScript
  • eslint: Linter
More in Testing

Alternatives

jsdom This page

MITGid 83167M/wk

Happy DOM

Pick Happy DOM when test speed matters more than full browser coverage; Vitest calls it faster but missing some APIs.

MITGid 7137.8M/wk

Cheerio

Pick Cheerio when you only need to read or change HTML with selectors and never run page scripts or events.

Playwright (JavaScript)

Pick Playwright when the test needs a real browser with layout, screenshots or page navigation.

Compare jsdom with another tool

Get started

Tell your AI assistant

“Use the npm package jsdom (pin jsdom@31.0.0, Node.js 22.22.2+ or 24.15.0+) to load HTML with new JSDOM(html) and query it with window.document; keep runScripts off for HTML you did not write, and call window.close() when done.”

  1. Add jsdom to your project (it needs Node.js 22.22.2+, 24.15.0+ or 26+).

    npm install jsdom

    From github.com/jsdom/jsdom/blob/main/README.md

  2. Create a page from an HTML string and use window.document like in a browser.

    const jsdom = require("jsdom");
    const { JSDOM } = jsdom;
    
    const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
    console.log(dom.window.document.querySelector("p").textContent); // "Hello world"
    

    From github.com/jsdom/jsdom/blob/main/README.md

Rules for your agent

Paste into AGENTS.md or CLAUDE.md
## jsdom
- Create documents with `new JSDOM(html, options)` and work through `dom.window` / `dom.window.document`.
- Do not set `runScripts: "dangerously"` for user-supplied or Internet content. The sandbox is not foolproof. Use `runScripts: "outside-only"` with `window.eval` when you only need to run code from outside.
- Do not mash jsdom into Node globals (e.g. `global.window = dom.window`). Run DOM-dependent scripts and tests inside the jsdom environment.
- When loading subresources, set `resources: "usable"` and a real `url`. With the default `about:blank`, relative URLs fail to load. External scripts also need `runScripts: "dangerously"`.
- Do not rely on layout or rendering. Use `pretendToBeVisual: true` only when code needs `requestAnimationFrame` or a visible `document.visibilityState`.
- Set up `VirtualConsole` listeners before calling `new JSDOM()`, because errors can occur during parsing.
- Interceptors and dispatchers are ignored for synchronous `XMLHttpRequest` inside the page.

Mistakes to avoid

new JSDOM(htmlFromTheInternet, { runScripts: 'dangerously' })
Keep the default (no scripts) for HTML you did not write; use a real browser in a sandbox if page scripts must run

Page scripts can escape jsdom and run code on your machine.

global.window = dom.window; global.document = dom.window.document; then running tests in Node's global scope
Use the test runner's jsdom environment (Vitest environment 'jsdom', Jest jest-environment-jsdom) or run code inside the jsdom window

The jsdom docs advise against mixing jsdom and Node.js globals.

Asserting on getBoundingClientRect(), offsetTop or visibility in jsdom tests
Test layout and visual results with a real browser (Playwright); keep jsdom tests to content and behavior

jsdom does no layout; in our run getBoundingClientRect().width was 0.

npm install canvas to get canvas and image support with jsdom 31
npm install @napi-rs/canvas@^1.0.13 next to jsdom

jsdom 31 switched the optional backend; the old canvas package alone no longer enables it.

Reading the document right after new JSDOM(html) when it has scripts that load files
Wait for DOMContentLoaded (or load), checking document.readyState first

Since version 31, parsing pauses at blocking scripts, so the constructor can return a partly built document.

Questions

Will scripts inside my HTML run?

Not by default. Pass runScripts 'dangerously' to run them, and only for HTML you trust.

Can it take screenshots or measure element sizes?

No. It does no layout or drawing; use a real browser such as Playwright for that.

Can it open a live web address?

Yes, JSDOM.fromURL() fetches the page and follows redirects; cookies from the response go into its cookie jar.

Why does my Node.js process not exit after the test?

Timers started inside the page keep it running. Call window.close() to stop them.

Does it work with Vitest and Jest?

Yes. Vitest has a built-in jsdom environment, and Jest ships it in the jest-environment-jsdom package.