Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
owens1127 committed Jan 30, 2025
1 parent adbbf35 commit 08fad9b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
15 changes: 1 addition & 14 deletions packages/trpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,4 @@ To add a new procedure, follow these steps:

## Testing with tRPC

### Unit Testing

You can write unit tests for your tRPC procedures using the `$trpcCaller` export. Here is an example test for the `hello` procedure:

```ts
import { expect, test } from "bun:test";

import { $trpcCaller } from "@good-dog/trpc/server";

test("hello world", async () => {
const result = await $trpcCaller.hello({ text: "world" });
expect(result.greeting).toEqual("hello world");
});
```
See documentation for tRPC [testing](../../tests/README.md).
58 changes: 51 additions & 7 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,63 @@
# Tests

## Endpoint Integration Testing

You can write integrations tests for your tRPC procedures using the `$createTrpcCaller` export.

This will return an api caller function that with the specified context and services.

```ts
import { expect, test } from "bun:test";

import { $createTrpcCaller } from "@good-dog/trpc/server";

const $api = $createTrpcCaller({
/**
* Here you attach your services you need.
*
* Let's say we wanted to mock a service used by the hello procedure
* used to generate a greeting. We can mock the service here.
*/
helloService: (str: string) => `hello ${str}`,
});

test("hello world", async () => {
const result = await $trpcCaller.hello({ text: "world" });
expect(result.greeting).toEqual("hello world");
});
```

## Mocks

### MockNextCookies

This is a mock for the cookies function in next/headers. In your tests, just instantiate
a new MockNextCookies object, set the cookies you would like, and run the apply() method
on the newly created object.
This module implements all functionality of the `cookies()` function from Next.js, but in a mockable way. This is useful for testing components that rely on cookies.

Example usage:

<pre>const cookies = new MockNextCookies();
cookies.set("myKey", "myValue");
cookies.apply();
... rest of your test</pre>
```ts
const cookies = new MockNextCookies();

const $api = $createTrpcCaller({
cookiesService: createMockCookieService(mockCookies),
});

afterEach(() => {
cookies.clear();
});

test(() => {
await $api.someProcedure();

expect(cookies.get).toHaveBeenCalledWith("cookie-name");
});
```

## MockEmailService

This module extends our email service to allow for mocking of the actual "send" function. This is useful for testing components that rely on sending emails.

It also overrides the default behavior of the randomized 6-digit code generator to always return the same code.

## Running Tests

Expand Down

0 comments on commit 08fad9b

Please sign in to comment.