-
Notifications
You must be signed in to change notification settings - Fork 18
/
livereload_server_test.ts
42 lines (38 loc) · 1.35 KB
/
livereload_server_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { livereloadServer } from "./livereload_server.ts";
import {
assert,
assertEquals,
assertStringIncludes,
deferred,
} from "./test_deps.ts";
Deno.test("livereloadServer", async () => {
const port = 34567;
const eventtarget = new EventTarget();
const server = livereloadServer(port, eventtarget);
try {
const wsURL = `ws://localhost:${port}`;
// Livereload server should serve livereload.js
const res = await fetch(`http://localhost:${port}/livereload.js`);
const text = await res.text();
assertStringIncludes(text, `new WebSocket("${wsURL}/livereload")`);
// Livereload server should accept websocket requests
const done = deferred<void>();
let receivedMessage: string | undefined;
const ws = new WebSocket(`${wsURL}/livereload`);
// Livereload server automatically closes a websocket connection when 'built' event is dispatched
ws.onclose = () => done.resolve();
ws.onopen = async () => {
await new Promise((resolve) => setTimeout(resolve, 200));
eventtarget.dispatchEvent(new Event("built"));
};
ws.onmessage = (ev: MessageEvent<string>) => {
receivedMessage = ev.data;
};
ws.onerror = (ev) => done.reject(ev);
await done;
assert(receivedMessage);
assertEquals(JSON.parse(receivedMessage), { type: "reload" });
} finally {
await server.close();
}
});