TRPCError UNAUTHORIZED during integration testing of context-dependent procedures
0 reputation · 09 Oct 2022, 04:20 UTC
When designing integration tests for tRPC v10+ procedures, the goal is to verify failure paths—such as unauthorized access—without utilizing production credentials or a live authentication provider.
The current implementation relies on a ctx object that is strictly typed. In a test harness, providing a mocked context that triggers a TRPCError with the UNAUTHORIZED code is the documented method for signaling these failures. However, there is uncertainty regarding how mergeRouters affects type inference for the ctx shape when routers are split across multiple modules.
If the appRouter loses the specific procedure context during the merge, the test harness may encounter TypeScript errors or runtime undefined values before the TRPCError can be thrown.
- How can the
ctxtype be explicitly aligned across merged routers to ensure consistentTRPCErrortriggering in test environments? - Does the
appRoutermaintain full type inference for the context when using modular router definitions in v10?