Files
node-intellicenter/tests/unit.test.ts
Parnic 458048b58a
All checks were successful
Node.js CI / build (18.x) (push) Successful in 16m17s
Node.js CI / build (20.x) (push) Successful in 11s
Node.js CI / build (22.x) (push) Successful in 11s
Reinstate ws WebSocket library
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.
2025-01-25 12:06:01 -06:00

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);
});
});