From a3f8658a9ac905bb0122c34c2e6e4c5fbfec147f Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Fri, 13 Dec 2024 12:24:38 +0100 Subject: [PATCH] use `node:worker_threads` implementation of `MessageChannel` --- .../ssr/__tests__/getDataFromTree.test.tsx | 15 +-------------- .../ssr/__tests__/messageChannel-polyfill.ts | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 14 deletions(-) create mode 100644 src/react/ssr/__tests__/messageChannel-polyfill.ts diff --git a/src/react/ssr/__tests__/getDataFromTree.test.tsx b/src/react/ssr/__tests__/getDataFromTree.test.tsx index 0d2f8e2936..65fe2f5a00 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 0000000000..3e55d66589 --- /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; +};