Closed lasseklovstad closed 3 weeks ago
Hi, @lasseklovstad. Thanks for reporting this!
I will take a look at this once I have a moment, I think I know the root cause of this.
The issue is caused by Next.js defining the global WebSocket
class (as it's missing in Node.js v20) in a way that forbids overrides:
This property definition doesn't support setters and doesn't have configurable: true
to re-define this property. This is an extremely restrictive way to re-define a global.
Ideally, open a PR to Next to define that property with configurable: true
. They don't own the WebSocket
class, but even if they did, defining a global this way is too restrictive. This is a similar issue to how they used to define fetch
in a way that was not overridable.
I've opened a pull request to Next.js to fix this issue: https://github.com/vercel/next.js/pull/72079. Hope the team will have time to look into this. Meanwhile, closed. Not an MSW issue. Please follow the progress at the linked pull request.
I still think MSW should have a better developer experience around non-configurable globals, so I'm adding an error and disabling the interceptor if that's the case: https://github.com/mswjs/interceptors/pull/665. Will make it easier to debug for everyone.
Prerequisites
Environment check
msw
versionNode.js version
20
Reproduction repository
https://github.com/lasseklovstad/msw-websocker-bug-nextjs
Reproduction steps
Run
npm i
,npm run dev
and open the nextjs appCurrent behavior
node_modules\@mswjs\interceptors\lib\browser\interceptors\WebSocket\index.mjs (668:1) @ _WebSocketInterceptor.setup ⨯ TypeError: Cannot set property WebSocket of #<Object> which has only a getter
Expected behavior
Nextjs app should work as before.