Testing
Test Suites
| Suite | Location | Runner | Count |
|---|---|---|---|
| Protocol schemas | packages/protocol/src/__tests__/ | Vitest | 47 |
| Common components | packages/common/src/__tests__/ | Vitest + jsdom | 129 |
| Rust integration | crates/klaxon-server/tests/ | cargo test | 4 |
Total: 180 tests
Running Tests
bash
# Everything
pnpm test
# TypeScript only
pnpm test:ts
# Rust only (requires DATABASE_URL)
pnpm test:rust
# Single file
pnpm --filter @ottercoders/klaxon-common exec vitest run src/__tests__/FormField.test.tsxMocking Pattern
Component tests mock the api transport layer:
tsx
import { invoke, listen } from "../api";
vi.mock("../api", () => ({
invoke: vi.fn(),
listen: vi.fn(() => Promise.resolve(() => {})),
}));
const mockInvoke = vi.mocked(invoke);
beforeEach(() => {
mockInvoke.mockResolvedValue(undefined);
});The invoke mock is then configured per-test to return the expected data:
tsx
mockInvoke.mockImplementation(async cmd => {
if (cmd === "klaxon_list_open") return { items: [testItem] };
if (cmd === "settings_get") return defaultSettings;
});MainWidget Mock Setup
MainWidget.test.tsx has a central setupInvokeMock() that handles all commands the widget calls. When adding a new command, add a handler here or tests fail.
Rust Integration Tests
Integration tests in crates/klaxon-server/tests/integration.rs run against a real PostgreSQL instance:
bash
DATABASE_URL=postgres://klaxon:klaxon@localhost:5432/klaxon cargo testThey validate:
- All 18 tables exist after migration
- Enum types have correct values
- Item CRUD lifecycle works
- Session token hashing is deterministic
Tests skip gracefully when DATABASE_URL is not set.
CI
The GitHub Actions workflow (.github/workflows/cloud.yml) runs:
pnpm typecheck— all packagespnpm test:ts— protocol + common testscargo check+cargo clippy+cargo fmtcargo sqlx migrate run— validates migrations against fresh Postgrescargo test— integration tests against Postgres service container