What is the best approach to test some additional function that wrapps native method of WebSocket ?
The code could looks like this:
const ws = new WebSocket('ws://localhost:8888/');
// Additional handlers like onconnect, message etc...
// Before I start send anything I want to be able to process some information before sending it like this:
const sendMessage = (msg) => {
// do some operation on 'msg' argument... etc
// and finally send message via websocket
ws.send(msg);
}
The question is - is it good enought to test ? Let's imagine that we need to fire it instantly and unit tests have connected to WebSocket. I can Mock it, but then I have no access to 'ws' which keeps WS connection.
Do you have some idea how to wrap native send method of WS by other function and make it possible to test easily ? I don't want to pass always 'ws' handler to sendMessage function - it would not be nice...
Thanks!
Aucun commentaire:
Enregistrer un commentaire