-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtrpc.test.ts
More file actions
132 lines (110 loc) · 4.18 KB
/
trpc.test.ts
File metadata and controls
132 lines (110 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { expect, test } from '@playwright/test';
import { waitForError, waitForTransaction } from '@sentry-internal/test-utils';
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
import type { AppRouter } from '../src/app';
test('Should record span for trpc query', async ({ baseURL }) => {
const transactionEventPromise = waitForTransaction('node-express', transactionEvent => {
return (
transactionEvent.transaction === 'GET /trpc' &&
!!transactionEvent.spans?.find(span => span.description === 'trpc/getSomething')
);
});
const trpcClient = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url: `${baseURL}/trpc`,
}),
],
});
await trpcClient.getSomething.query('foobar');
await expect(transactionEventPromise).resolves.toBeDefined();
const transaction = await transactionEventPromise;
expect(transaction.spans).toContainEqual(
expect.objectContaining({
data: expect.objectContaining({
'sentry.op': 'rpc.server',
'sentry.origin': 'auto.rpc.trpc',
}),
description: `trpc/getSomething`,
}),
);
});
test('Should record transaction for trpc mutation', async ({ baseURL }) => {
const transactionEventPromise = waitForTransaction('node-express', transactionEvent => {
return (
transactionEvent.transaction === 'POST /trpc' &&
!!transactionEvent.spans?.find(span => span.description === 'trpc/createSomething')
);
});
const trpcClient = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url: `${baseURL}/trpc`,
}),
],
});
await trpcClient.createSomething.mutate();
await expect(transactionEventPromise).resolves.toBeDefined();
const transaction = await transactionEventPromise;
expect(transaction.spans).toContainEqual(
expect.objectContaining({
data: expect.objectContaining({
'sentry.op': 'rpc.server',
'sentry.origin': 'auto.rpc.trpc',
}),
description: `trpc/createSomething`,
}),
);
});
test('Should record transaction and error for a crashing trpc handler', async ({ baseURL }) => {
const transactionEventPromise = waitForTransaction('node-express', transactionEvent => {
return (
transactionEvent.transaction === 'POST /trpc' &&
!!transactionEvent.spans?.find(span => span.description === 'trpc/crashSomething')
);
});
const errorEventPromise = waitForError('node-express', errorEvent => {
return !!errorEvent?.exception?.values?.some(exception => exception.value?.includes('I crashed in a trpc handler'));
});
const trpcClient = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url: `${baseURL}/trpc`,
}),
],
});
await expect(trpcClient.crashSomething.mutate({ nested: { nested: { nested: 'foobar' } } })).rejects.toBeDefined();
await expect(transactionEventPromise).resolves.toBeDefined();
await expect(errorEventPromise).resolves.toBeDefined();
expect((await errorEventPromise).contexts?.trpc?.['procedure_type']).toBe('mutation');
expect((await errorEventPromise).contexts?.trpc?.['procedure_path']).toBe('crashSomething');
// Should record nested context
expect((await errorEventPromise).contexts?.trpc?.['input']).toEqual({
nested: {
nested: {
nested: 'foobar',
},
},
});
});
test('Should record transaction and error for a trpc handler that returns a status code', async ({ baseURL }) => {
const transactionEventPromise = waitForTransaction('node-express', transactionEvent => {
return (
transactionEvent.transaction === 'POST /trpc' &&
!!transactionEvent.spans?.find(span => span.description === 'trpc/unauthorized')
);
});
const errorEventPromise = waitForError('node-express', errorEvent => {
return !!errorEvent?.exception?.values?.some(exception => exception.value?.includes('Unauthorized'));
});
const trpcClient = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url: `${baseURL}/trpc`,
}),
],
});
await expect(trpcClient.unauthorized.mutate()).rejects.toBeDefined();
await expect(transactionEventPromise).resolves.toBeDefined();
await expect(errorEventPromise).resolves.toBeDefined();
});