Skip to content

Commit 6cf7f53

Browse files
committed
Add integration tests for DurableObjects
1 parent 23a8ca3 commit 6cf7f53

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as Sentry from '@sentry/cloudflare';
2+
import { DurableObject } from 'cloudflare:workers';
3+
4+
interface Env {
5+
SENTRY_DSN: string;
6+
TEST_DURABLE_OBJECT: DurableObjectNamespace;
7+
}
8+
9+
class TestDurableObjectBase extends DurableObject<Env> {
10+
public constructor(ctx: DurableObjectState, env: Env) {
11+
super(ctx, env);
12+
}
13+
14+
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
15+
async sayHello(name: string): Promise<string> {
16+
return `Hello, ${name}`;
17+
}
18+
}
19+
20+
export const TestDurableObject = Sentry.instrumentDurableObjectWithSentry(
21+
(env: Env) => ({
22+
dsn: env.SENTRY_DSN,
23+
tracesSampleRate: 1.0,
24+
beforeSendTransaction: transaction => {
25+
console.log('beforeSendTransaction', transaction);
26+
return transaction;
27+
},
28+
}),
29+
TestDurableObjectBase,
30+
);
31+
32+
export default {
33+
async fetch(request, env): Promise<Response> {
34+
const id: DurableObjectId = env.TEST_DURABLE_OBJECT.idFromName('test');
35+
const stub = env.TEST_DURABLE_OBJECT.get(id) as unknown as TestDurableObjectBase;
36+
37+
if (request.url.includes('hello')) {
38+
const greeting = await stub.sayHello('world');
39+
return new Response(greeting);
40+
}
41+
42+
return new Response('Usual response');
43+
},
44+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { expect, it } from 'vitest';
2+
import { createRunner } from '../../../runner';
3+
4+
it('traces a durable object method', async () => {
5+
const runner = createRunner(__dirname)
6+
.expect(envelope => {
7+
const transactionEvent = envelope[1]?.[0]?.[1];
8+
expect(transactionEvent).toEqual(
9+
expect.objectContaining({
10+
contexts: expect.objectContaining({
11+
trace: expect.objectContaining({
12+
op: 'rpc',
13+
data: expect.objectContaining({
14+
'sentry.op': 'rpc',
15+
'sentry.origin': 'auto.faas.cloudflare_durableobjects',
16+
}),
17+
origin: 'auto.faas.cloudflare_durableobjects',
18+
}),
19+
}),
20+
transaction: 'sayHello',
21+
}),
22+
);
23+
})
24+
.start();
25+
await runner.makeRequest('get', '/hello');
26+
await runner.completed();
27+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "worker-name",
3+
"main": "index.ts",
4+
"compatibility_date": "2025-06-17",
5+
"migrations": [
6+
{
7+
"new_sqlite_classes": ["TestDurableObject"],
8+
"tag": "v1"
9+
}
10+
],
11+
"durable_objects": {
12+
"bindings": [
13+
{
14+
"class_name": "TestDurableObject",
15+
"name": "TEST_DURABLE_OBJECT"
16+
}
17+
]
18+
},
19+
"compatibility_flags": ["nodejs_als"],
20+
"vars": {
21+
"SENTRY_DSN": "https://[email protected]/4509553159831552"
22+
}
23+
}

0 commit comments

Comments
 (0)