When using the WebSocket preview, the Node.js process doesn't exit properly. It does not matter if you actually create WebSocket links. This is a minimal reproduction:
const wsBroadcastChannel = new BroadcastChannel("msw:ws-client-manager");
This PR fixes the problem by calling .unref() in Node.js environments (docs). The typing is a bit ugly, but unref is only available in the type when importing from node:working_threads and not when using the global variable. The browser version of BroadcastChannel does not have an unref function, so the call has to be conditional.
When using the WebSocket preview, the Node.js process doesn't exit properly. It does not matter if you actually create WebSocket links. This is a minimal reproduction:
I added
why-is-node-running
to the script:This shows there is a
MessagePort
keeping the process running:It traces back to this line:
This PR fixes the problem by calling
.unref()
in Node.js environments (docs). The typing is a bit ugly, butunref
is only available in the type when importing fromnode:working_threads
and not when using the global variable. The browser version ofBroadcastChannel
does not have anunref
function, so the call has to be conditional.