oRPC Testing & Mocking

Test oRPC procedures using server-side clients, and mock implementations for frontend testing.

SKILL.md

GitHub

Testing & Mocking

Strategies for testing and mocking oRPC routers and procedures.

Testing

Use Server-Side Clients to invoke procedures directly in tests, no additional setup required:

import { call } from '@orpc/server'

it('works', async () => {
  await expect(
    call(router.planet.list, { page: 1, size: 10 })
  ).resolves.toEqual([
    { id: '1', name: 'Earth' },
    { id: '2', name: 'Mars' },
  ])
})

You can also use the Fetch API to create production-like clients. See Optimize SSR.

Mocking

The Implementer is designed for contract-first development, but it can also create alternative versions of routers/procedures for testing.

import { implement, unlazyRouter } from '@orpc/server'

const fakeListPlanet = implement(router.planet.list).handler(() => [])

Use fakeListPlanet to replace the actual listPlanet implementation during tests.

implement is also useful for creating mock servers for frontend testing scenarios.

implement doesn't yet support lazy routers — use the unlazyRouter utility to convert before implementing.