Skip to content

Commit

Permalink
chore(tests): fix issues due to client#id type change
Browse files Browse the repository at this point in the history
  • Loading branch information
ZachHaber authored and darrachequesne committed Jan 8, 2024
1 parent f8d2644 commit 2c0a81c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/namespaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createClient,
successFn,
createPartialDone,
assert,
} from "./support/util";

describe("namespaces", () => {
Expand Down Expand Up @@ -417,6 +418,7 @@ describe("namespaces", () => {
socket1.on("a", successFn(done, io, socket1, socket2));

socket2.on("connect", () => {
assert(socket2.id);
io.except(socket2.id).emit("a");
});
});
Expand All @@ -435,6 +437,7 @@ describe("namespaces", () => {
socket1.on("a", successFn(done, io, socket1, socket2));

socket2.on("connect", () => {
assert(socket2.id);
nsp.except(socket2.id).emit("a");
});
});
Expand Down
10 changes: 10 additions & 0 deletions test/support/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ export function successFn(
return () => success(done, sio, ...clientSockets);
}

/**
* Asserts a condition so that typescript will recognize the assertion!
*
* Uses expect's `ok` check on condition
* @param condition
*/
export function assert(condition: any): asserts condition {
expect(condition).to.be.ok();
}

export function getPort(io: Server): number {
// @ts-ignore
return io.httpServer.address().port;
Expand Down
11 changes: 11 additions & 0 deletions test/uws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Server } from "..";
import { io as ioc, Socket as ClientSocket } from "socket.io-client";
import request from "supertest";
import expect from "expect.js";
import { assert } from "./support/util";

const createPartialDone = (done: (err?: Error) => void, count: number) => {
let i = 0;
Expand Down Expand Up @@ -134,6 +135,8 @@ describe("socket.io with uWebSocket.js-based engine", () => {
clientWSOnly.on("hello", partialDone);
clientPollingOnly.on("hello", partialDone);
clientCustomNamespace.on("hello", shouldNotHappen(done));
assert(clientWSOnly.id);
assert(clientPollingOnly.id);

io.of("/").sockets.get(clientWSOnly.id)!.join("room1");
io.of("/").sockets.get(clientPollingOnly.id)!.join("room1");
Expand All @@ -148,6 +151,8 @@ describe("socket.io with uWebSocket.js-based engine", () => {
clientWSOnly.on("hello", partialDone);
clientPollingOnly.on("hello", partialDone);
clientCustomNamespace.on("hello", shouldNotHappen(done));
assert(clientWSOnly.id);
assert(clientPollingOnly.id);

io.of("/").sockets.get(clientWSOnly.id)!.join("room1");
io.of("/").sockets.get(clientPollingOnly.id)!.join("room2");
Expand All @@ -163,6 +168,8 @@ describe("socket.io with uWebSocket.js-based engine", () => {
clientPollingOnly.on("hello", shouldNotHappen(done));
clientCustomNamespace.on("hello", shouldNotHappen(done));

assert(clientWSOnly.id);
assert(clientPollingOnly.id);
io.of("/").sockets.get(clientWSOnly.id)!.join("room1");
io.of("/").sockets.get(clientPollingOnly.id)!.join("room2");

Expand All @@ -177,6 +184,9 @@ describe("socket.io with uWebSocket.js-based engine", () => {
clientPollingOnly.on("hello", partialDone);
clientCustomNamespace.on("hello", shouldNotHappen(done));

assert(client.id);
assert(clientWSOnly.id);
assert(clientPollingOnly.id);
io.of("/").sockets.get(client.id)!.join("room1");
io.of("/").sockets.get(clientPollingOnly.id)!.join("room1");

Expand All @@ -189,6 +199,7 @@ describe("socket.io with uWebSocket.js-based engine", () => {
it("should not crash when socket is disconnected before the upgrade", (done) => {
client.on("disconnect", () => done());

assert(client.id);
io.of("/").sockets.get(client.id)!.disconnect();
});

Expand Down

0 comments on commit 2c0a81c

Please sign in to comment.