Support the native "fetch" in Node.js 17
See original GitHub issueEnvironment
Name | Version |
---|---|
msw | 0.38.1 |
node | 17.5.0 |
OS | MacOS 12.0.1 |
Request handlers
import { setupServer } from 'msw/node'
import { rest } from 'msw'
const server = setupServer(
rest.get('https://hello.world', (request, response, context) => {
return response(
context.status(200),
context.json({ hello: 'world' }),
)
})
)
server.listen()
Actual request
const response = await fetch('https://hello.world')
const data = await response.json()
console.log(data)
Current behavior
Running the above setup using native fetch in node 17.5.0 (using the --experimental-fetch
flag) does not work as expected. Requests are not intercepted, and origins (in this case https://hello.world) are hit.
Expected behavior
Intercepting requests, as usual. 😁
While this issue uses a “bug” template, I believe that msw
simply does not support native node.js fetch
yet. Are there any roadmaps I may watch to know if that’s already work-in-progress?
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
The Fetch API is finally coming to Node.js - LogRocket Blog
It's been a long time coming, but the Fetch API is now available in Node.js core. Learn why it took so long and...
Read more >fetch() in Node v17.5 — A Review - JavaScript in Plain English
Node.js has announced that they are introducing support for fetch() as an experimental module with v17.5 and it will be widely available for...
Read more >fetch() In Node.js Core: Why You Should Care - Fusebit
Node 17.5 introduces support for the fetch() HTTP client, a new way to send requests to HTTP APIs.
Read more >how can i use native fetch with node in typescript (node v17.6)
As a hacky workaround, you can extend your lib section in tsconfig.json with "dom" . This includes the types for fetch but also...
Read more >Fetch API in Node.js - Atatus
In all popular browsers, the Fetch API has native support. The node-fetch package is used by JavaScript developers to create server-side ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kettanaito you can see the PR where
fetch
was added: https://github.com/nodejs/node/pull/41749Hey, @antoinerey. Thanks for reporting this.
Yeah, you’re right saying MSW does not have official support for native
fetch
in Node.js. Moreover, the latest official Node.js version we support is12.22.0
:https://github.com/mswjs/msw/blob/1a7bc36d98e0fa0d0fa1c9da6d7dd40605f239ca/.nvmrc#L1
It doesn’t mean you can’t use the library with newer versions. It only means that our entire testing suite runs against the Node.js version specified in
.nvmrc
, which makes it the latest officially supported version.Native fetch support
As with other requests in Node.js, supporting native
fetch
would imply analyzing how it works under the hood. Since it’s not being intercepted right now, I can assume that it doesn’t usehttp
/https
internally, and, perhaps, constructs aClientRequest
directly.If anybody is willing to dive into the Node.js source and write a summary on how
fetch
is implemented there, they would be doing 50% of the work necessary to support it in MSW.