msw: TypeError: Failed to fetch in a `miniflare-like` environment
Prerequisites
- I confirm my issue is not in the opened issues
- I confirm the Frequently Asked Questions didn’t contain the answer to my issue
Environment check
- I’m using the latest
msw
version - I’m using Node.js version 18 or higher
Node.js version
v20.1.0
Reproduction repository
https://github.com/monoald/auth-hono
Reproduction steps
- npm i
- npm test
Current behavior
console.error
Trace: TypeError: Failed to fetch
at createNetworkError (/home/monoald/project/packages/hono-auth/node_modules/@mswjs/interceptors/lib/node/interceptors/fetch/index.js:140:24)
at globalThis.fetch (/home/monoald/project/packages/hono-auth/node_modules/@mswjs/interceptors/lib/node/interceptors/fetch/index.js:80:31)
at AuthFlow.getTokenFromCode (/home/monoald/project/packages/hono-auth/src/service/google/authFlow.ts:102:22)
at AuthFlow.getUserData (/home/monoald/project/packages/hono-auth/src/service/google/authFlow.ts:136:40)
at /home/monoald/project/packages/hono-auth/src/service/google/googleAuth.ts:48:5
at /home/monoald/project/packages/hono-auth/node_modules/hono/dist/cjs/hono-base.js:260:62 {
cause: TypeError: unusable
at Request.clone (/home/monoald/project/packages/hono-auth/node_modules/@miniflare/core/node_modules/undici/lib/fetch/request.js:688:13)
at Request.clone (/home/monoald/project/packages/hono-auth/node_modules/@miniflare/core/src/standards/http.ts:478:38)
at HttpHandler.<anonymous> (/home/monoald/project/packages/hono-auth/node_modules/msw/lib/core/handlers/RequestHandler.js:112:43)
at Generator.next (<anonymous>)
at /home/monoald/project/packages/hono-auth/node_modules/msw/lib/core/handlers/RequestHandler.js:53:61
at new Promise (<anonymous>)
at __async (/home/monoald/project/packages/hono-auth/node_modules/msw/lib/core/handlers/RequestHandler.js:37:10)
at HttpHandler.run (/home/monoald/project/packages/hono-auth/node_modules/msw/lib/core/handlers/RequestHandler.js:107:12)
at /home/monoald/project/packages/hono-auth/node_modules/msw/lib/core/utils/getResponse.js:48:28
at Generator.next (<anonymous>)
at /home/monoald/project/packages/hono-auth/node_modules/msw/lib/core/utils/getResponse.js:36:61
at new Promise (<anonymous>)
at __async (/home/monoald/project/packages/hono-auth/node_modules/msw/lib/core/utils/getResponse.js:20:10)
at getResponse (/home/monoald/project/packages/hono-auth/node_modules/msw/lib/core/utils/getResponse.js:44:63)
at /home/monoald/project/packages/hono-auth/node_modules/msw/lib/core/utils/handleRequest.js:58:49
at until (/home/monoald/project/packages/hono-auth/node_modules/@open-draft/until/src/until.ts:23:24)
at /home/monoald/project/packages/hono-auth/node_modules/msw/lib/core/utils/handleRequest.js:57:55
at Generator.next (<anonymous>)
at /home/monoald/project/packages/hono-auth/node_modules/msw/lib/core/utils/handleRequest.js:36:61
at new Promise (<anonymous>)
at __async (/home/monoald/project/packages/hono-auth/node_modules/msw/lib/core/utils/handleRequest.js:20:10)
at handleRequest (/home/monoald/project/packages/hono-auth/node_modules/msw/lib/core/utils/handleRequest.js:49:10)
at SetupServerApi.<anonymous> (/home/monoald/project/packages/hono-auth/node_modules/msw/lib/node/index.js:93:69)
at Generator.next (<anonymous>)
at /home/monoald/project/packages/hono-auth/node_modules/msw/lib/node/index.js:36:61
at new Promise (<anonymous>)
...
...
Trying to test using Hono JS, I followed all the necessary setup for jest, but still get the error.
Expected behavior
The test should run without any error and mock fetch request.
About this issue
- Original URL
- State: closed
- Created 8 months ago
- Reactions: 1
- Comments: 16 (11 by maintainers)
I’ve opened PRs in both
These 2 prs update the code which seems to cause issues in your tests, and should guard against future regressions in those dependencies, since I’ve added a
miniflare
environment test to each.I also opened a failing test using the
miniflare
environment on this repoOnce we land the 2 linked prs from dependencies, and bump their versions, I expect the regression test that’s failing in #1840 will pass
Released: v2.0.4 🎉
This has been released in v2.0.4!
Make sure to always update to the latest version (
npm i msw@latest
) to get the newest features and bug fixes.Predictable release automation by @ossjs/release.
It looks like there are a few places we read properties unavailable in cloudflares Request/Response objects.
here are the lines I modified (in node_modules directly to get things cleared up)
first, the log lines: (this, being non-synchronous) will likely end after your test does and cause some warnings.
request.credentials
:response.type
- https://github.com/mswjs/interceptors/blob/3258f5cc62c472ba63f5c70e455d5b245c74a216/src/interceptors/fetch/index.ts#L106Once I wrap those (in msw and msw/cookies) in some try/catch logic, it seems like you’re missing a
scope
response fromI’m not familiar about whether we should wrap these, or test for them somehow, and that might need a bit of prodding,
@monoald I’ll keep this open until we fix the underlying msw code to work by default if that’s ok with you
yep - we should be able to avoid
patch-package
once some changes tomsw
are made to avoid accessing properties that throw in those environments. but may take a little bit to get those into a stable place and landed, so patching should at least unblock your progressonce that lands we should be able to update
msw
and add a test in the miniflare environment to help catch regressions, while similarly checking for access in this library before before using unimplemented prototype properties in miniflare/cloudflareIt is strange to me that miniflare/cloudflare error instead of ignoring these
In the short term, I think you can use something like
patch-package
to make similar changes to wrap atry {} catch {}
around the areas I linked above, and make the diff changes to the test - and get things looking pretty green@monoald I think there’s something here:
1 - the error you’re seeing is from your attempt to log
request.json
, I think you want toawait request.clone().json()
to copy that rquest properly before reading it, since a body can only be read onceOnce doing that, we do get a different error
Which looks like an issue with the way
Request
is implemented in miniflare/cloudflare workers?We may need to wrap reads of
credentials
to help support that environment