Skip to content

Commit 0ceddfc

Browse files
committed
docs: Testing & Mocking
1 parent 0a9a32d commit 0ceddfc

File tree

3 files changed

+52
-31
lines changed

3 files changed

+52
-31
lines changed

apps/content/.vitepress/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,11 @@ export default withMermaid(defineConfig({
211211
{ text: 'Building Custom Plugins', link: '/docs/advanced/building-custom-plugins' },
212212
{ text: 'Exceeds the maximum length ...', link: '/docs/advanced/exceeds-the-maximum-length-problem' },
213213
{ text: 'Extend Body Parser', link: '/docs/advanced/extend-body-parser' },
214-
{ text: 'Mocking', link: '/docs/advanced/mocking' },
215214
{ text: 'Publish Client to NPM', link: '/docs/advanced/publish-client-to-npm' },
216215
{ text: 'RPC JSON Serializer', link: '/docs/advanced/rpc-json-serializer' },
217216
{ text: 'RPC Protocol', link: '/docs/advanced/rpc-protocol' },
218217
{ text: 'SuperJson', link: '/docs/advanced/superjson' },
218+
{ text: 'Testing & Mocking', link: '/docs/advanced/testing-mocking' },
219219
{ text: 'Validation Errors', link: '/docs/advanced/validation-errors' },
220220
],
221221
},

apps/content/docs/advanced/mocking.md

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
title: Testing & Mocking
3+
description: How to test and mock oRPC routers and procedures?
4+
---
5+
6+
# Testing & Mocking
7+
8+
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.
9+
10+
## Testing
11+
12+
Using [Server-Side Clients](/docs/client/server-side), you can directly invoke your procedures in tests without additional setup. This approach allows you to test procedures in isolation, ensuring they behave correctly.
13+
14+
```ts
15+
import { call } from '@orpc/server'
16+
17+
it('works', async () => {
18+
await expect(
19+
call(router.planet.list, { page: 1, size: 10 })
20+
).resolves.toEqual([
21+
{ id: '1', name: 'Earth' },
22+
{ id: '2', name: 'Mars' },
23+
])
24+
})
25+
```
26+
27+
::: info
28+
You can also use the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to create production-like clients for testing purposes. [Learn more](/docs/best-practices/optimize-ssr#alternative-approach)
29+
:::
30+
31+
## Mocking
32+
33+
The [Implementer](/docs/contract-first/implement-contract#the-implementer) is designed for contract-first development, but it can also create alternative versions of your [router](/docs/router) or [procedure](/docs/procedure) for testing.
34+
35+
```ts twoslash
36+
import { router } from './shared/planet'
37+
// ---cut---
38+
import { implement, unlazyRouter } from '@orpc/server'
39+
40+
const fakeListPlanet = implement(router.planet.list).handler(() => [])
41+
```
42+
43+
You can use `fakeListPlanet` to replace the actual `listPlanet` implementation during tests.
44+
45+
::: info
46+
The `implement` function is also useful for creating mock servers for frontend testing scenarios.
47+
:::
48+
49+
::: warning
50+
The `implement` function doesn't support [lazy routers](/docs/router#lazy-router) yet. Use the `unlazyRouter` utility to convert your lazy router before implementing. [Learn more](/docs/contract-first/router-to-contract#unlazy-the-router)
51+
:::

0 commit comments

Comments
 (0)