From 4eae0094584d364b791eb0e16e1e577fe625fd72 Mon Sep 17 00:00:00 2001 From: cod1k Date: Tue, 8 Jul 2025 11:19:44 +0300 Subject: [PATCH 1/9] Add Durable Object support and e2e tests for Cloudflare Workers This commit introduces Durable Object support in the Cloudflare Workers example to enhance functionality and error tracking via Sentry. It removes outdated tests, updates dependencies, configures Playwright for e2e testing, and refines build/test scripts for CI/CD use cases. --- .../cloudflare-workers/.gitignore | 1 + .../cloudflare-workers/package.json | 24 +++- .../cloudflare-workers/playwright.config.ts | 19 +++ .../cloudflare-workers/src/index.ts | 64 ++++++++- .../cloudflare-workers/start-event-proxy.mjs | 6 + .../cloudflare-workers/test/index.spec.ts | 25 ---- .../cloudflare-workers/test/tsconfig.json | 8 -- .../cloudflare-workers/tests/index.test.ts | 37 +++++ .../cloudflare-workers/tests/tsconfig.json | 8 ++ .../cloudflare-workers/tsconfig.json | 132 +++++------------- .../worker-configuration.d.ts | 1 + .../cloudflare-workers/wrangler.toml | 12 +- 12 files changed, 193 insertions(+), 144 deletions(-) create mode 100644 dev-packages/e2e-tests/test-applications/cloudflare-workers/.gitignore create mode 100644 dev-packages/e2e-tests/test-applications/cloudflare-workers/playwright.config.ts create mode 100644 dev-packages/e2e-tests/test-applications/cloudflare-workers/start-event-proxy.mjs delete mode 100644 dev-packages/e2e-tests/test-applications/cloudflare-workers/test/index.spec.ts delete mode 100644 dev-packages/e2e-tests/test-applications/cloudflare-workers/test/tsconfig.json create mode 100644 dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/index.test.ts create mode 100644 dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/tsconfig.json diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/.gitignore b/dev-packages/e2e-tests/test-applications/cloudflare-workers/.gitignore new file mode 100644 index 000000000000..e71378008bf1 --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/.gitignore @@ -0,0 +1 @@ +.wrangler diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json b/dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json index b195ec7e2716..0e1f1217ffce 100644 --- a/dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json @@ -4,27 +4,37 @@ "private": true, "scripts": { "deploy": "wrangler deploy", - "dev": "wrangler dev --var E2E_TEST_DSN=$E2E_TEST_DSN", - "build": "wrangler deploy --dry-run --var E2E_TEST_DSN=$E2E_TEST_DSN", - "test": "vitest", + "dev": "wrangler dev --var \"E2E_TEST_DSN:$E2E_TEST_DSN\" --log-level=$(test $CI && echo 'none' || echo 'log')", + "build": "wrangler deploy --dry-run", + "test": "vitest --run", "typecheck": "tsc --noEmit", + "clean:state": "rimraf .wrangler", "cf-typegen": "wrangler types", "test:build": "pnpm install && pnpm build", - "test:assert": "pnpm typecheck" + "test:assert": "pnpm test:dev && pnpm test:prod", + "test:prod": "pnpm clean:state && TEST_ENV=production playwright test", + "test:dev": "pnpm clean:state && TEST_ENV=development playwright test" }, "dependencies": { "@sentry/cloudflare": "latest || *" }, "devDependencies": { - "@cloudflare/vitest-pool-workers": "^0.4.5", + "@playwright/test": "~1.50.0", + "@cloudflare/vitest-pool-workers": "^0.8.19", "@cloudflare/workers-types": "^4.20240725.0", + "@sentry-internal/test-utils": "link:../../../test-utils", "typescript": "^5.5.2", - "vitest": "1.6.1", - "wrangler": "4.22.0" + "vitest": "~3.2.0", + "wrangler": "^4.23.0" }, "volta": { "extends": "../../package.json" }, + "pnpm": { + "overrides": { + "strip-literal": "~2.0.0" + } + }, "sentryTest": { "optional": true } diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/playwright.config.ts b/dev-packages/e2e-tests/test-applications/cloudflare-workers/playwright.config.ts new file mode 100644 index 000000000000..b90ce4dadc01 --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/playwright.config.ts @@ -0,0 +1,19 @@ +import { getPlaywrightConfig } from '@sentry-internal/test-utils'; +const testEnv = process.env.TEST_ENV; + +if (!testEnv) { + throw new Error('No test env defined'); +} + +const config = getPlaywrightConfig( + { + startCommand: 'pnpm dev', + port: 8787, + }, + { + // This comes with the risk of tests leaking into each other but the tests run quite slow so we should parallelize + workers: '100%', + }, +); + +export default config; diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/src/index.ts b/dev-packages/e2e-tests/test-applications/cloudflare-workers/src/index.ts index a3366168fa08..aed9ab7fa955 100644 --- a/dev-packages/e2e-tests/test-applications/cloudflare-workers/src/index.ts +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/src/index.ts @@ -11,15 +11,75 @@ * Learn more at https://developers.cloudflare.com/workers/ */ import * as Sentry from '@sentry/cloudflare'; +import { DurableObject } from "cloudflare:workers"; + +class MyDurableObjectBase extends DurableObject { + async throwException(): Promise { + throw new Error("Should be recorded in Sentry.") + } + + async fetch(request: Request){ + const {pathname} = new URL(request.url) + if(pathname === '/throwException'){ + await this.throwException() + } + return new Response('DO is fine') + } +} + +export const MyDurableObject = Sentry.instrumentDurableObjectWithSentry( + (env: Env) => ({ + dsn: env.E2E_TEST_DSN, + environment: 'qa', // dynamic sampling bias to keep transactions + tunnel: `http://localhost:3031/`, // proxy server + tracesSampleRate: 1.0, + sendDefaultPii: true, + transportOptions: { + // We are doing a lot of events at once in this test + bufferSize: 1000, + }, + }), + MyDurableObjectBase, +); export default Sentry.withSentry( (env: Env) => ({ dsn: env.E2E_TEST_DSN, - // Set tracesSampleRate to 1.0 to capture 100% of spans for tracing. + environment: 'qa', // dynamic sampling bias to keep transactions + tunnel: `http://localhost:3031/`, // proxy server tracesSampleRate: 1.0, + sendDefaultPii: true, + transportOptions: { + // We are doing a lot of events at once in this test + bufferSize: 1000, + }, }), { - async fetch(request, env, ctx) { + async fetch(request, env) { + const url = new URL(request.url); + switch (url.pathname) { + case '/rpc/throwException': + { + const id = env.MY_DURABLE_OBJECT.idFromName('foo'); + const stub = env.MY_DURABLE_OBJECT.get(id) as DurableObjectStub; + try { + await stub.throwException(); + } catch (e) { + //We will catch this to be sure not to log inside withSentry + return new Response(null, { status: 500 }); + } + } + break; + case '/throwException': + throw new Error('To be recorded in Sentry.'); + default: + if (url.pathname.startsWith('/pass-to-object/')) { + const id = env.MY_DURABLE_OBJECT.idFromName('foo'); + const stub = env.MY_DURABLE_OBJECT.get(id) as DurableObjectStub; + url.pathname = url.pathname.replace('/pass-to-object/', ''); + return stub.fetch(new Request(url, request)); + } + } return new Response('Hello World!'); }, } satisfies ExportedHandler, diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/start-event-proxy.mjs b/dev-packages/e2e-tests/test-applications/cloudflare-workers/start-event-proxy.mjs new file mode 100644 index 000000000000..738ec64293b5 --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/start-event-proxy.mjs @@ -0,0 +1,6 @@ +import {startEventProxyServer} from '@sentry-internal/test-utils' + +startEventProxyServer({ + port: 3031, + proxyServerName: 'cloudflare-workers', +}) diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/test/index.spec.ts b/dev-packages/e2e-tests/test-applications/cloudflare-workers/test/index.spec.ts deleted file mode 100644 index 21c9d1b7999a..000000000000 --- a/dev-packages/e2e-tests/test-applications/cloudflare-workers/test/index.spec.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { describe, expect, it } from 'vitest'; -import worker from '../src/index'; -// test/index.spec.ts -import { SELF, createExecutionContext, env, waitOnExecutionContext } from 'cloudflare:test'; - -// For now, you'll need to do something like this to get a correctly-typed -// `Request` to pass to `worker.fetch()`. -const IncomingRequest = Request; - -describe('Hello World worker', () => { - it('responds with Hello World! (unit style)', async () => { - const request = new IncomingRequest('http://example.com'); - // Create an empty context to pass to `worker.fetch()`. - const ctx = createExecutionContext(); - const response = await worker.fetch(request, env, ctx); - // Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions - await waitOnExecutionContext(ctx); - expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`); - }); - - it('responds with Hello World! (integration style)', async () => { - const response = await SELF.fetch('https://example.com'); - expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`); - }); -}); diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/test/tsconfig.json b/dev-packages/e2e-tests/test-applications/cloudflare-workers/test/tsconfig.json deleted file mode 100644 index bc019a7e2bfb..000000000000 --- a/dev-packages/e2e-tests/test-applications/cloudflare-workers/test/tsconfig.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "extends": "../tsconfig.json", - "compilerOptions": { - "types": ["@cloudflare/workers-types/experimental", "@cloudflare/vitest-pool-workers"] - }, - "include": ["./**/*.ts", "../src/env.d.ts"], - "exclude": [] -} diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/index.test.ts b/dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/index.test.ts new file mode 100644 index 000000000000..3b2238b4c466 --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/index.test.ts @@ -0,0 +1,37 @@ +import { expect, test } from '@playwright/test'; +import { waitForError } from '@sentry-internal/test-utils'; + +test('Index page', async ({ page }) => { + const result = await page.goto('http://localhost:8787/'); + expect(result?.status?.()).toBe(200); + await expect(page.textContent('body > pre')).resolves.toBe('Hello World!'); +}) + +test('worker\'s withSentry', async () => { + const eventWaiter = waitForError('cloudflare-workers', (event) => { + return event.exception?.values?.[0]?.mechanism?.type === 'cloudflare'; + }); + const response = await fetch('http://localhost:8787/throwException'); + expect(response.status).toBe(500); + const event = await eventWaiter; + expect(event.exception?.values?.[0]?.value).toBe('To be recorded in Sentry.'); +}) + +test('RPC method which throws an exception to be logged to sentry', async () => { + const eventWaiter = waitForError('cloudflare-workers', (event) => { + return event.exception?.values?.[0]?.mechanism?.type === 'cloudflare_durableobject'; + }); + const response = await fetch('http://localhost:8787/rpc/throwException'); + expect(response.status).toBe(500); + const event = await eventWaiter; + expect(event.exception?.values?.[0]?.value).toBe('Should be recorded in Sentry.'); +}); +test('Request processed by DurableObject\'s fetch is recorded', async () => { + const eventWaiter = waitForError('cloudflare-workers', (event) => { + return event.exception?.values?.[0]?.mechanism?.type === 'cloudflare_durableobject'; + }); + const response = await fetch('http://localhost:8787/pass-to-object/throwException'); + expect(response.status).toBe(500); + const event = await eventWaiter; + expect(event.exception?.values?.[0]?.value).toBe('Should be recorded in Sentry.'); +}); diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/tsconfig.json b/dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/tsconfig.json new file mode 100644 index 000000000000..978ecd87b7ce --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "types": ["@cloudflare/vitest-pool-workers"] + }, + "include": ["./**/*.ts", "../worker-configuration.d.ts"], + "exclude": [] +} diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/tsconfig.json b/dev-packages/e2e-tests/test-applications/cloudflare-workers/tsconfig.json index 79207ab7ae9a..ca1f83e3bc15 100644 --- a/dev-packages/e2e-tests/test-applications/cloudflare-workers/tsconfig.json +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/tsconfig.json @@ -2,103 +2,43 @@ "compilerOptions": { /* Visit https://aka.ms/tsconfig.json to read more about this file */ - /* Projects */ - // "incremental": true, /* Enable incremental compilation */ - // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ - // "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */ - // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */ - // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ - // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ - - /* Language and Environment */ - "target": "es2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, - "lib": ["es2021"] /* Specify a set of bundled library declaration files that describe the target runtime environment. */, - "jsx": "react-jsx" /* Specify what JSX code is generated. */, - // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ - // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ - // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */ - // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ - // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */ - // "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */ - // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ - // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ - - /* Modules */ - "module": "es2022" /* Specify what module code is generated. */, - // "rootDir": "./", /* Specify the root folder within your source files. */ - "moduleResolution": "Bundler" /* Specify how TypeScript looks up a file from a given module specifier. */, - // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ - // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ - // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ - // "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */ + /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "target": "es2021", + /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + "lib": ["es2021"], + /* Specify what JSX code is generated. */ + "jsx": "react-jsx", + + /* Specify what module code is generated. */ + "module": "es2022", + /* Specify how TypeScript looks up a file from a given module specifier. */ + "moduleResolution": "Bundler", + /* Enable importing .json files */ + "resolveJsonModule": true, + + /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */ + "allowJs": true, + /* Enable error reporting in type-checked JavaScript files. */ + "checkJs": false, + + /* Disable emitting files from a compilation. */ + "noEmit": true, + + /* Ensure that each file can be safely transpiled without relying on other imports. */ + "isolatedModules": true, + /* Allow 'import x from y' when a module doesn't have a default export. */ + "allowSyntheticDefaultImports": true, + /* Ensure that casing is correct in imports. */ + "forceConsistentCasingInFileNames": true, + + /* Enable all strict type-checking options. */ + "strict": true, + + /* Skip type checking all .d.ts files. */ + "skipLibCheck": true, "types": [ - "@cloudflare/workers-types/2023-07-01" - ] /* Specify type package names to be included without being referenced in a source file. */, - // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ - "resolveJsonModule": true /* Enable importing .json files */, - // "noResolve": true, /* Disallow `import`s, `require`s or ``s from expanding the number of files TypeScript should add to a project. */ - - /* JavaScript Support */ - "allowJs": true /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */, - "checkJs": false /* Enable error reporting in type-checked JavaScript files. */, - // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */ - - /* Emit */ - // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ - // "declarationMap": true, /* Create sourcemaps for d.ts files. */ - // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ - // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ - // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */ - // "outDir": "./", /* Specify an output folder for all emitted files. */ - // "removeComments": true, /* Disable emitting comments. */ - "noEmit": true /* Disable emitting files from a compilation. */, - // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ - // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */ - // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ - // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ - // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ - // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ - // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ - // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ - // "newLine": "crlf", /* Set the newline character for emitting files. */ - // "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */ - // "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */ - // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ - // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ - // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ - - /* Interop Constraints */ - "isolatedModules": true /* Ensure that each file can be safely transpiled without relying on other imports. */, - "allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */, - // "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */, - // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ - "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, - - /* Type Checking */ - "strict": true /* Enable all strict type-checking options. */, - // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */ - // "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */ - // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ - // "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */ - // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ - // "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */ - // "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */ - // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ - // "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */ - // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */ - // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ - // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ - // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ - // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ - // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ - // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */ - // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ - // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ - - /* Completeness */ - // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ - "skipLibCheck": true /* Skip type checking all .d.ts files. */ + "./worker-configuration.d.ts" + ] }, "exclude": ["test"], "include": ["worker-configuration.d.ts", "src/**/*.ts"] diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/worker-configuration.d.ts b/dev-packages/e2e-tests/test-applications/cloudflare-workers/worker-configuration.d.ts index 0c9e04919e42..08a92a61d05d 100644 --- a/dev-packages/e2e-tests/test-applications/cloudflare-workers/worker-configuration.d.ts +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/worker-configuration.d.ts @@ -3,4 +3,5 @@ interface Env { E2E_TEST_DSN: ''; + MY_DURABLE_OBJECT: DurableObjectNamespace } diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/wrangler.toml b/dev-packages/e2e-tests/test-applications/cloudflare-workers/wrangler.toml index 2fc762f4025c..70b0ae034580 100644 --- a/dev-packages/e2e-tests/test-applications/cloudflare-workers/wrangler.toml +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/wrangler.toml @@ -53,15 +53,15 @@ compatibility_flags = ["nodejs_compat"] # Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model. # Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps. # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects -# [[durable_objects.bindings]] -# name = "MY_DURABLE_OBJECT" -# class_name = "MyDurableObject" +[[durable_objects.bindings]] +name = "MY_DURABLE_OBJECT" +class_name = "MyDurableObject" # Durable Object migrations. # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#migrations -# [[migrations]] -# tag = "v1" -# new_classes = ["MyDurableObject"] +[[migrations]] +tag = "v1" +new_sqlite_classes = ["MyDurableObject"] # Bind a Hyperdrive configuration. Use to accelerate access to your existing databases from Cloudflare Workers. # Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#hyperdrive From 13ceb0ed8b4ee9060ff58675ba6a3e27a0420f74 Mon Sep 17 00:00:00 2001 From: cod1k Date: Tue, 8 Jul 2025 11:20:12 +0300 Subject: [PATCH 2/9] 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. --- .../cloudflare/test/durableobject.test.ts | 70 ++++++++++++++----- 1 file changed, 54 insertions(+), 16 deletions(-) diff --git a/packages/cloudflare/test/durableobject.test.ts b/packages/cloudflare/test/durableobject.test.ts index 40d33741658e..e3ed068f6ace 100644 --- a/packages/cloudflare/test/durableobject.test.ts +++ b/packages/cloudflare/test/durableobject.test.ts @@ -1,30 +1,68 @@ import type { ExecutionContext } from '@cloudflare/workers-types'; import * as SentryCore from '@sentry/core'; -import { describe, expect, it, onTestFinished, vi } from 'vitest'; -import { instrumentDurableObjectWithSentry } from '../src/durableobject'; +import { afterEach, describe, expect, it, onTestFinished, vi } from 'vitest'; +import { instrumentDurableObjectWithSentry } from '../src'; import { isInstrumented } from '../src/instrument'; -describe('durable object', () => { - it('instrumentDurableObjectWithSentry generic functionality', () => { +describe('instrumentDurableObjectWithSentry', () => { + afterEach(() => { + vi.restoreAllMocks(); + }); + it('Generic functionality', () => { const options = vi.fn(); const instrumented = instrumentDurableObjectWithSentry(options, vi.fn()); expect(instrumented).toBeTypeOf('function'); expect(() => Reflect.construct(instrumented, [])).not.toThrow(); expect(options).toHaveBeenCalledOnce(); }); - it('all available durable object methods are instrumented', () => { - const testClass = vi.fn(() => ({ - customMethod: vi.fn(), - fetch: vi.fn(), - alarm: vi.fn(), - webSocketMessage: vi.fn(), - webSocketClose: vi.fn(), - webSocketError: vi.fn(), - })); + it('Instruments prototype methods without "sticking" to the options', () => { + const initCore = vi.spyOn(SentryCore, 'initAndBind'); + vi.spyOn(SentryCore, 'getClient').mockReturnValue(undefined); + const options = vi + .fn() + .mockReturnValueOnce({ + orgId: 1, + }) + .mockReturnValueOnce({ + orgId: 2, + }); + const testClass = class { + method() {} + }; + (Reflect.construct(instrumentDurableObjectWithSentry(options, testClass as any), []) as any).method(); + (Reflect.construct(instrumentDurableObjectWithSentry(options, testClass as any), []) as any).method(); + expect(initCore).nthCalledWith(1, expect.any(Function), expect.objectContaining({ orgId: 1 })); + expect(initCore).nthCalledWith(2, expect.any(Function), expect.objectContaining({ orgId: 2 })); + }); + it('All available durable object methods are instrumented', () => { + const testClass = class { + propertyFunction = vi.fn(); + + rpcMethod() {} + + fetch() {} + + alarm() {} + + webSocketMessage() {} + + webSocketClose() {} + + webSocketError() {} + }; const instrumented = instrumentDurableObjectWithSentry(vi.fn(), testClass as any); - const dObject: any = Reflect.construct(instrumented, []); - for (const method of Object.getOwnPropertyNames(dObject)) { - expect(isInstrumented(dObject[method]), `Method ${method} is instrumented`).toBeTruthy(); + const obj = Reflect.construct(instrumented, []); + expect(Object.getPrototypeOf(obj), 'Prototype is instrumented').not.toBe(testClass.prototype); + expect(isInstrumented((obj as any)['rpcMethod']), 'RPC method').toBeFalsy(); + for (const method_name of [ + 'propertyFunction', + 'fetch', + 'alarm', + 'webSocketMessage', + 'webSocketClose', + 'webSocketError', + ]) { + expect(isInstrumented((obj as any)[method_name]), `Method ${method_name} is instrumented`).toBeTruthy(); } }); it('flush performs after all waitUntil promises are finished', async () => { From 48f4762f08c866d6b1c8e0fe88da260dadca9091 Mon Sep 17 00:00:00 2001 From: cod1k Date: Tue, 8 Jul 2025 11:20:37 +0300 Subject: [PATCH 3/9] Refactor Durable Object method wrapping for Sentry integration Introduce `instrumentPrototype` to enhance Sentry instrumentation by proxying prototypes and managing method wrapping at a granular level. Improves maintainability and modularity of the code for durable objects. --- packages/cloudflare/src/durableobject.ts | 57 +++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/packages/cloudflare/src/durableobject.ts b/packages/cloudflare/src/durableobject.ts index 0e919977025d..d4f85fa5cfde 100644 --- a/packages/cloudflare/src/durableobject.ts +++ b/packages/cloudflare/src/durableobject.ts @@ -25,7 +25,9 @@ type MethodWrapperOptions = { }; // eslint-disable-next-line @typescript-eslint/no-explicit-any -function wrapMethodWithSentry any>( +type OriginalMethod = (...args: any[]) => any; + +function wrapMethodWithSentry( wrapperOptions: MethodWrapperOptions, handler: T, callback?: (...args: Parameters) => void, @@ -221,8 +223,61 @@ export function instrumentDurableObjectWithSentry< ); } } + const instrumentedPrototype = instrumentPrototype(target, options, context); + Object.setPrototypeOf(obj, instrumentedPrototype); return obj; }, }); } + +function instrumentPrototype( + target: T, + options: CloudflareOptions, + context: MethodWrapperOptions['context'], +): typeof target.prototype { + const sentryMethods = new Map(); + let proto = target.prototype; + const instrumentedPrototype = new Proxy(proto, { + get(target, prop, receiver) { + if (sentryMethods.has(prop)) { + return sentryMethods.get(prop); + } + return Reflect.get(target, prop, receiver); + }, + }); + while (proto && proto !== Object.prototype) { + for (const method of Object.getOwnPropertyNames(proto)) { + if (method === 'constructor' || sentryMethods.has(method)) { + continue; + } + + const value = Reflect.get(proto, method, proto); + if (typeof value === 'function') { + sentryMethods.set( + method, + wrapMethodWithSentry( + { + options, + context, + spanName: method, + spanOp: 'rpc', + }, + // + new Proxy(value, { + set(target, p, newValue, receiver): boolean { + if ('__SENTRY_INSTRUMENTED__' === p) { + return true; + } + return Reflect.set(target, p, newValue, receiver); + }, + }), + // + ), + ); + } + } + proto = Object.getPrototypeOf(proto); + } + return instrumentedPrototype; +} From 52a4fa1307baa6898d644fd18f7e184696fe38ac Mon Sep 17 00:00:00 2001 From: cod1k Date: Tue, 8 Jul 2025 12:53:54 +0300 Subject: [PATCH 4/9] Update Playwright config to use dynamic app port variable Replaced hardcoded port value with a dynamic variable for better maintainability and consistency. This change ensures the port is defined in a single location, reducing the risk of errors during updates. --- .../cloudflare-workers/playwright.config.ts | 6 ++++-- .../cloudflare-workers/tests/index.test.ts | 20 +++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/playwright.config.ts b/dev-packages/e2e-tests/test-applications/cloudflare-workers/playwright.config.ts index b90ce4dadc01..c69c955fafd8 100644 --- a/dev-packages/e2e-tests/test-applications/cloudflare-workers/playwright.config.ts +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/playwright.config.ts @@ -5,10 +5,12 @@ if (!testEnv) { throw new Error('No test env defined'); } +const APP_PORT = 38787; + const config = getPlaywrightConfig( { - startCommand: 'pnpm dev', - port: 8787, + startCommand: `pnpm dev --port ${APP_PORT}`, + port: APP_PORT, }, { // This comes with the risk of tests leaking into each other but the tests run quite slow so we should parallelize diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/index.test.ts b/dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/index.test.ts index 3b2238b4c466..35793341d04c 100644 --- a/dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/index.test.ts +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/index.test.ts @@ -1,36 +1,36 @@ import { expect, test } from '@playwright/test'; import { waitForError } from '@sentry-internal/test-utils'; -test('Index page', async ({ page }) => { - const result = await page.goto('http://localhost:8787/'); - expect(result?.status?.()).toBe(200); - await expect(page.textContent('body > pre')).resolves.toBe('Hello World!'); +test('Index page', async ({ baseURL }) => { + const result = await fetch(baseURL!); + expect(result.status).toBe(200); + await expect(result.text()).resolves.toBe('Hello World!'); }) -test('worker\'s withSentry', async () => { +test('worker\'s withSentry', async ({baseURL}) => { const eventWaiter = waitForError('cloudflare-workers', (event) => { return event.exception?.values?.[0]?.mechanism?.type === 'cloudflare'; }); - const response = await fetch('http://localhost:8787/throwException'); + const response = await fetch(`${baseURL}/throwException`); expect(response.status).toBe(500); const event = await eventWaiter; expect(event.exception?.values?.[0]?.value).toBe('To be recorded in Sentry.'); }) -test('RPC method which throws an exception to be logged to sentry', async () => { +test('RPC method which throws an exception to be logged to sentry', async ({baseURL}) => { const eventWaiter = waitForError('cloudflare-workers', (event) => { return event.exception?.values?.[0]?.mechanism?.type === 'cloudflare_durableobject'; }); - const response = await fetch('http://localhost:8787/rpc/throwException'); + const response = await fetch(`${baseURL}/rpc/throwException`); expect(response.status).toBe(500); const event = await eventWaiter; expect(event.exception?.values?.[0]?.value).toBe('Should be recorded in Sentry.'); }); -test('Request processed by DurableObject\'s fetch is recorded', async () => { +test('Request processed by DurableObject\'s fetch is recorded', async ({baseURL}) => { const eventWaiter = waitForError('cloudflare-workers', (event) => { return event.exception?.values?.[0]?.mechanism?.type === 'cloudflare_durableobject'; }); - const response = await fetch('http://localhost:8787/pass-to-object/throwException'); + const response = await fetch(`${baseURL}/pass-to-object/throwException`); expect(response.status).toBe(500); const event = await eventWaiter; expect(event.exception?.values?.[0]?.value).toBe('Should be recorded in Sentry.'); From 860581609c1f30bae92af8eefde8fd8a123f24e5 Mon Sep 17 00:00:00 2001 From: cod1k Date: Tue, 8 Jul 2025 16:32:47 +0300 Subject: [PATCH 5/9] Refactor Durable Object instrumentation logic Simplified the prototype instrumentation logic by replacing the manual mapping of methods with proxies. Added tests to ensure prototype methods are properly instrumented and maintain consistency. --- packages/cloudflare/src/durableobject.ts | 69 ++++++++----------- .../cloudflare/test/durableobject.test.ts | 9 ++- 2 files changed, 35 insertions(+), 43 deletions(-) diff --git a/packages/cloudflare/src/durableobject.ts b/packages/cloudflare/src/durableobject.ts index d4f85fa5cfde..d3f304447204 100644 --- a/packages/cloudflare/src/durableobject.ts +++ b/packages/cloudflare/src/durableobject.ts @@ -31,12 +31,15 @@ function wrapMethodWithSentry( wrapperOptions: MethodWrapperOptions, handler: T, callback?: (...args: Parameters) => void, + noMark?: true, ): T { if (isInstrumented(handler)) { return handler; } - markAsInstrumented(handler); + if (!noMark) { + markAsInstrumented(handler); + } return new Proxy(handler, { apply(target, thisArg, args: Parameters) { @@ -235,49 +238,31 @@ function instrumentPrototype( target: T, options: CloudflareOptions, context: MethodWrapperOptions['context'], -): typeof target.prototype { - const sentryMethods = new Map(); - let proto = target.prototype; - const instrumentedPrototype = new Proxy(proto, { +): T { + return new Proxy(target.prototype, { get(target, prop, receiver) { - if (sentryMethods.has(prop)) { - return sentryMethods.get(prop); + const value = Reflect.get(target, prop, receiver); + if (prop === 'constructor' || typeof value !== 'function') { + return value; } - return Reflect.get(target, prop, receiver); + const wrapped = wrapMethodWithSentry( + { options, context, spanName: prop.toString(), spanOp: 'rpc' }, + value, + undefined, + true, + ); + const instrumented = new Proxy(wrapped, { + get(target, p, receiver) { + if ('__SENTRY_INSTRUMENTED__' === p) { + return true; + } + return Reflect.get(target, p, receiver); + }, + }); + Object.defineProperty(receiver, prop, { + value: instrumented, + }); + return instrumented; }, }); - while (proto && proto !== Object.prototype) { - for (const method of Object.getOwnPropertyNames(proto)) { - if (method === 'constructor' || sentryMethods.has(method)) { - continue; - } - - const value = Reflect.get(proto, method, proto); - if (typeof value === 'function') { - sentryMethods.set( - method, - wrapMethodWithSentry( - { - options, - context, - spanName: method, - spanOp: 'rpc', - }, - // - new Proxy(value, { - set(target, p, newValue, receiver): boolean { - if ('__SENTRY_INSTRUMENTED__' === p) { - return true; - } - return Reflect.set(target, p, newValue, receiver); - }, - }), - // - ), - ); - } - } - proto = Object.getPrototypeOf(proto); - } - return instrumentedPrototype; } diff --git a/packages/cloudflare/test/durableobject.test.ts b/packages/cloudflare/test/durableobject.test.ts index e3ed068f6ace..b627c4051b41 100644 --- a/packages/cloudflare/test/durableobject.test.ts +++ b/packages/cloudflare/test/durableobject.test.ts @@ -15,6 +15,13 @@ describe('instrumentDurableObjectWithSentry', () => { expect(() => Reflect.construct(instrumented, [])).not.toThrow(); expect(options).toHaveBeenCalledOnce(); }); + it('Instruments prototype methods and defines implementation in the object', () => { + const testClass = class { + method() {} + }; + const obj = Reflect.construct(instrumentDurableObjectWithSentry(vi.fn(), testClass as any), []) as any; + expect(obj.method).toBe(obj.method); + }); it('Instruments prototype methods without "sticking" to the options', () => { const initCore = vi.spyOn(SentryCore, 'initAndBind'); vi.spyOn(SentryCore, 'getClient').mockReturnValue(undefined); @@ -53,7 +60,6 @@ describe('instrumentDurableObjectWithSentry', () => { const instrumented = instrumentDurableObjectWithSentry(vi.fn(), testClass as any); const obj = Reflect.construct(instrumented, []); expect(Object.getPrototypeOf(obj), 'Prototype is instrumented').not.toBe(testClass.prototype); - expect(isInstrumented((obj as any)['rpcMethod']), 'RPC method').toBeFalsy(); for (const method_name of [ 'propertyFunction', 'fetch', @@ -61,6 +67,7 @@ describe('instrumentDurableObjectWithSentry', () => { 'webSocketMessage', 'webSocketClose', 'webSocketError', + 'rpcMethod', ]) { expect(isInstrumented((obj as any)[method_name]), `Method ${method_name} is instrumented`).toBeTruthy(); } From 6bee74cb073754d6fa7f17bfaf1255c2c008a1e2 Mon Sep 17 00:00:00 2001 From: cod1k Date: Tue, 8 Jul 2025 17:13:24 +0300 Subject: [PATCH 6/9] Make Durable Object properties enumerable and configurable Previously, properties added to the Durable Object receiver were not enumerable, writable, or configurable by default. This change ensures these properties have full flexibility, improving compatibility and developer experience. --- packages/cloudflare/src/durableobject.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/cloudflare/src/durableobject.ts b/packages/cloudflare/src/durableobject.ts index d3f304447204..c9de8763750c 100644 --- a/packages/cloudflare/src/durableobject.ts +++ b/packages/cloudflare/src/durableobject.ts @@ -261,6 +261,9 @@ function instrumentPrototype( }); Object.defineProperty(receiver, prop, { value: instrumented, + enumerable: true, + writable: true, + configurable: true, }); return instrumented; }, From 5322f4682e06a0f136532f7b12f90a55abff9b42 Mon Sep 17 00:00:00 2001 From: 0xbad0c0d3 <0xbad0c0d3@gmail.com> Date: Tue, 8 Jul 2025 19:20:17 +0300 Subject: [PATCH 7/9] Remove sentryTest.optional=true --- .../test-applications/cloudflare-workers/package.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json b/dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json index 0e1f1217ffce..4da31ab40fdf 100644 --- a/dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json @@ -34,8 +34,5 @@ "overrides": { "strip-literal": "~2.0.0" } - }, - "sentryTest": { - "optional": true } } From 43bcb2eceff72f9c41d68d4c64222040a3089d7f Mon Sep 17 00:00:00 2001 From: 0xbad0c0d3 <0xbad0c0d3@gmail.com> Date: Tue, 8 Jul 2025 19:39:36 +0300 Subject: [PATCH 8/9] Not needed because of tmp-dir-build --- .../test-applications/cloudflare-workers/package.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json b/dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json index 4da31ab40fdf..6a9dd726c670 100644 --- a/dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json @@ -8,12 +8,11 @@ "build": "wrangler deploy --dry-run", "test": "vitest --run", "typecheck": "tsc --noEmit", - "clean:state": "rimraf .wrangler", "cf-typegen": "wrangler types", "test:build": "pnpm install && pnpm build", "test:assert": "pnpm test:dev && pnpm test:prod", - "test:prod": "pnpm clean:state && TEST_ENV=production playwright test", - "test:dev": "pnpm clean:state && TEST_ENV=development playwright test" + "test:prod": "TEST_ENV=production playwright test", + "test:dev": "TEST_ENV=development playwright test" }, "dependencies": { "@sentry/cloudflare": "latest || *" From b56cd358175fd6fe222eae24f582adab9d136d5a Mon Sep 17 00:00:00 2001 From: cod1k Date: Wed, 9 Jul 2025 14:06:30 +0300 Subject: [PATCH 9/9] Add WebSocket support and Sentry tracking in Cloudflare Workers Implemented WebSocket handling with message reception and connection closure logic, including error recording in Sentry. Updated tests to verify WebSocket behaviors and added the "ws" package for WebSocket functionality. --- .../cloudflare-workers/package.json | 3 +- .../cloudflare-workers/src/index.ts | 45 ++++++++++++++----- .../cloudflare-workers/tests/index.test.ts | 30 +++++++++++++ 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json b/dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json index 6a9dd726c670..91a49e0788f4 100644 --- a/dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json @@ -24,7 +24,8 @@ "@sentry-internal/test-utils": "link:../../../test-utils", "typescript": "^5.5.2", "vitest": "~3.2.0", - "wrangler": "^4.23.0" + "wrangler": "^4.23.0", + "ws": "^8.18.3" }, "volta": { "extends": "../../package.json" diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/src/index.ts b/dev-packages/e2e-tests/test-applications/cloudflare-workers/src/index.ts index aed9ab7fa955..f329cea238e8 100644 --- a/dev-packages/e2e-tests/test-applications/cloudflare-workers/src/index.ts +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/src/index.ts @@ -14,17 +14,42 @@ import * as Sentry from '@sentry/cloudflare'; import { DurableObject } from "cloudflare:workers"; class MyDurableObjectBase extends DurableObject { - async throwException(): Promise { - throw new Error("Should be recorded in Sentry.") - } + private throwOnExit = new WeakMap(); + async throwException(): Promise { + throw new Error('Should be recorded in Sentry.'); + } - async fetch(request: Request){ - const {pathname} = new URL(request.url) - if(pathname === '/throwException'){ - await this.throwException() - } - return new Response('DO is fine') - } + async fetch(request: Request) { + const { pathname } = new URL(request.url); + switch (pathname) { + case '/throwException': { + await this.throwException(); + break; + } + case '/ws': + const webSocketPair = new WebSocketPair(); + const [client, server] = Object.values(webSocketPair); + this.ctx.acceptWebSocket(server); + return new Response(null, { status: 101, webSocket: client }); + } + return new Response('DO is fine'); + } + + webSocketMessage(ws: WebSocket, message: string | ArrayBuffer): void | Promise { + if (message === 'throwException') { + throw new Error('Should be recorded in Sentry: webSocketMessage'); + } else if (message === 'throwOnExit') { + this.throwOnExit.set(ws, new Error('Should be recorded in Sentry: webSocketClose')); + } + } + + webSocketClose(ws: WebSocket): void | Promise { + if (this.throwOnExit.has(ws)) { + const error = this.throwOnExit.get(ws)!; + this.throwOnExit.delete(ws); + throw error; + } + } } export const MyDurableObject = Sentry.instrumentDurableObjectWithSentry( diff --git a/dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/index.test.ts b/dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/index.test.ts index 35793341d04c..ac8f2e38952e 100644 --- a/dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/index.test.ts +++ b/dev-packages/e2e-tests/test-applications/cloudflare-workers/tests/index.test.ts @@ -1,5 +1,6 @@ import { expect, test } from '@playwright/test'; import { waitForError } from '@sentry-internal/test-utils'; +import {WebSocket} from 'ws' test('Index page', async ({ baseURL }) => { const result = await fetch(baseURL!); @@ -35,3 +36,32 @@ test('Request processed by DurableObject\'s fetch is recorded', async ({baseURL} const event = await eventWaiter; expect(event.exception?.values?.[0]?.value).toBe('Should be recorded in Sentry.'); }); +test('Websocket.webSocketMessage', async ({baseURL}) => { + const eventWaiter = waitForError('cloudflare-workers', (event) => { + return event.exception?.values?.[0]?.mechanism?.type === 'cloudflare_durableobject'; + }); + const url = new URL('/pass-to-object/ws', baseURL); + url.protocol = url.protocol.replace('http', 'ws'); + const socket = new WebSocket(url.toString()); + socket.addEventListener('open', () => { + socket.send('throwException') + }); + const event = await eventWaiter; + socket.close(); + expect(event.exception?.values?.[0]?.value).toBe('Should be recorded in Sentry: webSocketMessage'); +}) + +test('Websocket.webSocketClose', async ({baseURL}) => { + const eventWaiter = waitForError('cloudflare-workers', (event) => { + return event.exception?.values?.[0]?.mechanism?.type === 'cloudflare_durableobject'; + }); + const url = new URL('/pass-to-object/ws', baseURL); + url.protocol = url.protocol.replace('http', 'ws'); + const socket = new WebSocket(url.toString()); + socket.addEventListener('open', () => { + socket.send('throwOnExit') + socket.close() + }); + const event = await eventWaiter; + expect(event.exception?.values?.[0]?.value).toBe('Should be recorded in Sentry: webSocketClose'); +})