diff --git a/tests/setupTests.jsx b/tests/setupTests.jsx index de86de9f..7f7a4a39 100644 --- a/tests/setupTests.jsx +++ b/tests/setupTests.jsx @@ -27,6 +27,43 @@ class Worker { } } window.Worker = Worker; +// Polyfill MessageChannel for environments (like jest/jsdom) that don't provide it +if (typeof global.MessageChannel === 'undefined') { + class PolyMessageChannel { + constructor() { + const channel = this; + this.port1 = { + postMessage(msg) { + setTimeout(() => { + if ( + channel.port2 && + typeof channel.port2.onmessage === 'function' + ) { + channel.port2.onmessage({ data: msg }); + } + }, 0); + }, + }; + this.port2 = { + postMessage(msg) { + setTimeout(() => { + if ( + channel.port1 && + typeof channel.port1.onmessage === 'function' + ) { + channel.port1.onmessage({ data: msg }); + } + }, 0); + }, + }; + } + } + + global.MessageChannel = PolyMessageChannel; + if (typeof window !== 'undefined') { + window.MessageChannel = PolyMessageChannel; + } +} if (typeof window !== 'undefined') { // ref: https://github.com/ant-design/ant-design/issues/18774