Skip to content

Commit a37cfcf

Browse files
JPeer264claude
andauthored
test(cloudflare): Cover withSentry and ExecutionContextCompat (#24559)
closes #24041 Add unit tests for the WorkerEntrypoint and error-swallowing paths of withSentry, and type tests that keep ExecutionContextCompat assignable from both the v4 and v5 ExecutionContext shapes. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent 5b691f0 commit a37cfcf

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { ExecutionContext } from '@cloudflare/workers-types';
2+
import { describe, expectTypeOf, it } from 'vitest';
3+
import type { ExecutionContextCompat } from '../src/executionContext';
4+
5+
// The shape of `ExecutionContext` in `@cloudflare/workers-types` v4, which has no `exports` and an optional `tracing`.
6+
interface ExecutionContextV4 {
7+
waitUntil(promise: Promise<unknown>): void;
8+
passThroughOnException(): void;
9+
readonly props: unknown;
10+
}
11+
12+
describe('ExecutionContextCompat', () => {
13+
it('accepts a v5 ExecutionContext', () => {
14+
expectTypeOf<ExecutionContext>().toExtend<ExecutionContextCompat>();
15+
});
16+
17+
it('accepts a v4 ExecutionContext without the members v5 made required', () => {
18+
expectTypeOf<ExecutionContextV4>().toExtend<ExecutionContextCompat>();
19+
});
20+
21+
it('rejects a context without waitUntil', () => {
22+
expectTypeOf<Omit<ExecutionContextV4, 'waitUntil'>>().not.toExtend<ExecutionContextCompat>();
23+
});
24+
25+
it('exposes waitUntil from both majors', () => {
26+
expectTypeOf<ExecutionContextCompat['waitUntil']>().toEqualTypeOf<ExecutionContext['waitUntil']>();
27+
});
28+
});
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import type { ExecutionContext } from '@cloudflare/workers-types';
2+
import { afterEach, describe, expect, it, vi } from 'vitest';
3+
import { withSentry } from '../src/withSentry';
4+
import { resetSdk } from './testUtils';
5+
6+
const MOCK_ENV = {
7+
SENTRY_DSN: 'https://public@dsn.ingest.sentry.io/1337',
8+
};
9+
10+
function createMockExecutionContext(): ExecutionContext {
11+
return {
12+
waitUntil: vi.fn(),
13+
passThroughOnException: vi.fn(),
14+
props: {},
15+
} as unknown as ExecutionContext;
16+
}
17+
18+
class WorkerEntrypoint {
19+
public constructor(
20+
public ctx: ExecutionContext,
21+
public env: unknown,
22+
) {}
23+
}
24+
25+
describe('withSentry', () => {
26+
afterEach(() => {
27+
vi.restoreAllMocks();
28+
resetSdk();
29+
});
30+
31+
it('returns the same handler object with its methods wrapped', () => {
32+
const fetch = vi.fn();
33+
const handler = { fetch };
34+
35+
const wrapped = withSentry(() => ({}), handler);
36+
37+
expect(wrapped).toBe(handler);
38+
expect(wrapped.fetch).not.toBe(fetch);
39+
});
40+
41+
it('instruments a WorkerEntrypoint class instead of treating it as a handler object', () => {
42+
class MyEntrypoint extends WorkerEntrypoint {
43+
public ping(): string {
44+
return 'pong';
45+
}
46+
}
47+
48+
const optionsCallback = vi.fn().mockReturnValue({ dsn: MOCK_ENV.SENTRY_DSN });
49+
const context = createMockExecutionContext();
50+
51+
const Wrapped = withSentry(optionsCallback, MyEntrypoint as never) as unknown as typeof MyEntrypoint;
52+
const instance = new Wrapped(context, MOCK_ENV);
53+
54+
expect(Wrapped).not.toBe(MyEntrypoint);
55+
expect(optionsCallback).toHaveBeenCalledWith(MOCK_ENV);
56+
expect(instance).toBeInstanceOf(MyEntrypoint);
57+
expect(instance.ctx).not.toBe(context);
58+
expect(instance.ping()).toBe('pong');
59+
});
60+
61+
it('returns a handler it cannot instrument unchanged instead of throwing', () => {
62+
const fetch = vi.fn();
63+
const handler = Object.freeze({ fetch });
64+
65+
let wrapped: typeof handler | undefined;
66+
expect(() => {
67+
wrapped = withSentry(() => ({}), handler);
68+
}).not.toThrow();
69+
70+
expect(wrapped).toBe(handler);
71+
expect(wrapped?.fetch).toBe(fetch);
72+
});
73+
});

0 commit comments

Comments
 (0)