42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
|
|
import { spawn, ChildProcessWithoutNullStreams } from "child_process";
|
|
import { WebSocket } from "ws";
|
|
|
|
describe("Test connections", () => {
|
|
let process: ChildProcessWithoutNullStreams;
|
|
|
|
beforeAll(async () => {
|
|
process = spawn("bun", ["run", "./index.ts"]);
|
|
await new Promise<void>((resolve, reject) => {
|
|
process.on("error", (err) => reject(err))
|
|
process.stdout.on("data", chunk => {
|
|
if (chunk.toString().includes("Listening")) {
|
|
resolve();
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
afterAll(() => {
|
|
process.kill();
|
|
});
|
|
|
|
it("test", async () => {
|
|
const ws1 = new WebSocket("ws://localhost:8080/");
|
|
const ws2 = new WebSocket("ws://localhost:8080/");
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
ws1.onerror = reject;
|
|
ws2.onerror = reject;
|
|
|
|
ws2.onmessage = (data) => {
|
|
expect(data.data).toBe("TEST");
|
|
resolve();
|
|
};
|
|
ws1.onopen = () => {
|
|
ws1.send("TEST");
|
|
};
|
|
});
|
|
});
|
|
});
|