Skip to content

feat(tests): add tRPC router test harness helpers#1000

Merged
aaight merged 1 commit intodevfrom
feature/trpc-router-test-harness
Mar 23, 2026
Merged

feat(tests): add tRPC router test harness helpers#1000
aaight merged 1 commit intodevfrom
feature/trpc-router-test-harness

Conversation

@aaight
Copy link
Copy Markdown
Collaborator

@aaight aaight commented Mar 23, 2026

Summary

  • Creates tests/helpers/trpcTestHarness.ts with three helpers that eliminate the most repeated boilerplate in API router tests
  • createCallerFor(router) — generic caller factory replacing 15 per-file function createCaller(ctx) { return fooRouter.createCaller(ctx); } duplicates
  • setupOwnershipCheckMock() — returns { mockDbSelect, mockDbFrom, mockDbWhere, configureOwnership } pre-wiring the select → from → where Drizzle chain that 6+ test files set up identically; configureOwnership(orgId) simulates project belonging to an org
  • expectTRPCError(promise, code) — assertion helper replacing 30+ try/catch blocks or rejects.toMatchObject({ code }) patterns
  • JSDoc documents vi.hoisted() pattern as the preferred replacement for the (...args: unknown[]) => mockFn(...args) anti-pattern present in 120+ places across API router tests

Test plan

  • npm test passes — all 332 test files, 6326 tests green (zero regressions, additive only)
  • npm run lint passes — no lint errors
  • npm run typecheck passes — no type errors
  • No existing test files modified

Card

https://trello.com/c/69c166e2cc53ab34b4bb6d42

🤖 Generated with Claude Code

🕵️ claude-code · claude-sonnet-4-6 · run details

Copy link
Copy Markdown
Collaborator

@nhopeatall nhopeatall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

LGTM — Clean, well-documented test helper utilities that accurately target the most repeated boilerplate patterns across the API router test suite.

Verification of claims:

  • createCallerFor: 17 test files contain the exact function createCaller(ctx) { return xRouter.createCaller(ctx); } pattern — confirmed ✓
  • setupOwnershipCheckMock: 7 test files manually wire the identical mockDbSelect/mockDbFrom/mockDbWhere chain — confirmed ✓
  • expectTRPCError: 261 occurrences of toBeInstanceOf(TRPCError) or the (...args) => mockFn(...args) wrapper across 62 files — confirmed ✓
  • No existing test files modified — confirmed ✓
  • All CI checks passing — confirmed ✓

Design observations:

  • Correctly avoids centralizing vi.mock() calls (which must remain in consumer files due to hoisting) and instead exposes composable building blocks — this is the right trade-off for Vitest's architecture.
  • setupOwnershipCheckMock complements (rather than duplicates) the existing createMockDb helper in tests/helpers/mockDb.ts: createMockDb is a general-purpose Drizzle chain mock, while this new helper specifically targets the narrow ownership-check pattern without wiring select→from→where (callers still do that in beforeEach), which is the correct layering.
  • The TRPCErrorCode union type is manually maintained rather than derived from @trpc/server's types — acceptable for a test helper since it acts as documentation, and any mismatch would surface as a TypeScript error at the consumer site.
  • expectTRPCError using rejects.toSatisfy is valid (toSatisfy is a first-class Vitest matcher from @vitest/expect). When nested expect calls inside toSatisfy throw, Vitest propagates the assertion error with full context, so test failures remain readable.
  • Purely additive with zero consumers — no risk to existing tests. Migration tracked separately.

🕵️ claude-code · claude-opus-4-6 · run details

@aaight aaight merged commit 2fafadc into dev Mar 23, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants