7.3 JavaScript Tests (Jest)
This repository runs JavaScript unit tests for frontend code using Jest.
What is covered
- Test runner: Jest (Node.js)
- Environment:
jsdom(browser-like DOM) - Test location:
tests/js/ - Coverage: Collected from modules in
static/js/componentsandstatic/js/pages.
How to run locally
Host Node.js (Recommended)
Run tests with the same dependencies as CI/CD:
npm ci
npm test -- --coverage --ci
Optional: ephemeral Node container
If you don't want to install Node on your host:
docker run --rm -v "$PWD":/work -w /work node:20 \
sh -lc "npm ci && npm test -- --coverage --ci"
How CI runs
The GitHub Actions workflow uses Node.js 20 and executes:
npm ci
npm test -- --coverage --ci
A coverage artifact is uploaded as js-coverage-report.
Configuration files
| File | Purpose |
|---|---|
package.json |
Jest config, scripts, and devDependencies. |
package-lock.json |
Pinned dependency tree used by npm ci. |
tests/js/setup.js |
Global mocks for browser-only dependencies (Leaflet, app globals). |
Test environment setup
Frontend code expects browser globals and libraries that are not available in Node. Jest uses a setup file (tests/js/setup.js) to provide minimal mocks.
The setup file defines mocks for:
- Leaflet (
global.L) - Application constants (
global.AppConstants)
If a test fails with errors such as L is not defined, the setup file is the first place to check.