Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
test: share one Cloudflare workerd vitest config across the JS SDKs (#1778)
devin-ai-integration[bot]7da6f09df6f5
diff --git a/package.json b/package.jsonindex d0a87ee46..a89b5116b 100644--- a/package.json+++ b/package.json@@ -21,7 +21,9 @@},"devDependencies": {"@changesets/cli": "2.31.1",+ "@cloudflare/vitest-pool-workers": "^0.18.7","@types/node": "catalog:",+ "dotenv": "^16.4.5","oxlint": "^1.78.0","prettier": "^3.6.2","tsdown": "catalog:",diff --git a/packages/code-interpreter-js/tests/runtimes/cloudflare/vitest.config.mts b/packages/code-interpreter-js/tests/runtimes/cloudflare/vitest.config.mtsindex 226e6139e..b5b071caa 100644--- a/packages/code-interpreter-js/tests/runtimes/cloudflare/vitest.config.mts+++ b/packages/code-interpreter-js/tests/runtimes/cloudflare/vitest.config.mts@@ -1,43 +1,5 @@-import { cloudflareTest } from '@cloudflare/vitest-pool-workers'-import { config } from 'dotenv'import { defineConfig } from 'vitest/config'-const env = config()+import { createCloudflareVitestConfig } from '../../../../../vitest.cloudflare.config.mts'-// Runs the same suite the node/bun/deno legs run, but inside Cloudflare's-// workerd via vitest-pool-workers, against src.-export default defineConfig({- plugins: [- cloudflareTest({- miniflare: {- compatibilityDate: '2026-03-01',- // nodejs_compat is a hard requirement for the SDK on Workers. It also- // mirrors the bindings below into process.env (default since compat- // date 2025-04-01), which is how tests and the SDK read E2B_*.- compatibilityFlags: ['nodejs_compat'],- bindings: {- E2B_API_KEY: process.env.E2B_API_KEY ?? env.parsed?.E2B_API_KEY ?? '',- E2B_DOMAIN: process.env.E2B_DOMAIN ?? env.parsed?.E2B_DOMAIN ?? '',- },- },- }),- ],- test: {- name: 'cloudflare',- include: ['tests/**/*.test.ts'],- globals: false,- testTimeout: 30_000,- bail: 0,- // workerd reports a rejection as unhandled unless a handler is attached- // within the same microtask drain, so the stream teardown of a sandbox the- // test kills (or interrupts) mid-request is reported even though the test- // awaits it. Drop only those two shapes; anything else still fails the run.- onUnhandledError(error) {- const message = String(error.message ?? '')- const expectedRejection =- error.type === 'Unhandled Rejection' &&- (message === 'Network connection lost.' || error.name === 'AbortError')- if (expectedRejection) return false- },- },-})+export default defineConfig(createCloudflareVitestConfig())diff --git a/packages/desktop-js/tests/runtimes/cloudflare/vitest.config.mts b/packages/desktop-js/tests/runtimes/cloudflare/vitest.config.mtsindex 31a82c426..2ff592880 100644--- a/packages/desktop-js/tests/runtimes/cloudflare/vitest.config.mts+++ b/packages/desktop-js/tests/runtimes/cloudflare/vitest.config.mts@@ -1,50 +1,13 @@-import { cloudflareTest } from '@cloudflare/vitest-pool-workers'-import { config } from 'dotenv'import { defineConfig } from 'vitest/config'-const env = config()+import {+ createCloudflareVitestConfig,+ isConnectRpcRejection,+} from '../../../../../vitest.cloudflare.config.mts'-// Runs the Desktop suite inside Cloudflare's workerd via vitest-pool-workers,-// against src — the same coverage as test:bun / test:deno get from the Node-// suite, but exercising the Workers runtime's fetch/streams implementations.-export default defineConfig({- plugins: [- cloudflareTest({- miniflare: {- compatibilityDate: '2026-03-01',- // nodejs_compat is a hard requirement for the SDK on Workers. It also- // mirrors the bindings below into process.env (default since compat- // date 2025-04-01), which is how tests and the SDK read E2B_*.- compatibilityFlags: ['nodejs_compat'],- bindings: {- E2B_API_KEY: process.env.E2B_API_KEY ?? env.parsed?.E2B_API_KEY ?? '',- E2B_DOMAIN: process.env.E2B_DOMAIN ?? env.parsed?.E2B_DOMAIN ?? '',- },- },- }),- ],- test: {- name: 'cloudflare',- include: ['tests/**/*.test.ts'],- exclude: ['tests/runtimes/**'],- globals: false,+export default defineConfig(+ createCloudflareVitestConfig({testTimeout: 60_000,- bail: 0,- // workerd reports a rejection as unhandled unless a handler is attached- // within the same microtask drain, and vitest never processes the- // rejectionhandled retraction, so the aborts the SDK itself provokes when- // a test's sandbox is killed mid-stream false-positive the run. Drop only- // those shapes; uncaught exceptions and any unknown rejection still fail.- onUnhandledError(error) {- const message = String(error.message ?? '')- const expectedRejection =- error.type === 'Unhandled Rejection' &&- (error.name === 'AbortError' ||- error.name === 'ConnectError' ||- message.startsWith('ConnectError:') ||- // workerd's teardown error for in-flight streams.- message === 'Network connection lost.')- if (expectedRejection) return false- },- },-})+ isExpectedRejection: isConnectRpcRejection,+ })+)diff --git a/packages/js-sdk/tests/runtimes/cloudflare/vitest.config.mts b/packages/js-sdk/tests/runtimes/cloudflare/vitest.config.mtsindex 85cae1af5..6b55b8a82 100644--- a/packages/js-sdk/tests/runtimes/cloudflare/vitest.config.mts+++ b/packages/js-sdk/tests/runtimes/cloudflare/vitest.config.mts@@ -1,8 +1,9 @@-import { cloudflareTest } from '@cloudflare/vitest-pool-workers'-import { config } from 'dotenv'import { defineConfig } from 'vitest/config'-const env = config()+import {+ createCloudflareVitestConfig,+ isConnectRpcRejection,+} from '../../../../../vitest.cloudflare.config.mts'// Error names thrown by src/errors.ts (plus CommandExitError) — the shapes// this suite's rejection tests expect. Kept as a literal list so an unknown@@ -27,64 +28,23 @@ const SDK_ERROR_NAMES = new Set([])// Runs the unit + connectionConfig projects (same coverage as test:bun /-// test:deno) inside Cloudflare's workerd via vitest-pool-workers, against src.-// The real-deploy suite (tests/runtimes/cloudflare-deploy) keeps covering the-// built bundle on actual Cloudflare infrastructure.-export default defineConfig({- plugins: [- cloudflareTest({- miniflare: {- compatibilityDate: '2026-03-01',- // nodejs_compat is a hard requirement for the SDK on Workers. It also- // mirrors the bindings below into process.env (default since compat- // date 2025-04-01), which is how tests and the SDK read E2B_*.- compatibilityFlags: ['nodejs_compat'],- bindings: {- E2B_API_KEY: process.env.E2B_API_KEY ?? env.parsed?.E2B_API_KEY ?? '',- E2B_DOMAIN: process.env.E2B_DOMAIN ?? env.parsed?.E2B_DOMAIN ?? '',- },- },- }),- ],- test: {- name: 'cloudflare',- include: ['tests/**/*.test.ts'],+// test:deno) inside workerd, against src. The real-deploy suite+// (tests/runtimes/cloudflare-deploy) keeps covering the built bundle on actual+// Cloudflare infrastructure.+export default defineConfig(+ createCloudflareVitestConfig({exclude: [- 'tests/runtimes/**','tests/template/**',// Inspects the host-built dist/index.mjs via node:fs, which workerd's// virtual filesystem can never see (and throws in CI when the file is// "missing"); the Node unit project keeps running it.'tests/bundle/**',],- globals: false,- testTimeout: 30_000,- bail: 0,- // workerd reports a rejection as unhandled unless a handler is attached- // within the same microtask drain, and vitest never processes the- // rejectionhandled retraction, so rejections the tests DO handle (the- // `await expect(p).rejects` pattern, including inline ones — workerd also- // flags intermediate promises of the async-function chain) false-positive- // ~60 times per run. Instead of dangerouslyIgnoreUnhandledErrors, drop- // only the rejection shapes these tests deliberately provoke; uncaught- // exceptions and any unknown rejection shape still fail the run.- onUnhandledError(error) {- const message = String(error.message ?? '')- const expectedRejection =- error.type === 'Unhandled Rejection' &&- // SDK public errors — what `expect(p).rejects` asserts on.- (SDK_ERROR_NAMES.has(error.name) ||- // The transport errors the SDK wraps: connect-rpc failures and- // aborted requests surface on intermediate promises too.- error.name === 'ConnectError' ||- message.startsWith('ConnectError:') ||- error.name === 'AbortError' ||- // workerd's teardown error for in-flight streams when a test kills- // the sandbox mid-request.- message === 'Network connection lost.' ||- // Stub rejection from tests/sandbox/git/validation.test.ts.- message === 'commands.run should not be called')- if (expectedRejection) return false- },- },-})+ isExpectedRejection: (error, message) =>+ // SDK public errors — what `expect(p).rejects` asserts on.+ SDK_ERROR_NAMES.has(String(error.name)) ||+ isConnectRpcRejection(error, message) ||+ // Stub rejection from tests/sandbox/git/validation.test.ts.+ message === 'commands.run should not be called',+ })+)diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 4b652335a..2e8431eb8 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -68,9 +68,15 @@ importers:'@changesets/cli':specifier: 2.31.1version: 2.31.1(@types/[email protected])+ '@cloudflare/vitest-pool-workers':+ specifier: ^0.18.7+ version: 0.18.7(@types/[email protected])(@vitest/[email protected])(@vitest/[email protected])([email protected])([email protected])([email protected])'@types/node':specifier: 'catalog:'version: 20.19.43+ dotenv:+ specifier: ^16.4.5+ version: 16.4.5oxlint:specifier: ^1.78.0version: 1.78.0diff --git a/vitest.cloudflare.config.mts b/vitest.cloudflare.config.mtsnew file mode 100644index 000000000..dd5f93060--- /dev/null+++ b/vitest.cloudflare.config.mts@@ -0,0 +1,87 @@+import { cloudflareTest } from '@cloudflare/vitest-pool-workers'+import { config } from 'dotenv'+import type { ViteUserConfig } from 'vitest/config'++type UnhandledError = Parameters<+ NonNullable<NonNullable<ViteUserConfig['test']>['onUnhandledError']>+>[0]++/**+ * The connect-rpc failures the SDK wraps — they surface on the intermediate+ * promises of the async-function chain too, so suites that assert on them with+ * `expect(p).rejects` need them dropped.+ */+export function isConnectRpcRejection(+ error: UnhandledError,+ message: string+): boolean {+ return error.name === 'ConnectError' || message.startsWith('ConnectError:')+}++export interface CloudflareVitestConfigOptions {+ /** Globs excluded on top of `tests/runtimes/**`. */+ exclude?: string[]+ testTimeout?: number+ /** Additional rejection shapes to drop, on top of the shared ones. */+ isExpectedRejection?: (error: UnhandledError, message: string) => boolean+}++/**+ * Runs a package's Node suite inside Cloudflare's workerd via+ * vitest-pool-workers, against src — the same coverage as the bun and deno+ * legs, but exercising the Workers runtime's fetch/streams implementations.+ */+export function createCloudflareVitestConfig({+ exclude = [],+ testTimeout = 30_000,+ isExpectedRejection,+}: CloudflareVitestConfigOptions = {}): ViteUserConfig {+ const env = config()++ return {+ plugins: [+ cloudflareTest({+ miniflare: {+ compatibilityDate: '2026-03-01',+ // nodejs_compat is a hard requirement for the SDK on Workers. It also+ // mirrors the bindings below into process.env (default since compat+ // date 2025-04-01), which is how tests and the SDK read E2B_*.+ compatibilityFlags: ['nodejs_compat'],+ bindings: {+ E2B_API_KEY:+ process.env.E2B_API_KEY ?? env.parsed?.E2B_API_KEY ?? '',+ E2B_DOMAIN: process.env.E2B_DOMAIN ?? env.parsed?.E2B_DOMAIN ?? '',+ },+ },+ }),+ ],+ test: {+ name: 'cloudflare',+ include: ['tests/**/*.test.ts'],+ exclude: ['tests/runtimes/**', ...exclude],+ globals: false,+ testTimeout,+ bail: 0,+ // workerd reports a rejection as unhandled unless a handler is attached+ // within the same microtask drain, and vitest never processes the+ // rejectionhandled retraction, so rejections the tests DO handle (the+ // `await expect(p).rejects` pattern, including the intermediate promises+ // of the async-function chain) false-positive the run. Instead of+ // dangerouslyIgnoreUnhandledErrors, drop only the rejection shapes these+ // suites deliberately provoke; uncaught exceptions and any unknown+ // rejection shape still fail the run.+ onUnhandledError(error) {+ if (error.type !== 'Unhandled Rejection') return+ const message = String(error.message ?? '')+ const expectedRejection =+ // Aborted requests surface on intermediate promises too.+ error.name === 'AbortError' ||+ // workerd's teardown error for in-flight streams when a test kills+ // the sandbox mid-request.+ message === 'Network connection lost.' ||+ isExpectedRejection?.(error, message) === true+ if (expectedRejection) return false+ },+ },+ }+}