https://github.com/maloguertin/msw-trpc
tPRC support for MSW
https://github.com/maloguertin/msw-trpc
jest msw trpc
Last synced: about 1 month ago
JSON representation
tPRC support for MSW
- Host: GitHub
- URL: https://github.com/maloguertin/msw-trpc
- Owner: maloguertin
- Created: 2023-01-22T20:00:46.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-18T15:51:57.000Z (over 1 year ago)
- Last Synced: 2024-03-15T15:10:18.645Z (about 1 year ago)
- Topics: jest, msw, trpc
- Language: TypeScript
- Homepage:
- Size: 166 KB
- Stars: 170
- Watchers: 4
- Forks: 13
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
> [!WARNING]
> You are looking at a pre-release version of msw-trpc, which adds support for msw v2. Documentation may be out of date and bugs might occur, use at your own risk## **[tPRC](https://trpc.io/) support for [MSW](https://mswjs.io/)**
- Create MSW handlers from your tRPC router.
- Get your tRPC typing into your MSW handlers.
- Augments MSW with utils for tRPC.## Motivation
As someone who loves MSW and was already using it I wanted to keep using it instead of mocking tRPC. While it is possible to simply write the Rest handlers it felt like it would be great not to lose the full power of tRPC types in the tests.
## Usage
**1. Install `msw-trpc`.**
```bash
npm i msw-trpc --save-dev
```**2. build your trpcMsw with createTRPCMsw.**
```typescript
import { createTRPCMsw } from 'msw-trpc'
import type { AppRouter } from 'path/to/your/router'export const trpcMsw = createTRPCMsw() /* 👈 */
```**3. Start using it.**
```typescript
const server = setupServer(
trpcMsw.userById.query((req, res, ctx) => {
return res(ctx.status(200), ctx.data({ id: '1', name: 'Uncle bob' }))
}),
trpcMsw.createUser.mutation(async (req, res, ctx) => {
return res(ctx.status(200), ctx.data({ id: '2', name: await req.json() }))
})
)
```## How it works
`createTRPCMsw` returns a Proxy that infers types from your AppRouter
```typescript
// all queries will expose a query function that accepts a MSW handler
trpcMsw.myQuery.query((req, res, ctx) => {})// all mutations will expose a mutation function that accepts a MSW handler
trpcMsw.myMutation.mutation((req, res, ctx) => {})
```**supports merged routers**
```typescript
// @filename: routers/_app.ts
// taken from https://trpc.io/docs/merging-routers
import { userRouter } from './user'
import { postRouter } from './post'const appRouter = router({
user: userRouter, // put procedures under "user" namespace
post: postRouter, // put procedures under "post" namespace
})// @filename: frontend/test/PostList.tsx
// all nested routers will be infered properly
trpcMsw.user.list.query((req, res, ctx) => {})
```## MSW Augments
**ctx.data**
Inspired by MSW GraphQL the context of your handlers now exposes a data function that will transform your data to the tRPC structure
```typescript
mswTrpc.userById.query((req, res, ctx) => {
console.log(ctx.data({ my: 'data' })) /* 👈 */
})// return ctx.json with
{
result: {
data: {
my: 'data'
}
}
}
```**req.getInput**
Returns the parsed input from the request
```typescript
//router.ts
const appRouter = t.router({
userById: t.procedure.input(z.object({ name: z.string() })).query(req => {
const { input } = req
const user = userList.find(u => u.name === input.name)
return user
}),
})//test-file.ts
mswTrpc.userById.query((req, res, ctx) => {
console.log(req.getInput()) /* 👈 */
})mswTrpc.createUser.mutation(async (req, res, ctx) => {
console.log(await req.getInput()) /* 👈 in mutation handle getInput returns a promise because it uses req.json() */
})// outputs
{
name: 'Pedro'
}test('should do something', () => {
trpc.userById.query({ name: 'Pedro' })
})
```Please note:
- calling `req.getInput` and req.json in the same mutation handler will fail
## Usage With `superjson`
```typescript
import { createTRPCMsw } from 'msw-trpc'
import type { AppRouter } from 'path/to/your/router'
import superjson from 'superjson';export const trpcMsw = createTRPCMsw({
transformer: {
input: superjson, /* 👈 */
output: superjson, /* 👈 */
},
})
```
## Config`createTRPCMsw` accepts a 2nd argument:
```typescript
type config = {
basePath?: string
baseUrl?: string
transformer?: {
input: {
serialize(object: any): any
deserialize(object: any): any
}
output: {
serialize(object: any): any
deserialize(object: any): any
}
}
}
```| property | default | details |
| ----------- | ------------------ | ------------------------------------------------------------------------------------------- |
| basePath | 'trpc' | Will match all requests to _basePath_ regardless of host |
| baseUrl | undefined | Setting this overrides basePath and will only match requests to this specific _baseUrl_ |
| transformer | defaultTransformer | Will transform your output data with `transformer.output.serialize` when calling `ctx.data` |## Requirements
Peer dependencies:
- [`tRPC`](https://github.com/trpc/trpc) server v10 (`@trpc/server`) must be installed.
- [`msw`](https://github.com/mswjs/msw) (`msw`) must be installed.Please note:
- Batch is not yet supported
- Merged routers will match in MSW against . or / (I'm open to PRs on how to only match against . 😊 )```typescript
mswTrpc.user.list.query() // this will match /trpc/user/list and /trpc/user.list
```