code: function EmulatedWebSocketServerFactory() {
          this.createServer = function (connectCallback) {
              var server = {
                  _connect: function (socket) {
                      var conn = {
                          _client: socket,
                          _listeners: { "text": [], "close": [] },
                          on: function (type, listener) {
                              conn._listeners[type].push(listener);
                          },
                          _fire: function (type, args) {
                              conn._listeners[type].forEach(function (listener) {
                                  listener.apply(null, args);
                              });
                          },
  
                          _text: function (msg) {
                              conn._fire("text", [msg]);
                          },
                          sendText: function (msg) {
                              setTimeout(function () {
                                  conn._client._text(msg);
                              }, 0);
                          },
                          close: function (code, reason) {
                              setTimeout(function () {
                                  conn._client._disconnect(code, reason);
                              }, 0);
                          },
                          _disconnect: function (code, reason) {
                              conn._fire("close", [code, reason]);
                          }
                      };
                      connectCallback(conn);
                      return conn;
                  },
                  listen: function (port) {
                      EmulatedWebSocketServerFactory.listeners[port] = this;
                      return this;
                  }
              }
              return server;
          }
      }
docs: To be able to test both server and client in a browser, emulate a WebSocketServer in the browser, and replace the WebSocket api with an emulated WebSocket that communicates with emulated WebSocketServer.