Skip to content

jsdom

In one line

jsdom is a fake DOM implemented in JavaScript — it lets you test DOM code in Node.js without launching a real browser.

In simple words

jsdom is a JavaScript library that simulates the DOM (Document Object Model) in Node.js. It implements enough of the HTML and DOM standards that you can test browser code without a real browser.

With jsdom, you get document, window, navigator, and other browser globals in your Node.js test environment. You can create elements, modify the DOM, dispatch events, and query elements — all in fast, headless Node.js.

Jest uses jsdom by default when you set testEnvironment: 'jsdom'. This is how Jest can test React components (which need a DOM) without launching Chrome.

Limitations: jsdom doesn't do actual layout or rendering (no visual output), doesn't support some browser APIs (like canvas, IntersectionObserver), and has no CSS layout engine. For those, you need a real browser.

Example

javascript
// jsdom simulates the DOM in Node.js (no browser needed)

import { JSDOM } from 'jsdom';

const dom = new JSDOM('<!DOCTYPE html><div id="app"></div>');
const document = dom.window.document;

// Now you can manipulate the DOM as if in a browser:
const app = document.getElementById('app');
const btn = document.createElement('button');
btn.textContent = 'Click me';
btn.addEventListener('click', () => {
  btn.textContent = 'Clicked!';
});
app.appendChild(btn);

// Simulate a click
btn.click();
console.log(btn.textContent); // 'Clicked!'

// Vitest/Jest use jsdom automatically in test files

jsdom creates a fake DOM in Node.js so you can run DOM-dependent tests without a browser. It implements enough of the DOM API (createElement, addEventListener, etc.) for most unit tests.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary