I'm using Jest to test my server's status and I've been trying to use setTimeout, delays, while loops to check if the readyState changes from 0 - CONNECTING to 3 - CLOSED or 1 - OPEN. Just one problem, and that is that I've tried to use event listeners but since that gets executed later or never in this scenario after the function terminates, Jest provides an error
I've tried different functions like these ones
function checkServerStatus() {
const ws = new WebSocket('ws://127.0.0.1:8080');
let a;
ws.onopen = () => {
a = ws.readyState;
ws.close();
}
return a;
}