Skip to main content
OpenFront uses Vitest for testing, ensuring game logic stability and catching regressions early.

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 in vite.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/server

Test Scripts

Test Structure

Tests are organized in the /tests directory:

Writing Tests

Basic Test Structure

Testing Core Logic

All changes to /src/core MUST include tests. This is non-negotiable for game stability.
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

Core logic (/src/core) should aim for high test coverage (80%+ recommended).
Generate coverage reports:
This creates a coverage report in /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
Usage:

Mocking and Stubbing

Vitest provides mocking utilities:

Performance Testing

Run performance benchmarks:
This executes tests in /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

Ensure tsconfig.json paths are correctly set and Vitest is using vite-tsconfig-paths.
This usually indicates:
  • Non-deterministic code (missing seed)
  • Timing issues (use vi.useFakeTimers())
  • Shared state between tests (use beforeEach)
Delete the coverage directory and re-run:
Increase timeout for slow tests:

Architecture

Understand the system design

Setup Guide

Set up development environment

Contributing

Contribution guidelines