Skip to content

Commit 77525d4

Browse files
committed
fix: add web globals preload for Bun tests to fix Request polyfill issue
1 parent 2e64cfb commit 77525d4

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

bunfig.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ linkWorkspacePackages = true
77
[test]
88
# Exclude test repositories, integration tests, and Playwright e2e tests from test execution by default
99
exclude = ["evals/test-repos/**", "**/*.integration.test.*", "web/src/__tests__/e2e/**"]
10-
preload = ["./sdk/test/setup-env.ts", "./test/setup-bigquery-mocks.ts"]
10+
preload = ["./sdk/test/setup-env.ts", "./test/setup-bigquery-mocks.ts", "./web/test/setup-globals.ts"]

web/src/app/api/orgs/[orgId]/billing/portal/__tests__/org-billing-portal.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { describe, expect, mock, test } from 'bun:test'
2-
import { NextRequest } from 'next/server'
32

43
import type { Logger } from '@codebuff/common/types/contracts/logger'
54

65
import { postOrgBillingPortal } from '../_post'
76

8-
// NextRequest import above sets up the global Request polyfill needed by NextResponse
9-
void NextRequest
10-
117
import type {
128
CreateBillingPortalSessionFn,
139
GetMembershipFn,

web/src/app/api/user/billing-portal/__tests__/billing-portal.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { describe, expect, mock, test } from 'bun:test'
2-
import { NextRequest } from 'next/server'
32

43
import type { Logger } from '@codebuff/common/types/contracts/logger'
54

65
import { postBillingPortal } from '../_post'
76

8-
// NextRequest import above sets up the global Request polyfill needed by NextResponse
9-
void NextRequest
10-
117
import type { CreateBillingPortalSessionFn, GetSessionFn, Session } from '../_post'
128

139
const createMockLogger = (errorFn = mock(() => {})): Logger => ({

web/test/setup-globals.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Polyfill web globals for Bun tests that import Next.js server modules.
3+
*
4+
* Next.js's `next/server` module (NextRequest, NextResponse) expects the
5+
* standard web globals (Request, Response, Headers, fetch) to exist.
6+
* Bun provides these in its runtime, but they may not be available at
7+
* module load time during tests.
8+
*
9+
* This preload script ensures these globals are set up before any test
10+
* modules are imported.
11+
*/
12+
13+
// Bun has built-in support for web APIs, but we need to ensure they're
14+
// available on globalThis for Next.js server modules
15+
if (typeof globalThis.Request === 'undefined') {
16+
// @ts-expect-error - Bun provides these but TypeScript may not know
17+
globalThis.Request = Request
18+
}
19+
20+
if (typeof globalThis.Response === 'undefined') {
21+
// @ts-expect-error - Bun provides these but TypeScript may not know
22+
globalThis.Response = Response
23+
}
24+
25+
if (typeof globalThis.Headers === 'undefined') {
26+
// @ts-expect-error - Bun provides these but TypeScript may not know
27+
globalThis.Headers = Headers
28+
}
29+
30+
if (typeof globalThis.fetch === 'undefined') {
31+
// @ts-expect-error - Bun provides these but TypeScript may not know
32+
globalThis.fetch = fetch
33+
}

0 commit comments

Comments
 (0)