jsdom

noun

say it: “JAY-ESS-dom” /ˈdʒeɪ ɛs dɒm/

A pretend browser page that runs inside Node, so tests can build a page, click its buttons and read its text without opening a browser.

What is jsdom in JavaScript?

jsdom — A browser's page model, rebuilt in JavaScript so it runs inside Node. Tests use it to build a page, click a button with dispatchEvent and read text with textContent, without opening a browser. Vitest turns it on with the jsdom environment.

// @vitest-environment jsdom
import { it, expect } from 'vitest';

it('shows the seats left', () => {
  document.body.innerHTML = '<p id="seats">Seats left: 3</p>';
  expect(document.querySelector('#seats').textContent).toBe('Seats left: 3');
});

In a sentence

  • “The test runs in jsdom, so it can click the Add to cart button and read the new total.”
  • “I added the comment // @vitest-environment jsdom at the top of the test file so document worked.”

Level: Freelancer · More JavaScript terms at this level

Updated