@hyper-fetch/sockets 9.5.1 → 9.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -18,6 +18,11 @@ describe("Socket Adapter [ Connection ]", () => {
|
|
|
18
18
|
socket = createSocket(socketOptions);
|
|
19
19
|
});
|
|
20
20
|
|
|
21
|
+
afterEach(() => {
|
|
22
|
+
vi.clearAllTimers();
|
|
23
|
+
vi.useRealTimers();
|
|
24
|
+
});
|
|
25
|
+
|
|
21
26
|
afterAll(() => {
|
|
22
27
|
stopServer();
|
|
23
28
|
});
|
|
@@ -55,16 +60,19 @@ describe("Socket Adapter [ Connection ]", () => {
|
|
|
55
60
|
});
|
|
56
61
|
|
|
57
62
|
it("should reconnect when connection attempt takes too long", async () => {
|
|
63
|
+
// The socket from beforeEach has to finish connecting before the clock is replaced
|
|
64
|
+
await waitForConnection(socket);
|
|
65
|
+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
|
|
66
|
+
|
|
58
67
|
const spy = vi.fn();
|
|
59
68
|
const newUrl = "ws://test";
|
|
60
69
|
socket = createSocket({ url: newUrl, reconnectTime: 1 });
|
|
61
70
|
socket.events.onReconnecting(spy);
|
|
62
|
-
await socket.adapter.connect();
|
|
63
71
|
|
|
64
|
-
await
|
|
65
|
-
|
|
66
|
-
});
|
|
72
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
73
|
+
expect(spy).toHaveBeenCalledTimes(0);
|
|
67
74
|
|
|
75
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
68
76
|
expect(spy).toHaveBeenCalledTimes(1);
|
|
69
77
|
});
|
|
70
78
|
});
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { createSseMockingServer
|
|
2
|
-
import { waitFor } from "@testing-library/dom";
|
|
1
|
+
import { createSseMockingServer } from "@hyper-fetch/testing";
|
|
3
2
|
import { ServerSentEventsAdapter } from "adapter-sse";
|
|
4
3
|
|
|
5
4
|
import { createSocket } from "../../utils/socket.utils";
|
|
@@ -10,39 +9,53 @@ const socketOptions: Parameters<typeof createSocket>[0] = {
|
|
|
10
9
|
};
|
|
11
10
|
const { startServer } = createSseMockingServer();
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
/** Opens the mocked connection, `startServer` waits for a timer so the clock has to move for it to finish */
|
|
13
|
+
const openConnection = async (start: () => Promise<void>) => {
|
|
14
|
+
const opened = start();
|
|
15
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
16
|
+
await opened;
|
|
17
|
+
};
|
|
15
18
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
describe("Socket SSE [ Connection ]", () => {
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
|
|
19
22
|
vi.resetAllMocks();
|
|
20
23
|
});
|
|
21
24
|
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
// A socket that never opened reschedules its reconnection forever. Dropping the timer stops it from
|
|
27
|
+
// replacing the EventSource registered for the shared url while the next test is running.
|
|
28
|
+
vi.clearAllTimers();
|
|
29
|
+
vi.useRealTimers();
|
|
30
|
+
});
|
|
31
|
+
|
|
22
32
|
it("should auto connect", async () => {
|
|
23
33
|
const spy = vi.fn();
|
|
34
|
+
const socket = createSocket(socketOptions);
|
|
24
35
|
socket.events.onConnected(spy);
|
|
25
36
|
|
|
26
|
-
startServer
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
});
|
|
37
|
+
await openConnection(startServer);
|
|
38
|
+
|
|
39
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
30
40
|
});
|
|
31
41
|
|
|
32
42
|
it("should prevent initial connection", async () => {
|
|
33
43
|
const spy = vi.fn();
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
44
|
+
const url = "ws://localhost:2346";
|
|
45
|
+
const { getSource } = createSseMockingServer(url);
|
|
46
|
+
const socket = createSocket({ ...socketOptions, url, adapterOptions: { autoConnect: false } });
|
|
47
|
+
socket.events.onConnecting(spy);
|
|
48
|
+
|
|
49
|
+
await vi.advanceTimersByTimeAsync(10);
|
|
50
|
+
|
|
51
|
+
expect(spy).toHaveBeenCalledTimes(0);
|
|
52
|
+
expect(getSource()).toBeUndefined();
|
|
40
53
|
});
|
|
41
54
|
|
|
42
55
|
it("should reconnect when connection attempt takes too long", async () => {
|
|
43
|
-
const
|
|
44
|
-
const
|
|
45
|
-
const
|
|
56
|
+
const onReconnecting = vi.fn();
|
|
57
|
+
const onConnected = vi.fn();
|
|
58
|
+
const onConnecting = vi.fn();
|
|
46
59
|
const url = "ws://localhost:2345";
|
|
47
60
|
const { startServer: startNewServer } = createSseMockingServer(url);
|
|
48
61
|
const newSocket = createSocket({
|
|
@@ -51,17 +64,30 @@ describe("Socket SSE [ Connection ]", () => {
|
|
|
51
64
|
reconnectTime: 30,
|
|
52
65
|
adapterOptions: { autoConnect: false },
|
|
53
66
|
});
|
|
54
|
-
newSocket.events.onReconnecting(
|
|
55
|
-
newSocket.events.onConnected(
|
|
56
|
-
newSocket.events.onConnecting(
|
|
67
|
+
newSocket.events.onReconnecting(onReconnecting);
|
|
68
|
+
newSocket.events.onConnected(onConnected);
|
|
69
|
+
newSocket.events.onConnecting(onConnecting);
|
|
70
|
+
|
|
57
71
|
newSocket.adapter.connect();
|
|
58
|
-
await
|
|
59
|
-
startNewServer();
|
|
72
|
+
await vi.advanceTimersByTimeAsync(29);
|
|
60
73
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
74
|
+
expect(onConnecting).toHaveBeenCalledTimes(1);
|
|
75
|
+
expect(onReconnecting).toHaveBeenCalledTimes(0);
|
|
76
|
+
|
|
77
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
78
|
+
|
|
79
|
+
expect(onConnecting).toHaveBeenCalledTimes(3);
|
|
80
|
+
expect(onReconnecting).toHaveBeenCalledTimes(1);
|
|
81
|
+
|
|
82
|
+
await openConnection(startNewServer);
|
|
83
|
+
|
|
84
|
+
expect(onConnecting).toHaveBeenCalledTimes(4);
|
|
85
|
+
expect(onReconnecting).toHaveBeenCalledTimes(1);
|
|
86
|
+
expect(onConnected).toHaveBeenCalledTimes(1);
|
|
87
|
+
|
|
88
|
+
// The open connection cancelled the timeout of the attempt that succeeded
|
|
89
|
+
await vi.advanceTimersByTimeAsync(30);
|
|
90
|
+
|
|
91
|
+
expect(onReconnecting).toHaveBeenCalledTimes(1);
|
|
66
92
|
});
|
|
67
93
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/sockets",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.6.0",
|
|
4
4
|
"description": "Sockets connection for hyper-fetch",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/",
|
|
@@ -16,8 +16,7 @@
|
|
|
16
16
|
"lint": "oxlint .",
|
|
17
17
|
"format": "oxfmt --write .",
|
|
18
18
|
"typecheck": "tsgo --noEmit --emitDeclarationOnly false && tsgo --noEmit -p __tests__/tsconfig.json",
|
|
19
|
-
"tests": "pnpm lint-staged"
|
|
20
|
-
"release": "pnpm semantic-release --extends ../../release.config.js -t 'hyper-fetch-sockets-v${version}'"
|
|
19
|
+
"tests": "pnpm lint-staged"
|
|
21
20
|
},
|
|
22
21
|
"publishConfig": {
|
|
23
22
|
"access": "public",
|