question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support the native "fetch" in Node.js 17

See original GitHub issue

Environment

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:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cmolinacommented, Mar 22, 2022

@kettanaito you can see the PR where fetch was added: https://github.com/nodejs/node/pull/41749

1reaction
kettanaitocommented, Feb 22, 2022

Hey, @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 is 12.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 use http/https internally, and, perhaps, constructs a ClientRequest 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found