-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.cloudflare.config.mts
More file actions
87 lines (82 loc) · 3.29 KB
/
Copy pathvitest.cloudflare.config.mts
File metadata and controls
87 lines (82 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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 AGENTBOX_*.
compatibilityFlags: ['nodejs_compat'],
bindings: {
AGENTBOX_API_KEY:
process.env.AGENTBOX_API_KEY ?? env.parsed?.AGENTBOX_API_KEY ?? '',
AGENTBOX_DOMAIN: process.env.AGENTBOX_DOMAIN ?? env.parsed?.AGENTBOX_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
},
},
}
}