0
votes

I am using jest in nextJs with Typescript project to mock a web Worker code. //worker.ts

addEventListener( 'message',handleMessage);
export async function handleMessage ( event )  {
  postMessage(['somemessage','somedata']);
}

//worker.test.ts

let postMessage = jest.fn();
  it(' should call post message ', async () => {

    await handleMessage('someeventdata');
    expect(postMessage).toBeCalled();

  });

output

TypeError: 'postMessage' requires 2 arguments: 'message' and 'targetOrigin'

why this mock is not working. I am able to mock the postMessage from main thread on worker e.g

someworker.postMessage = jest.fn(); // this test  works perfectly