0Pricing
tRPC End-to-End Type Safe APIs · Lesson

Mocking Context and Dependencies in tRPC Tests

Isolate procedures from real databases and services by mocking the tRPC context, sessions, and external dependencies in your tests.

Why Mock the Context

tRPC procedures receive a context holding the db client, the user session, and other services. Real tests should not hit a real database. Mocking the context keeps tests fast and deterministic.

What Lives in Context

A typical context includes:

  • db — the database client
  • session — the authenticated user
  • headers — request metadata

Your createContext function builds this per request.

All lessons in this course

  1. Unit Testing tRPC Procedures
  2. Integration Testing tRPC Routers
  3. End-to-End Testing Strategies
  4. Mocking Context and Dependencies in tRPC Tests
← Back to tRPC End-to-End Type Safe APIs