我尝试过在Github上发帖,但到目前为止没有任何效果。我从Jest计时器中收到一些奇怪的错误,并且从Websocket中获得了一些未序列化的数据。我可以从我的套接字控制台记录消息,但是无法对其进行测试。我已经花了大约2天的时间来进行这项工作,而且我已经精疲力尽了。有任何想法吗?这是我的测试剩下的内容:
import { WebSocket as Socket, Server, CloseOptions } from "mock-socket";
import * as WebSocket from "ws";
let url: string;
url = "ws://localhost:8080";
const mockServer = new Server(url);
jest.useFakeTimers();
const msgs = <Array<WebSocket.Data>>[];
describe("Websockets", () => {
beforeEach(done => {
mockServer.on("connection", (socket: Socket) => {
socket.send("message");
done();
});
});
afterEach(() => {
let closer: CloseOptions;
closer = {
code: 0,
reason: "disconnected",
wasClean: true
};
mockServer.close(closer);
});
it("can communicate", done => {
const ws = new WebSocket(url);
ws.onmessage = async msg => {
await msg;
console.log(msg); // loggable
msgs.push(msg.data); // not writable
return msgs;
};
jest.runOnlyPendingTimers();
expect(msgs.length).toBe(1);
done();
});
});