mirror of
https://github.com/parnic/node-intellicenter.git
synced 2025-06-16 18:20:14 -05:00
In some environments, such as MagicMirror modules, the WebSocket class is not defined. This is apparently a client-only class and generally only exists in browsers, but I'm confused how the unit tests were passing when run against just node. Either way, this means we have to disable the test again since ws is not compatible with jest-websocket-mock, and I haven't found an alternative mock server that is.
48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import { Unit } from "../src/index.js";
|
|
import * as messages from "../src/messages/messages.js";
|
|
import * as WS from "jest-websocket-mock";
|
|
import { xdescribe, beforeEach, afterEach, it } from "@jest/globals";
|
|
|
|
function makeid(length: number) {
|
|
const characters =
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
const charactersLength = characters.length;
|
|
|
|
let result = "";
|
|
let counter = 0;
|
|
while (counter < length) {
|
|
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
counter += 1;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// temporarily disabled: as long as Unit uses the "ws" library, it is incompatible with the jest-websocket-mock server
|
|
xdescribe("basic message tests", () => {
|
|
let unit: Unit;
|
|
let mockServer: WS.WS;
|
|
beforeEach(async () => {
|
|
mockServer = new WS.WS("ws://localhost:6680");
|
|
|
|
unit = new Unit("localhost", 6680);
|
|
await unit.connect();
|
|
await mockServer.connected;
|
|
});
|
|
|
|
afterEach(() => {
|
|
unit.close();
|
|
WS.WS.clean();
|
|
});
|
|
|
|
it("can send a message and return its response", async () => {
|
|
const msg = messages.GetBodyStatus();
|
|
const sender = unit.send(msg);
|
|
await expect(mockServer).toReceiveMessage(JSON.stringify(msg));
|
|
const response = { messageID: msg.messageID, command: makeid(8) };
|
|
mockServer.send(JSON.stringify(response));
|
|
const clientResp = await sender;
|
|
expect(clientResp).toEqual(response);
|
|
});
|
|
});
|