nuxt environment: `fetch(new Request(url))` fails with `Failed to parse URL from [object Request]`
Maintainer thường phản hồi trong vòng 2 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- node.js, nuxt, typescript
Hướng nghiên cứu
Start with src/runtime/shared/h3-v2.ts around the fetch fallback at line 36; the issue identifies the same line in src/runtime/shared/h3-v1.ts. Check how the wrapper builds url and init from a Request, then run the provided Vitest reproduction in the Nuxt environment and confirm the request reaches the refused-connection error. An open linked pull request (#1892) is already addressing this issue.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Environment
@nuxt/test-utils4.4.0 (the newest release; the same commit asmain,a817f987858e68144d093472b57fb85d0c8a09eb)nuxt-nightly5.0.0-2611151847-38ffb0e with h3 2.0.8- vitest 5.1.0, happy-dom 21.0.4, msw 3.0.6, vue 3.5.45
- Node.js v26.13.1, macOS
- Source of truth: the
nodeandhappy-domenvironments of the same vitest version
Reproduction
package.json:
{
"name": "repro",
"private": true,
"type": "module",
"dependencies": {
"@nuxt/test-utils": "4.4.0",
"happy-dom": "21.0.4",
"nuxt": "npm:[email protected]",
"vitest": "5.1.0",
"vue": "3.5.45"
}
}
nuxt.config.ts:
export default defineNuxtConfig({ compatibilityDate: "2026-11-01" });
vitest.config.ts:
import { defineVitestProject } from "@nuxt/test-utils/config";
export default await defineVitestProject({});
test/request.test.ts:
// @vitest-environment nuxt
import { expect, test } from "vitest";
test("fetch reads the URL of a Request", async () => {
const error = await fetch(new Request("http://127.0.0.1:50999/api")).then(
() => undefined,
(e: Error) => e,
);
expect(String(error?.message) + String(error?.cause)).toContain("ECONNREFUSED");
});
test/request-node.test.ts is the same file with // @vitest-environment node on its first line.
pnpm install
pnpm exec vitest run test/request.test.ts
pnpm exec vitest run test/request-node.test.ts
Describe the bug
In the nuxt environment, fetch(new Request(url)) fails with TypeError: Failed to parse URL from [object Request]. A string URL or a URL object works. The node and happy-dom environments accept the same call and report the refused connection, which names the host and the port of the Request.
msw 3 calls fetch with a Request object, so every msw 3 request fails in the nuxt environment. msw 2.15.0 passes the same test.
Cause. The fetch wrapper reads a Request into url and init (src/runtime/shared/h3-v2.ts#L20-L27). Its fallback then passes the original input to Node's fetch (#L36):
return _fetch(input, _init)
The Request global is happy-dom's class (populateGlobal). Node's fetch does not read a Request that is not its own class, and converts it to the text [object Request]. A copy of the installed dist/vitest-environment.mjs with this line changed to return _fetch(url, init) passes the test, and seven msw 3 request forms. src/runtime/shared/h3-v1.ts line 38 has the same line. It was not run.
The change that fixes it: pass url and init to _fetch in the fallback, not input and _init.
Real occurrence: https://github.com/Esposter/Esposter/blob/fe97e1c3cc/apps/web/app/services/trpc/mswTrpc.test.ts#L39
Additional context
Call in the nuxt environment |
Result (3 of 3 runs) |
|---|---|
fetch(url) |
fetch failed, cause connect ECONNREFUSED |
fetch(new URL(url)) |
fetch failed, cause connect ECONNREFUSED |
fetch(new Request(url)) |
Failed to parse URL from [object Request] |
fetch(new Request(url, { headers: new Headers({ a: "b" }) })) |
Failed to parse URL from [object Request] |
Not known: a stable Nuxt release, the h3 1 path and a request with a stream body were not run. By source only, the same line is in v3.20.0 and v4.0.0, so no regression is claimed.
Not a duplicate of: #798 (an ofetch.create override of $fetch), #775 and #1222 (msw and $fetch or useFetch, with no Request object and no error text), #943 (registerEndpoint and $fetch), and #1876 (merged in 4.4.0, header values in registerEndpoint; it does not change the fallback).
Logs
test/request.test.ts in the nuxt environment, 3 of 3 runs:
AssertionError: expected 'Failed to parse URL from [object Requ…' to contain 'ECONNREFUSED'
Expected: "ECONNREFUSED"
Received: "Failed to parse URL from [object Request]TypeError: Invalid URL"
test/request-node.test.ts in the node environment: passes, 3 of 3 runs.
🤖 Generated with Claude Code
- Ngôn ngữ chính
- TypeScript
- Star
- 445
- Fork
- 121
- Merge trung bình
- 3 ngày 11 giờ
- Pull request đã merge (30 ngày)
- 23
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của nuxt/test-utils
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 76/100
nuxt/test-utils#1889 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
nuxt/test-utils#1883 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
pending triage
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
nuxt/test-utils#1671 · 5 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 2 ngày
-
pending triage
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 52/100
nuxt/test-utils#1607 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
`@vitest-environment-options` is not workingCó thể đã có người làm @yamachi4416 đã nhận 210 ngày trước. Đang mởpending triage
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
nuxt/test-utils#1541 · 2 reaction ·
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của nuxt/test-utils
Issue tương tự
-
bug priority: low role: BE stage: now
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
ahmadrosid/nakama#2594 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
milind-soni/OpenMausBot#6495 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Navigating the session tree adds the branch's prompts to Up-arrow history againCó thể đã có người làm @acmerfight đã nhận hôm nay. Đang mởuntriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
earendil-works/pi#12279 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
approved documentation
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 89/100
vitest-dev/vitest#12015 · 4 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày