Integration Testing a DOM Application with jsdom

Integration Testing a DOM Application with jsdom

The Lesson

A browser-based application that uses DOM APIs (querySelector, innerHTML, addEventListener) can be integration-tested in Node.js using jsdom, without launching a real browser. This is faster than Playwright/Selenium and simpler to set up, but requires dependency injection to decouple the application from global DOM state.

Context

The quiz application's QuizApp class in app.js had 0% test coverage because it was tightly coupled to the browser DOM — it called document.getElementById() in its constructor. This couldn't be tested in Node.js as-is.

What Happened

  1. QuizApp was refactored to accept DOM element references via constructor parameters instead of querying the global document
  2. Integration tests created a jsdom document with the required HTML structure
  3. Tests exercised the full flow: load exam → render question → select answer → submit → verify feedback
  4. Test coverage for app.js went from 0% to meaningful coverage
  5. vitest with environment: 'jsdom' was configured in vitest.config.js

Key Insights

Applicability

jsdom integration testing works well for applications whose complexity is in DOM manipulation, event handling, and data flow — not in visual rendering, animations, or cross-browser layout differences. If your tests need to verify visual appearance, use Playwright or Cypress. If they need to verify logic and data flow through the DOM, jsdom is faster and simpler.

Related Lessons