Support Node.js v18
See original GitHub issuePrerequisites
- 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 14 or higher
Node.js version
v18.8.0
Reproduction repository
https://github.com/ericmasiello/msw-node-18
Reproduction steps
- Verify you are using node 18.8.0
npm ci
npm run test
Note that the tests pass but they actually should not. I’m using a toMatchInlineSnapshot()
. That data snapshotted is the live data coming from real Pokemon API. what should be snapshotted is the mocked response I created in foo.test.ts
Current behavior
The tests pass because the msw is not intercepting the response. Therefore, the toMatchInlineSnapshot
matches live data from the API.
Expected behavior
The tests should fail because the data should match the mocked response in foo.test.ts
.
Issue Analytics
- State:
- Created a year ago
- Reactions:16
- Comments:13 (2 by maintainers)
Top Results From Across the Web
Node v18.12.0 (LTS)
This release marks the transition of Node.js 18.x into Long Term Support (LTS) with the codename 'Hydrogen'. The 18.x release line now moves ......
Read more >npm WARN: npm does not support Node.js v12.4.0
What worked for me: if you try running this command "npm cache clean -f" and it doesn't work,. on your windows machine(mine is...
Read more >Welcome Node.js 18 - Red Hat
Welcome Node.js 18 · Experimental Fetch · Test Runner · ECMAScript modules improvements · Improved support for AbortController and AbortSignal ...
Read more >AWS Lambda adds support for Node.js 18
AWS Lambda now supports Node.js 18 as both a managed runtime and a container base image. Developers creating serverless applications in ...
Read more >Node.js - endoflife.date
Release Released Active Support
19 2 months and 6 days ago. (18 Oct 2022) Ends in 3 months and 1 week. (01 Apr 2...
18...
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
What is left for this? Node 18.12.0 is now the LTS and this is preventing us from updating.
Hey! I actually have a PR for this. Well, I have one simple PR, and then a series of more robust PRs. Let me summarize…
Background
fetch
continues to be intercepted in Node 18 (mostly, I think)fetch
implementation. This might happen in tests that uselocalhost
on an IPv6 system, because in Node 18, the resolution order has changed to prefer the order of OS resolution, instead of the previous behavior of preferring IPv4. You can revert to the previous behavior with the optional Node flagnode --dns-result-order=ipv4first
. This exposes the actual bug in msw (and some of its dependencies), which is incorrect URL serialization of IPv6 hosts (should be surrounded by brackets but is not, e.g.http://::1:80
should behttp://[::1]:80
).globalThis.fetch
interceptor is able to intercept nativefetch
, butmsw
does not instantiateFetchInterceptor
duringcreateSetupServer
, nor is it exported, so there is no easy way to call it from user codeundici
as internal code, but there is no way to import its package directly, so if you want to e.g.setGlobalDispatcher
, you must addundici
to your project and import it from there.Quick fix
setupServer
will make it “mostly” work.yarn patch
or equivalent.)Robust fix
mswjs/interceptors
,page-with
andopen-draft/test-server
(source unavailable). In my PRs, I fixed these problems with hacky prototype patching, and @kettanaito also merged the fix intopage-with
(but still needs to release its package to npm, so I can remove the prototype patching and bump thepage-with
dependency version)Summary
To fully migrate to Node 18, we need to open/merge the following PRs:
page-with
package with the new versionpage-with
Note: These PRs are not as complicated/dangerous as they look. Most of the complexity only affects development/CI environment, and there are hardly any user-facing changes. And the high count of files changed is due to changing an import path to use the patched
@open-draft/test-server
.In the meantime, as a “quick fix,” we could make the two-line change described above (https://github.com/milesforks/msw/pull/1). If @kettanaito is okay with that approach, I can rebase my PR and open it here, or he can just make the change himself, since it’s literally two lines.
Blockers
page-with
to npm so I can rebase https://github.com/mswjs/interceptors/pull/283@open-draft/test-server
and release the new version of it. (optional because the hacky prototype patching in my PR works fine)Next steps
I spent most of last week on this (thanks to ipv6…), so I would love to get this merged! Please let me know how you’d like to coordinate the order of merges here, and if you have any input on that last memory leak blocking my PR from being ready.