Browse Source

test: add MessageChannel polyfill for jest/jsdom environments

pull/11604/head
afc163 3 months ago
parent
commit
27734cd0f5
  1. 37
      tests/setupTests.jsx

37
tests/setupTests.jsx

@ -27,6 +27,43 @@ class Worker {
} }
} }
window.Worker = 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') { if (typeof window !== 'undefined') {
// ref: https://github.com/ant-design/ant-design/issues/18774 // ref: https://github.com/ant-design/ant-design/issues/18774

Loading…
Cancel
Save