-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathmiddleware.test.ts
More file actions
120 lines (104 loc) · 4.66 KB
/
middleware.test.ts
File metadata and controls
120 lines (104 loc) · 4.66 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
import { expect, test } from '@playwright/test';
import { waitForError, waitForTransaction } from '@sentry-internal/test-utils';
const packageJson = require('../package.json');
const nextjsVersion = packageJson.dependencies.next;
const nextjsMajor = Number(nextjsVersion.split('.')[0]);
test('Should create a transaction for middleware', async ({ request }) => {
test.skip(
nextjsMajor === 13,
'Middleware transactions are not created in Next.js 13 after dropping tracing from middleware',
);
const middlewareTransactionPromise = waitForTransaction('nextjs-pages-dir', async transactionEvent => {
return transactionEvent?.transaction === 'middleware GET';
});
const response = await request.get('/api/endpoint-behind-middleware');
expect(await response.json()).toStrictEqual({ name: 'John Doe' });
const middlewareTransaction = await middlewareTransactionPromise;
expect(middlewareTransaction.contexts?.trace?.status).toBe('ok');
expect(middlewareTransaction.contexts?.trace?.op).toBe('http.server.middleware');
expect(middlewareTransaction.contexts?.runtime?.name).toBe('vercel-edge');
expect(middlewareTransaction.transaction_info?.source).toBe('url');
// Assert that isolation scope works properly
expect(middlewareTransaction.tags?.['my-isolated-tag']).toBe(true);
expect(middlewareTransaction.tags?.['my-global-scope-isolated-tag']).not.toBeDefined();
});
test('Faulty middlewares', async ({ request }) => {
test.skip(
nextjsMajor === 13,
'Middleware transactions are not created in Next.js 13 after dropping tracing from middleware',
);
const middlewareTransactionPromise = waitForTransaction('nextjs-pages-dir', async transactionEvent => {
return transactionEvent?.transaction === 'middleware GET';
});
const errorEventPromise = waitForError('nextjs-pages-dir', errorEvent => {
return errorEvent?.exception?.values?.[0]?.value === 'Middleware Error';
});
request.get('/api/endpoint-behind-faulty-middleware', { headers: { 'x-should-throw': '1' } }).catch(() => {
// Noop
});
await test.step('should record transactions', async () => {
const middlewareTransaction = await middlewareTransactionPromise;
expect(middlewareTransaction.contexts?.trace?.status).toBe('internal_error');
expect(middlewareTransaction.contexts?.trace?.op).toBe('http.server.middleware');
expect(middlewareTransaction.contexts?.runtime?.name).toBe('vercel-edge');
expect(middlewareTransaction.transaction_info?.source).toBe('url');
});
await test.step('should record exceptions', async () => {
const errorEvent = await errorEventPromise;
// Assert that isolation scope works properly
expect(errorEvent.tags?.['my-isolated-tag']).toBe(true);
expect(errorEvent.tags?.['my-global-scope-isolated-tag']).not.toBeDefined();
expect(errorEvent.transaction).toBe('middleware GET');
});
});
test('Should trace outgoing fetch requests inside middleware and create breadcrumbs for it', async ({ request }) => {
test.skip(
nextjsMajor === 13,
'Middleware transactions are not created in Next.js 13 after dropping tracing from middleware',
);
const middlewareTransactionPromise = waitForTransaction('nextjs-pages-dir', async transactionEvent => {
return (
transactionEvent?.transaction === 'middleware GET' &&
!!transactionEvent.spans?.find(span => span.op === 'http.client')
);
});
request.get('/api/endpoint-behind-middleware', { headers: { 'x-should-make-request': '1' } }).catch(() => {
// Noop
});
const middlewareTransaction = await middlewareTransactionPromise;
expect(middlewareTransaction.spans).toEqual(
expect.arrayContaining([
{
data: {
'http.method': 'GET',
'http.response.status_code': 200,
type: 'fetch',
url: 'http://localhost:3030/',
'http.url': 'http://localhost:3030/',
'server.address': 'localhost:3030',
'sentry.op': 'http.client',
'sentry.origin': 'auto.http.wintercg_fetch',
},
description: 'GET http://localhost:3030/',
op: 'http.client',
origin: 'auto.http.wintercg_fetch',
parent_span_id: expect.stringMatching(/[a-f0-9]{16}/),
span_id: expect.stringMatching(/[a-f0-9]{16}/),
start_timestamp: expect.any(Number),
status: 'ok',
timestamp: expect.any(Number),
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
},
]),
);
expect(middlewareTransaction.breadcrumbs).toEqual(
expect.arrayContaining([
{
category: 'fetch',
data: { method: 'GET', status_code: 200, url: 'http://localhost:3030/' },
timestamp: expect.any(Number),
type: 'http',
},
]),
);
});