Overview
OpenFront’s testing strategy focuses on:Core Logic
Deterministic simulation must be thoroughly tested
Game Mechanics
All gameplay features need test coverage
Regression Prevention
Tests prevent breaking existing functionality
Client Rendering
UI components and rendering logic
Test Framework
OpenFront uses Vitest, a fast unit test framework with:- Native ESM support
- TypeScript support out of the box
- Jest-compatible API
- Fast watch mode
- Coverage reports via v8
Configuration
Vitest is configured invite.config.ts:
vite.config.ts
Running Tests
Basic Commands
The
npm test command runs both core tests and server tests: vitest run && vitest run tests/serverTest Scripts
Test Structure
Tests are organized in the/tests directory:
Writing Tests
Basic Test Structure
Testing Core Logic
Example core test:tests/Attack.test.ts
Testing Determinism
Determinism is critical for OpenFront’s architecture:1
Use Seeded Random
Always use seeded random number generators:
2
Test Identical Results
Run the same operation twice with the same seed:
3
Test Different Seeds
Verify different seeds produce different results:
Testing Client Components
For UI components using Lit:tests/client/ui/Button.test.ts
Testing Server Logic
Server tests are in/tests/server:
tests/server/Lobby.test.ts
Coverage Requirements
Generate coverage reports:/coverage:
Coverage Metrics
Focus on meaningful coverage over arbitrary percentages. A well-tested feature at 70% coverage is better than superficial tests at 90%.
Testing Best Practices
1. Test Behavior, Not Implementation
2. Use Descriptive Test Names
3. Test Edge Cases
4. Keep Tests Isolated
5. Use Factories for Test Data
tests/util/factories.ts
Mocking and Stubbing
Vitest provides mocking utilities:Performance Testing
Run performance benchmarks:/tests/perf using the Benchmark.js library:
tests/perf/pathfinding.perf.ts
Continuous Integration
OpenFront uses GitHub Actions for CI:.github/workflows/ci.yml
All PRs must pass CI checks before being merged.
Common Testing Patterns
Testing Alliances
tests/AllianceSystem.test.ts
Testing Game Tick Execution
Debugging Tests
Using VS Code
Add to.vscode/launch.json:
Console Logging
Troubleshooting
Tests fail with module resolution errors
Tests fail with module resolution errors
Ensure
tsconfig.json paths are correctly set and Vitest is using vite-tsconfig-paths.Flaky tests (sometimes pass, sometimes fail)
Flaky tests (sometimes pass, sometimes fail)
This usually indicates:
- Non-deterministic code (missing seed)
- Timing issues (use
vi.useFakeTimers()) - Shared state between tests (use
beforeEach)
Coverage not updating
Coverage not updating
Delete the coverage directory and re-run:
Tests timeout
Tests timeout
Increase timeout for slow tests:
Related Documentation
Architecture
Understand the system design
Setup Guide
Set up development environment
Contributing
Contribution guidelines