use flush to fix tests
This commit is contained in:
+12
-3
@@ -6,15 +6,16 @@ const hostname = "0.0.0.0";
|
||||
const port = 8042;
|
||||
const href = `ws://${hostname}:${port}`;
|
||||
|
||||
async function connect(url: string): Promise<[WebSocket, string[]]> {
|
||||
return new Promise((resolve) => {
|
||||
function connect(url: string): Promise<[WebSocket, string[]]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const messages: string[] = [];
|
||||
const socket = new WebSocket(url);
|
||||
|
||||
socket.onopen = () => resolve([socket, messages]);
|
||||
socket.onerror = (error) => reject(error);
|
||||
socket.onmessage = ({ data }) => {
|
||||
messages.push(data);
|
||||
};
|
||||
socket.onopen = () => resolve([socket, messages]);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -41,12 +42,18 @@ function setup(
|
||||
};
|
||||
}
|
||||
|
||||
function flush(): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, 0));
|
||||
}
|
||||
|
||||
Deno.test(
|
||||
"broadcasts a message on connect to all clients",
|
||||
setup(async () => {
|
||||
const [s1, m1] = await connect(href);
|
||||
const [s2, m2] = await connect(href);
|
||||
|
||||
await flush();
|
||||
|
||||
assertEquals(m1, ["1 has joined!", "2 has joined!"]);
|
||||
assertEquals(m2, ["2 has joined!"]);
|
||||
|
||||
@@ -62,6 +69,8 @@ Deno.test(
|
||||
const [s2, m2] = await connect(href);
|
||||
const [s3, m3] = await connect(href);
|
||||
|
||||
await flush();
|
||||
|
||||
m1.length = 0;
|
||||
m2.length = 0;
|
||||
m3.length = 0;
|
||||
|
||||
Reference in New Issue
Block a user