Skip to content

Commit cca060f

Browse files
committed
Add node-koa tests
1 parent 216aaeb commit cca060f

File tree

6 files changed

+130
-72
lines changed

6 files changed

+130
-72
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ local.log
4343
.rpt2_cache
4444

4545
lint-results.json
46+
trace.zip
4647

4748
# legacy
4849
tmp.js

dev-packages/e2e-tests/test-applications/node-koa/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ const port1 = 3030;
1414
const port2 = 3040;
1515

1616
const Koa = require('koa');
17+
const { bodyParser } = require('@koa/bodyparser');
1718
const Router = require('@koa/router');
1819
const http = require('http');
1920

2021
const app1 = new Koa();
22+
app1.use(bodyParser());
2123

2224
Sentry.setupKoaErrorHandler(app1);
2325

@@ -109,6 +111,10 @@ router1.get('/test-assert/:condition', async ctx => {
109111
ctx.assert(condition, 400, 'ctx.assert failed');
110112
});
111113

114+
router1.post('/test-post', async ctx => {
115+
ctx.body = { status: 'ok', body: ctx.request.body };
116+
});
117+
112118
app1.use(router1.routes()).use(router1.allowedMethods());
113119

114120
app1.listen(port1);

dev-packages/e2e-tests/test-applications/node-koa/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"test:assert": "pnpm test"
1111
},
1212
"dependencies": {
13+
"@koa/bodyparser": "^5.1.1",
1314
"@koa/router": "^12.0.1",
1415
"@sentry/node": "latest || *",
1516
"@sentry/types": "latest || *",

dev-packages/e2e-tests/test-applications/node-koa/tests/assert.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ test('Returns 400 from failed assert', async ({ baseURL }) => {
1616

1717
expect(errorEvent.request).toEqual({
1818
method: 'GET',
19-
cookies: {},
2019
headers: expect.any(Object),
2120
url: 'http://localhost:3030/test-assert/false',
2221
});

dev-packages/e2e-tests/test-applications/node-koa/tests/errors.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ test('Sends correct error event', async ({ baseURL }) => {
1515

1616
expect(errorEvent.request).toEqual({
1717
method: 'GET',
18-
cookies: {},
1918
headers: expect.any(Object),
2019
url: 'http://localhost:3030/test-exception/123',
2120
});

dev-packages/e2e-tests/test-applications/node-koa/tests/transactions.test.ts

Lines changed: 122 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -46,76 +46,128 @@ test('Sends an API route transaction', async ({ baseURL }) => {
4646
origin: 'auto.http.otel.http',
4747
});
4848

49-
expect(transactionEvent).toEqual(
50-
expect.objectContaining({
51-
spans: [
52-
{
53-
data: {
54-
'koa.name': '',
55-
'koa.type': 'middleware',
56-
'sentry.origin': 'auto.http.otel.koa',
57-
'sentry.op': 'middleware.koa',
58-
},
59-
op: 'middleware.koa',
60-
origin: 'auto.http.otel.koa',
61-
description: '< unknown >',
62-
parent_span_id: expect.any(String),
63-
span_id: expect.any(String),
64-
start_timestamp: expect.any(Number),
65-
status: 'ok',
66-
timestamp: expect.any(Number),
67-
trace_id: expect.any(String),
68-
},
69-
{
70-
data: {
71-
'http.route': '/test-transaction',
72-
'koa.name': '/test-transaction',
73-
'koa.type': 'router',
74-
'sentry.origin': 'auto.http.otel.koa',
75-
'sentry.op': 'router.koa',
76-
},
77-
op: 'router.koa',
78-
description: '/test-transaction',
79-
parent_span_id: expect.any(String),
80-
span_id: expect.any(String),
81-
start_timestamp: expect.any(Number),
82-
status: 'ok',
83-
timestamp: expect.any(Number),
84-
trace_id: expect.any(String),
85-
origin: 'auto.http.otel.koa',
86-
},
87-
{
88-
data: {
89-
'sentry.origin': 'manual',
90-
},
91-
description: 'test-span',
92-
parent_span_id: expect.any(String),
93-
span_id: expect.any(String),
94-
start_timestamp: expect.any(Number),
95-
status: 'ok',
96-
timestamp: expect.any(Number),
97-
trace_id: expect.any(String),
98-
origin: 'manual',
99-
},
100-
{
101-
data: {
102-
'sentry.origin': 'manual',
103-
},
104-
description: 'child-span',
105-
parent_span_id: expect.any(String),
106-
span_id: expect.any(String),
107-
start_timestamp: expect.any(Number),
108-
status: 'ok',
109-
timestamp: expect.any(Number),
110-
trace_id: expect.any(String),
111-
origin: 'manual',
112-
},
113-
],
114-
transaction: 'GET /test-transaction',
115-
type: 'transaction',
116-
transaction_info: {
117-
source: 'route',
49+
expect(transactionEvent).toMatchObject({
50+
transaction: 'GET /test-transaction',
51+
type: 'transaction',
52+
transaction_info: {
53+
source: 'route',
54+
},
55+
});
56+
57+
expect(transactionEvent.spans).toEqual([
58+
{
59+
data: {
60+
'koa.name': 'bodyParser',
61+
'koa.type': 'middleware',
62+
'sentry.op': 'middleware.koa',
63+
'sentry.origin': 'auto.http.otel.koa',
64+
},
65+
description: 'bodyParser',
66+
op: 'middleware.koa',
67+
origin: 'auto.http.otel.koa',
68+
parent_span_id: expect.any(String),
69+
span_id: expect.any(String),
70+
start_timestamp: expect.any(Number),
71+
status: 'ok',
72+
timestamp: expect.any(Number),
73+
trace_id: expect.any(String),
74+
},
75+
{
76+
data: {
77+
'koa.name': '',
78+
'koa.type': 'middleware',
79+
'sentry.origin': 'auto.http.otel.koa',
80+
'sentry.op': 'middleware.koa',
81+
},
82+
op: 'middleware.koa',
83+
origin: 'auto.http.otel.koa',
84+
description: '< unknown >',
85+
parent_span_id: expect.any(String),
86+
span_id: expect.any(String),
87+
start_timestamp: expect.any(Number),
88+
status: 'ok',
89+
timestamp: expect.any(Number),
90+
trace_id: expect.any(String),
91+
},
92+
{
93+
data: {
94+
'http.route': '/test-transaction',
95+
'koa.name': '/test-transaction',
96+
'koa.type': 'router',
97+
'sentry.origin': 'auto.http.otel.koa',
98+
'sentry.op': 'router.koa',
11899
},
100+
op: 'router.koa',
101+
description: '/test-transaction',
102+
parent_span_id: expect.any(String),
103+
span_id: expect.any(String),
104+
start_timestamp: expect.any(Number),
105+
status: 'ok',
106+
timestamp: expect.any(Number),
107+
trace_id: expect.any(String),
108+
origin: 'auto.http.otel.koa',
109+
},
110+
{
111+
data: {
112+
'sentry.origin': 'manual',
113+
},
114+
description: 'test-span',
115+
parent_span_id: expect.any(String),
116+
span_id: expect.any(String),
117+
start_timestamp: expect.any(Number),
118+
status: 'ok',
119+
timestamp: expect.any(Number),
120+
trace_id: expect.any(String),
121+
origin: 'manual',
122+
},
123+
{
124+
data: {
125+
'sentry.origin': 'manual',
126+
},
127+
description: 'child-span',
128+
parent_span_id: expect.any(String),
129+
span_id: expect.any(String),
130+
start_timestamp: expect.any(Number),
131+
status: 'ok',
132+
timestamp: expect.any(Number),
133+
trace_id: expect.any(String),
134+
origin: 'manual',
135+
},
136+
]);
137+
});
138+
139+
test('Captures request metadata', async ({ baseURL }) => {
140+
const transactionEventPromise = waitForTransaction('node-koa', transactionEvent => {
141+
return (
142+
transactionEvent?.contexts?.trace?.op === 'http.server' && transactionEvent?.transaction === 'POST /test-post'
143+
);
144+
});
145+
146+
const res = await fetch(`${baseURL}/test-post`, {
147+
method: 'POST',
148+
body: JSON.stringify({ foo: 'bar', other: 1 }),
149+
headers: {
150+
'Content-Type': 'application/json',
151+
},
152+
});
153+
const resBody = await res.json();
154+
155+
expect(resBody).toEqual({ status: 'ok', body: { foo: 'bar', other: 1 } });
156+
157+
const transactionEvent = await transactionEventPromise;
158+
159+
expect(transactionEvent.request).toEqual({
160+
url: expect.stringMatching(/^http:\/\/localhost:(\d+)\/test-post$/),
161+
method: 'POST',
162+
headers: expect.objectContaining({
163+
'user-agent': expect.stringContaining(''),
164+
'content-type': 'application/json',
119165
}),
120-
);
166+
data: JSON.stringify({
167+
foo: 'bar',
168+
other: 1,
169+
}),
170+
});
171+
172+
expect(transactionEvent.user).toEqual(undefined);
121173
});

0 commit comments

Comments
 (0)