Skip to content

Run unit tests in a jsdom environment via Vitest#150

Open
CarsonDavis wants to merge 3 commits into
developmentfrom
tests-jsdom
Open

Run unit tests in a jsdom environment via Vitest#150
CarsonDavis wants to merge 3 commits into
developmentfrom
tests-jsdom

Conversation

@CarsonDavis

@CarsonDavis CarsonDavis commented Jun 16, 2026

Copy link
Copy Markdown

What this does

Makes the unit test suite actually run. Right now it doesn't — and it never has in CI — so a large set of written unit tests (~200 cases) provides zero protection. This migrates the unit suite to Vitest with a jsdom (simulated-browser) environment so those tests can finally execute. Playwright keeps the end-to-end suite.

Implements #148.

Why the tests weren't running

Many unit specs test browser-coupled code — the map-engine adapters, the engine registry, the panel manager. That code reaches for browser globals like window the moment it loads. Unit tests run in plain Node, which has no browser, so these specs crashed at import — before a single assertion — which aborted the entire unit run.

This stayed invisible for a long time:

  • CI has never had a passing run. The test job was failing at an earlier step and never reached the tests, so nothing ever surfaced the crashes — there was no green build to measure against.
  • The breakage accumulated quietly. Some of these tests ran fine when first written, then silently broke later when an unrelated change added a browser-coupled import to the code they pull in. Others needed a browser from the start. With nothing running them, the rot went unnoticed.

The fix is to give the unit tests a simulated DOM (jsdom), which Vitest provides out of the box.

Changes

  • Add vitest + jsdom. New vitest.config.ts (jsdom environment, an alias for the build-generated src/pre/tools.js, a setup file), tests/unit/vitest.setup.js (an import-time window.L stub), and a shared mmgisAPI test stub.
  • Swap the import in all 38 tests/unit/**/*.spec.js from @playwright/test to vitest (one line per file — the test/expect API is the same).
  • Mock mmgisAPI in the panel-manager specs so they don't drag in the entire map-rendering stack just to test panel logic.
  • Scope Playwright to tests/e2e. npm test still runs the full suite (Vitest unit, then Playwright e2e); npm run test:unit runs just the fast unit half (test:unit:watch for the dev loop).
  • CI: bump Node 18 → 20, and run the (hermetic) Vitest unit step before the e2e-only setup so it actually gates.

Result

660 passed / 9 failed / 669 total — previously the suite couldn't get past import. All 38 spec files now execute, including the ~200 previously-dead browser-dependent cases.

⚠️ Notes for reviewers

The browser-coupled unit specs (map-engine adapters, engine registry, panel manager) read window/document at import time, so they errored under plain Node and aborted the whole Playwright unit run — the suite gave zero protection. Run the unit suite under Vitest with a jsdom environment instead; Playwright keeps the e2e suite.

- Add vitest + jsdom; vitest.config.ts (jsdom env, alias for generated pre/tools), tests/unit/vitest.setup.js (import-time window.L stub), tests/unit/__mocks__/preTools.js

- Swap tests/unit import specifier @playwright/test -> vitest (38 files)

- Mock mmgisAPI in the panelManager specs to cut the heavy Map_ import chain

- Scope Playwright to tests/e2e; point npm test/test:unit at Vitest

- CI: Node 18 -> 20; run the hermetic unit step via Vitest before e2e setup

9 pre-existing stale tests are revealed by finally running the suite; they are tracked and fixed in #149. Refs #148, #149.
@CarsonDavis CarsonDavis self-assigned this Jun 16, 2026
@github-actions

github-actions Bot commented Jun 16, 2026

Copy link
Copy Markdown

✅ Version Already Updated

This PR includes a manual version update to 4.2.11-20260611

No automatic version bump needed.

Replaces the repeated inline vi.mock factory in the 5 panelManager specs with Vitest's adjacent __mocks__ automock, so each spec is a single vi.mock() line. No behavior change (660 pass / 9 pre-existing fail).
Comment thread package.json Outdated
"build:themes": "bash scripts/build-themes.sh",
"test": "playwright test",
"test:unit": "cross-env PLAYWRIGHT_TEST_UNIT_ONLY=true playwright test tests/unit",
"test": "vitest run",

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we changing the behavior here? i think we should be running playwright and vitest

Preserves the prior 'npm test runs everything' behavior and matches the AGENTS.md description; test:unit runs just the fast unit suite.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant