Issues Testing Web Worker with Timers in Jest – How to Receive Correct Countdown Messages?

export const timerWorker = () => {
    let timerStart = false;
    let intervalId = null;
    let timeLeft = 0;
    
    self.onmessage = (e) => {
        const { data = {} } = e;
        const { action, payload } = data;
        
        switch (action) {
            case 'start':
                start(payload);
                break;
            case 'stop':
                stop();
                break;
            default:
                self.postMessage('please pass a valid action');
                break;
        }
    };

    function start(payload) {
        const { interval = 1000, duration = 0 } = payload;
        stop();
        timeLeft = duration;
        if (duration >= 0) self.postMessage(timeLeft);
        intervalId = setInterval(function () {
            timeLeft -= interval;
            if (timeLeft <= 0) {
                stop();
            } else {
                self.postMessage(timeLeft);
            }
        }, interval);
        timerStart = true;
    }

    function stop() {
        clearInterval(intervalId);
        timerStart = false;
        timeLeft = 0;
        self.postMessage(timeLeft);
    }
};

// Create a Blob URL for the worker script
let code = timerWorker.toString();
code = code.substring(code.indexOf('{') + 1, code.lastIndexOf('}'));

const blob =
    typeof Blob !== 'undefined'
        ? new Blob([code], { type: 'application/javascript' })
        : {};

const worker_script =
    typeof Blob !== 'undefined' ? URL.createObjectURL(blob) : {};

// Export the blob URL as the worker script and the timerWorker function for testing
export { worker_script };

The above code contains the start stop functions for a timer countdown to tell the time left for the event to happen.

I am getting the error :

This is probably due to the self.on message being used. I am confused as to how I could write the code to test this timerworker.js file. I am not sure what targetOrigin means in this context. The fact that I am testing the worker and using its instance to post and listen to messages, I am not quite sure what targetOrigin has to be in the postmessage.

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

          timerStart = false;
          timeLeft = 0;
          self.postMessage(timeLeft);