vitest.config.ts with environment-appropriate settings.
Vitest Setup
Root Configuration
The rootvitest.config.ts sets the baseline for all packages:
node — browser-dependent apps override this to jsdom.
Per-App Overrides
Each app customizes the base config for its runtime context:| App / Package | Environment | Key Overrides |
|---|---|---|
apps/api | node | fileParallelism: false, 15s timeout, Cloudflare module aliases |
apps/workbench | jsdom | @vitejs/plugin-react, @testing-library/jest-dom setup file |
apps/mobile | node | React Native module mocks in test/setup.ts |
packages/* | node | Minimal config, shared mergeV8Coverage preset |
API Worker Test Config
The API worker has the most complex config because it stubs Cloudflare-only modules:Workbench Test Config
The workbench (Vite SPA) usesjsdom and stubs all @openinsure/ui subpath imports:
setup.ts file provides @testing-library/jest-dom matchers and mocks window.matchMedia:
Running Tests
Test Data Conventions
Caution:
Use realistic insurance data in tests — never { foo: 'bar' }. Test data should reflect actual
policy numbers, NAICS codes, premium amounts, and claim descriptions.
API Tests: Mutable DB State Pattern
API route tests use avi.hoisted() pattern to create mutable stores that simulate database behavior:
Workbench Tests: UI Stub Pattern
The workbench stubs all@openinsure/ui components with minimal HTML equivalents. This avoids building the UI package for tests:
Integration Tests with Real DB
Some API tests support running against a real database whenTEST_DATABASE_URL is set:
Mocking Patterns
Cloudflare Bindings
The API worker tests mock Cloudflare-specific bindings (KV, Queues, R2, Durable Objects):Package Mocks
Domain packages are mocked at the module level to isolate route handler logic:Form Mocking (Workbench)
The workbench mocks@openinsure/form and @openinsure/react-query to test form behavior without real API calls:
Playwright E2E
Configuration
Each app with a UI has aplaywright.config.ts:
Coverage Collection
E2E tests collect V8 coverage via a custom Playwright fixture:Running E2E Tests
Coverage Enforcement
V8 Coverage Preset
All packages use a sharedmergeV8Coverage preset from vitest.coverage.preset.ts:
Coverage Exclusions (API Worker)
The API worker excludes Cloudflare-runtime code that cannot be tested in Node:- Queue handlers (require Cloudflare Queue bindings)
- Durable Objects (require Cloudflare DO runtime)
- External SDK wrappers (require live credentials)
- Workflow orchestrators (require Cloudflare Workflows runtime)
Required Coverage Paths
Financial, compliance, and state-transition code paths require 100% coverage:packages/billing/— premium calculation, installment planspackages/compliance/— filing deadlines, sanctions screeningpackages/policy/— state machine transitions, bind checklistspackages/rating/— rate table lookups, factor waterfalls
Test Organization
| Location | Type | Environment |
|---|---|---|
packages/<pkg>/__tests__/ | Unit tests for domain logic | node |
apps/api/src/__tests__/ | API route integration tests | node |
apps/workbench/src/__tests__/ | React component tests | jsdom |
apps/mobile/lib/__tests__/ | Validation, cache, auth store | node |
apps/<portal>/src/__tests__/ | Portal component tests | jsdom |
apps/<app>/e2e/ | Playwright E2E tests | Browser |
Test Count by Area
The API worker alone has 100+ test files covering routes, middleware, services, and integrations. Key test suites include:billing.test.ts— invoices, payments, commissionsclaims-lifecycle.test.ts— FNOL through settlementbind-workflow.test.ts— submission to bound policycompliance.test.ts— filing deadlines, sanctionsledger-client.test.ts— TigerBeetle double-entry operationspolicy-cancel.test.ts— cancellation state machine
Best Practices
-
Use
vi.clearAllMocks()inbeforeEach— but be aware it does not clearmockReturnValueOncequeues -
Use
vi.hoisted()for mutable test state that needs to be available beforevi.mock()calls -
Prefer
vi.mock()at the module level for package stubs; usevi.spyOn()for individual function assertions -
Always reset mutable stores (
dbStore,kvStore, etc.) inbeforeEach -
For async operations, use
waitFor()from@testing-library/reactrather than manual timeouts -
Import
describe,expect,it,vifromvitestexplicitly (not relying on globals in non-workbench packages)CI/CD Pipeline
How tests run in CircleCI: format, lint, typecheck, test, build, deploy.Local Development
Docker Compose services, environment setup, and dev server commands.