jsdom emulates browser DOM and HTML APIs in Node.js for testing and scraping real web apps. The JSDOM constructor parses HTML and exposes a window object with configurable behavior.
Overview
jsdom is a JavaScript implementation of many web standards—chiefly the WHATWG DOM and HTML—for Node.js. The stated goal is to emulate enough of a browser subset to be useful when testing or scraping applications that expect a DOM.
The main entry point is the JSDOM class: pass HTML (or load from URL or file) and receive a document with window, standard DOM APIs, and options for URL, referrer, storage quotas, and parser location metadata. By default it parses like a browser, including implied html, head, and body structure.
Script execution inside parsed pages is optional and off by default because embedded scripts can reach the Node.js host if enabled with runScripts set to dangerously. Separate modes allow outside-only script globals or loading external scripts when resource loading is configured. jsdom can fetch subresources (frames, stylesheets, scripts, images with optional canvas) when resources are set to usable.
The environment behaves as headless by default (document.hidden true) unless pretendToBeVisual is set. Virtual consoles forward or capture page and implementation logs. Custom resource loaders, user agents, and request interceptors support controlled fetching for tests.
Key features
- WHATWG DOM and HTML in Node.js
- JSDOM constructor with window API
- Optional in-page script execution
- Configurable URL and referrer
- Subresource loading options
- Virtual console forwarding
- fromURL and fromFile helpers
- Headless default with visual pretend mode
Use cases
- Unit tests for browser-oriented code
- Server-side HTML parsing
- Scraping with DOM APIs
- Simulating pages without a browser
- Controlled script execution in tests