|
1 | 1 | import type { ExecutionContext } from '@cloudflare/workers-types';
|
2 | 2 | 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'; |
5 | 5 | import { isInstrumented } from '../src/instrument';
|
6 | 6 |
|
7 |
| -describe('durable object', () => { |
8 |
| - it('instrumentDurableObjectWithSentry generic functionality', () => { |
| 7 | +describe('instrumentDurableObjectWithSentry', () => { |
| 8 | + afterEach(() => { |
| 9 | + vi.restoreAllMocks(); |
| 10 | + }); |
| 11 | + it('Generic functionality', () => { |
9 | 12 | const options = vi.fn();
|
10 | 13 | const instrumented = instrumentDurableObjectWithSentry(options, vi.fn());
|
11 | 14 | expect(instrumented).toBeTypeOf('function');
|
12 | 15 | expect(() => Reflect.construct(instrumented, [])).not.toThrow();
|
13 | 16 | expect(options).toHaveBeenCalledOnce();
|
14 | 17 | });
|
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 | + }; |
24 | 53 | 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(); |
28 | 66 | }
|
29 | 67 | });
|
30 | 68 | it('flush performs after all waitUntil promises are finished', async () => {
|
|
0 commit comments