Testing & Mocking
Testing and mocking are essential parts of the development process, ensuring your oRPC routers and procedures work as expected. This guide covers strategies for testing and mocking in oRPC applications.
Testing
Using Server-Side Clients, you can directly invoke your procedures in tests without additional setup. This approach allows you to test procedures in isolation, ensuring they behave correctly.
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' },
])
})
INFO
You can also use the Fetch API to create production-like clients for testing purposes. Learn more
Mocking
The Implementer is designed for contract-first development, but it can also create alternative versions of your router or procedure for testing.
import { implement, unlazyRouter } from '@orpc/server'
const fakeListPlanet = implement(router.planet.list).handler(() => [])
You can use fakeListPlanet
to replace the actual listPlanet
implementation during tests.
INFO
The implement
function is also useful for creating mock servers for frontend testing scenarios.
WARNING
The implement
function doesn't support lazy routers yet. Use the unlazyRouter
utility to convert your lazy router before implementing. Learn more