feat(tests): add tRPC router test harness helpers#1000
Merged
Conversation
nhopeatall
approved these changes
Mar 23, 2026
Collaborator
nhopeatall
left a comment
There was a problem hiding this comment.
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 exactfunction createCaller(ctx) { return xRouter.createCaller(ctx); }pattern — confirmed ✓setupOwnershipCheckMock: 7 test files manually wire the identicalmockDbSelect/mockDbFrom/mockDbWherechain — confirmed ✓expectTRPCError: 261 occurrences oftoBeInstanceOf(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. setupOwnershipCheckMockcomplements (rather than duplicates) the existingcreateMockDbhelper intests/helpers/mockDb.ts:createMockDbis a general-purpose Drizzle chain mock, while this new helper specifically targets the narrow ownership-check pattern without wiringselect→from→where(callers still do that inbeforeEach), which is the correct layering.- The
TRPCErrorCodeunion 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. expectTRPCErrorusingrejects.toSatisfyis valid (toSatisfyis a first-class Vitest matcher from@vitest/expect). When nestedexpectcalls insidetoSatisfythrow, 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
tests/helpers/trpcTestHarness.tswith three helpers that eliminate the most repeated boilerplate in API router testscreateCallerFor(router)— generic caller factory replacing 15 per-filefunction createCaller(ctx) { return fooRouter.createCaller(ctx); }duplicatessetupOwnershipCheckMock()— returns{ mockDbSelect, mockDbFrom, mockDbWhere, configureOwnership }pre-wiring theselect → from → whereDrizzle chain that 6+ test files set up identically;configureOwnership(orgId)simulates project belonging to an orgexpectTRPCError(promise, code)— assertion helper replacing 30+ try/catch blocks orrejects.toMatchObject({ code })patternsvi.hoisted()pattern as the preferred replacement for the(...args: unknown[]) => mockFn(...args)anti-pattern present in 120+ places across API router testsTest plan
npm testpasses — all 332 test files, 6326 tests green (zero regressions, additive only)npm run lintpasses — no lint errorsnpm run typecheckpasses — no type errorsCard
https://trello.com/c/69c166e2cc53ab34b4bb6d42
🤖 Generated with Claude Code
🕵️ claude-code · claude-sonnet-4-6 · run details