Skip to content

Testing

Running Tests

bash
# All tests
pnpm test

# Specific file
pnpm test apps/backend/test/durables/PriceCollector.spec.ts

# Filter by name
pnpm test apps/backend/test/durables/PriceAlert.spec.ts -t 'should evaluate alerts'

# Watch mode
pnpm test --watch

# Web API tests
pnpm web:test:run -t "should return portfolio state with valid auth"

Cross-DO Integration Tests

When testing cross-DO callbacks (e.g., PriceAlert triggering UserPortfolio):

  1. Initialize via RPC first: Always call getPortfolioState(stub, userId) before other operations to ensure account exists in SQLite
  2. Use withMockedTime(): For timestamp-dependent operations (openLimitOrder, openPosition use Date.now() internally)
  3. Inject price data at PAST timestamps: Alarm uses Date.now() as endCursor, so future timestamps won't be processed
  4. Set cursor to match data range: startCursor = lastCheckedTimestamp + 1
  5. Avoid direct internal method access: runInDurableObject state changes don't persist to SQLite — use RPC methods for cross-DO visible state

Cloudflare-Specific Testing

See the Cloudflare Testing Guide for detailed patterns around Durable Object testing, miniflare, and worker test utilities.