Test setup, commands, views, events, and complete game flows.
Test the executor your game exports. This covers input schemas, validation, execution, stage transitions, events, and visibility together.Add Vitest when you are ready to write engine tests:
npm install --save-dev --workspace=engine vitestnpm pkg set scripts.test="vitest run" --workspace=enginenpm test --workspace=engine
Test one command
import { expect, test } from "vitest";import { executor } from "../src/game.ts";test("the active player advances", () => { const initial = executor.createInitialState({ seed: "test-seed", players: ["p1", "p2"], }); const result = executor.executeCommand(initial, { type: "advance", actorId: "p1", input: { spaces: 2 }, }); expect(result.ok).toBe(true); expect(result.state.game.positions.p1).toBe(2); expect(initial.game.positions.p1).toBe(0);});
Use a fixed seed in every test. A failure then reproduces the same shuffles and rolls.
Test rejection
test("a player cannot move out of turn", () => { const state = executor.createInitialState({ seed: "test-seed", players: ["p1", "p2"], }); const result = executor.executeCommand(state, { type: "advance", actorId: "p2", input: { spaces: 2 }, }); expect(result).toMatchObject({ ok: false, reason: "not_active_player", state, events: [], });});
Also test every reason returned by your own .validate() callbacks.
Test a complete scenario
runScenario applies a sequence and collects every result:
The argument after the command array is the same init object you pass to createInitialState. Add setup to it when your definition declares setup input.
Test hidden information
Create views for at least the owner, another player, and a spectator: