Testing BroadcastChannel API using JEST

797 Views Asked by At

I have implemented cross-browser tab communication using the broadcast channel API in my React Project. It's working as expected in the browser, but I am struggling to write unit tests for it using JEST. When I run the test case, the post message gets called, but the onmessage function never gets called. Can anyone help me?

const channel = new BroadcastChannel('some-name');

channel.onmessage = function(e) {
  // some code

}

 channel.postMessage({ message: 'some message' });
0

There are 0 best solutions below