diff --git a/src/react/ssr/__tests__/getDataFromTree.test.tsx b/src/react/ssr/__tests__/getDataFromTree.test.tsx index 0d2f8e29362..65fe2f5a00f 100644 --- a/src/react/ssr/__tests__/getDataFromTree.test.tsx +++ b/src/react/ssr/__tests__/getDataFromTree.test.tsx @@ -1,19 +1,6 @@ +import "./messageChannel-polyfill"; import React from "react"; -if (React.version.startsWith("19")) { - // react-dom/server uses MessageChannel in React 19 - window.MessageChannel = jest.fn().mockImplementation(() => { - return { - port1: { - set onmessage(_cb: unknown) {}, - }, - port2: { - postMessage(_data: unknown) {}, - }, - }; - }); -} - import gql from "graphql-tag"; import { DocumentNode } from "graphql"; diff --git a/src/react/ssr/__tests__/messageChannel-polyfill.ts b/src/react/ssr/__tests__/messageChannel-polyfill.ts new file mode 100644 index 00000000000..3e55d66589d --- /dev/null +++ b/src/react/ssr/__tests__/messageChannel-polyfill.ts @@ -0,0 +1,17 @@ +import { MessageChannel as MC } from "node:worker_threads"; + +const messageChannels: MC[] = []; + +afterEach(() => { + let mc: MC; + while ((mc = messageChannels.pop())) { + mc.port1.close(); + mc.port2.close(); + } +}); +//@ts-ignore +globalThis.MessageChannel = function MessageChannel() { + const mc = new MC(); + messageChannels.push(mc); + return mc; +};