gitmyhub

jsdom

JavaScript ★ 0 updated 11y ago ⑂ fork

A JavaScript implementation of the WHATWG DOM and HTML standards, for use with io.js

jsdom simulates a web browser inside Node.js, letting you load HTML, run JavaScript against it, and test pages without opening a real browser.

JavaScriptio.jssetup: moderatecomplexity 2/5

jsdom Explanation

jsdom is a tool that lets you run web pages inside a Node.js or io.js program, without needing a real browser. Think of it as a fake browser that exists entirely in JavaScript. You can load an HTML page—either from a URL, a file, or as a string of code—and then run JavaScript against it, manipulate the page, or check what's on it, all from your Node.js server.

The main use case is testing. If you're building a website or web app and want to automatically check that your pages work correctly, you can use jsdom to load the page in a simulated environment and verify that links exist, buttons are clickable, or that JavaScript does what you expect. For example, you could point it at a web page, load jQuery library into it, and then count how many links are on the page—all without opening a real browser.

jsdom works by parsing HTML into a fake document object (called a "DOM," which is the standard way web pages represent themselves). You can then interact with that document using the same methods and properties that real browsers support. You can load external JavaScript files into the fake page, run them, and see the results. The tool even supports cookies, so if you're testing something that relies on login state or other cookie-based data, you can simulate that too.

There are two main APIs. The "easy mode" version, called jsdom.env, handles most of the setup for you—you just give it a URL or HTML string and a callback function, and it figures out the rest. The "hardcore" version, jsdom.jsdom, gives you more control if you need it. By default, jsdom doesn't run external JavaScript for safety reasons (since code running inside could potentially access your server), but you can turn that on if you trust the code. The README notes it was originally written for io.js and requires that runtime instead of the older Node.js.

Where it fits