Skip to content

Commit 13ceb0e

Browse files
author
cod1k
committed
Refactor Durable Object tests and enhance instrumentation
Refactor test structure for clarity and maintainability, including renaming test descriptions and adding `afterEach` for cleaning up mocks. Improve instrumentation validation by checking proper handling of prototype methods and method-specific behaviors. Ensure consistent and detailed coverage of Durable Object functionality.
1 parent 4eae009 commit 13ceb0e

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

packages/cloudflare/test/durableobject.test.ts

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,68 @@
11
import type { ExecutionContext } from '@cloudflare/workers-types';
22
import * as SentryCore from '@sentry/core';
3-
import { describe, expect, it, onTestFinished, vi } from 'vitest';
4-
import { instrumentDurableObjectWithSentry } from '../src/durableobject';
3+
import { afterEach, describe, expect, it, onTestFinished, vi } from 'vitest';
4+
import { instrumentDurableObjectWithSentry } from '../src';
55
import { isInstrumented } from '../src/instrument';
66

7-
describe('durable object', () => {
8-
it('instrumentDurableObjectWithSentry generic functionality', () => {
7+
describe('instrumentDurableObjectWithSentry', () => {
8+
afterEach(() => {
9+
vi.restoreAllMocks();
10+
});
11+
it('Generic functionality', () => {
912
const options = vi.fn();
1013
const instrumented = instrumentDurableObjectWithSentry(options, vi.fn());
1114
expect(instrumented).toBeTypeOf('function');
1215
expect(() => Reflect.construct(instrumented, [])).not.toThrow();
1316
expect(options).toHaveBeenCalledOnce();
1417
});
15-
it('all available durable object methods are instrumented', () => {
16-
const testClass = vi.fn(() => ({
17-
customMethod: vi.fn(),
18-
fetch: vi.fn(),
19-
alarm: vi.fn(),
20-
webSocketMessage: vi.fn(),
21-
webSocketClose: vi.fn(),
22-
webSocketError: vi.fn(),
23-
}));
18+
it('Instruments prototype methods without "sticking" to the options', () => {
19+
const initCore = vi.spyOn(SentryCore, 'initAndBind');
20+
vi.spyOn(SentryCore, 'getClient').mockReturnValue(undefined);
21+
const options = vi
22+
.fn()
23+
.mockReturnValueOnce({
24+
orgId: 1,
25+
})
26+
.mockReturnValueOnce({
27+
orgId: 2,
28+
});
29+
const testClass = class {
30+
method() {}
31+
};
32+
(Reflect.construct(instrumentDurableObjectWithSentry(options, testClass as any), []) as any).method();
33+
(Reflect.construct(instrumentDurableObjectWithSentry(options, testClass as any), []) as any).method();
34+
expect(initCore).nthCalledWith(1, expect.any(Function), expect.objectContaining({ orgId: 1 }));
35+
expect(initCore).nthCalledWith(2, expect.any(Function), expect.objectContaining({ orgId: 2 }));
36+
});
37+
it('All available durable object methods are instrumented', () => {
38+
const testClass = class {
39+
propertyFunction = vi.fn();
40+
41+
rpcMethod() {}
42+
43+
fetch() {}
44+
45+
alarm() {}
46+
47+
webSocketMessage() {}
48+
49+
webSocketClose() {}
50+
51+
webSocketError() {}
52+
};
2453
const instrumented = instrumentDurableObjectWithSentry(vi.fn(), testClass as any);
25-
const dObject: any = Reflect.construct(instrumented, []);
26-
for (const method of Object.getOwnPropertyNames(dObject)) {
27-
expect(isInstrumented(dObject[method]), `Method ${method} is instrumented`).toBeTruthy();
54+
const obj = Reflect.construct(instrumented, []);
55+
expect(Object.getPrototypeOf(obj), 'Prototype is instrumented').not.toBe(testClass.prototype);
56+
expect(isInstrumented((obj as any)['rpcMethod']), 'RPC method').toBeFalsy();
57+
for (const method_name of [
58+
'propertyFunction',
59+
'fetch',
60+
'alarm',
61+
'webSocketMessage',
62+
'webSocketClose',
63+
'webSocketError',
64+
]) {
65+
expect(isInstrumented((obj as any)[method_name]), `Method ${method_name} is instrumented`).toBeTruthy();
2866
}
2967
});
3068
it('flush performs after all waitUntil promises are finished', async () => {

0 commit comments

Comments
 (0)