vitest-websocket-mock 0.1.3 → 0.2.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.
- package/LICENSE +1 -23
- package/dist/index.d.ts +1 -4
- package/dist/index.js +48 -32
- package/package.json +2 -2
package/LICENSE
CHANGED
|
@@ -1,27 +1,5 @@
|
|
|
1
|
-
Copyright 2023 Akiomi Kamakura
|
|
2
|
-
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
in the Software without restriction, including without limitation the rights
|
|
6
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
furnished to do so, subject to the following conditions:
|
|
9
|
-
|
|
10
|
-
The above copyright notice and this permission notice shall be included in all
|
|
11
|
-
copies or substantial portions of the Software.
|
|
12
|
-
|
|
13
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
-
SOFTWARE.
|
|
20
|
-
|
|
21
|
-
The major part of vitest-websocket-mock implementation is based on jest-webosocket-mock, which is subject to the same license.
|
|
22
|
-
Here is the original copyright notice for jest-webosocket-mock:
|
|
23
|
-
|
|
24
1
|
Copyright 2018 Romain Bertrand
|
|
2
|
+
Copyright 2023 Akiomi Kamakura
|
|
25
3
|
|
|
26
4
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
27
5
|
of this software and associated documentation files (the "Software"), to deal
|
package/dist/index.d.ts
CHANGED
|
@@ -20,9 +20,6 @@ interface WSOptions extends ServerOptions {
|
|
|
20
20
|
jsonProtocol?: boolean;
|
|
21
21
|
}
|
|
22
22
|
type DeserializedMessage<TMessage = object> = string | TMessage;
|
|
23
|
-
interface MockWebSocket extends Omit<Client, 'close'> {
|
|
24
|
-
close(options?: CloseOptions): void;
|
|
25
|
-
}
|
|
26
23
|
declare class WS {
|
|
27
24
|
server: Server;
|
|
28
25
|
serializer: (deserializedMessage: DeserializedMessage) => string;
|
|
@@ -37,7 +34,7 @@ declare class WS {
|
|
|
37
34
|
get connected(): Promise<Client>;
|
|
38
35
|
get closed(): Promise<void>;
|
|
39
36
|
get nextMessage(): Promise<unknown>;
|
|
40
|
-
on(eventName: 'connection' | 'message' | 'close', callback: (socket:
|
|
37
|
+
on(eventName: 'connection' | 'message' | 'close', callback: (socket: Client) => void): void;
|
|
41
38
|
send(message: DeserializedMessage): void;
|
|
42
39
|
close(options?: CloseOptions): void;
|
|
43
40
|
error(options?: CloseOptions): void;
|
package/dist/index.js
CHANGED
|
@@ -137,7 +137,7 @@ var WS = class _WS {
|
|
|
137
137
|
|
|
138
138
|
// src/matchers.ts
|
|
139
139
|
var WAIT_DELAY = 1e3;
|
|
140
|
-
var TIMEOUT = Symbol("
|
|
140
|
+
var TIMEOUT = Symbol("timeout");
|
|
141
141
|
var makeInvalidWsMessage = function makeInvalidWsMessage2(ws, matcher) {
|
|
142
142
|
return this.utils.matcherHint(this.isNot ? `.not.${matcher}` : `.${matcher}`, "WS", "expected") + `
|
|
143
143
|
|
|
@@ -145,14 +145,14 @@ Expected the websocket object to be a valid WS mock.
|
|
|
145
145
|
Received: ${typeof ws}
|
|
146
146
|
${this.utils.printReceived(ws)}`;
|
|
147
147
|
};
|
|
148
|
-
|
|
149
|
-
async
|
|
148
|
+
var deriveToReceiveMessage = (name, fn) => {
|
|
149
|
+
return async function(ws, expected, options) {
|
|
150
150
|
const isWS = ws instanceof WS;
|
|
151
151
|
if (!isWS) {
|
|
152
152
|
return {
|
|
153
153
|
pass: this.isNot,
|
|
154
154
|
// always fail
|
|
155
|
-
message: makeInvalidWsMessage.bind(this, ws,
|
|
155
|
+
message: makeInvalidWsMessage.bind(this, ws, name)
|
|
156
156
|
};
|
|
157
157
|
}
|
|
158
158
|
const waitDelay = options?.timeout ?? WAIT_DELAY;
|
|
@@ -164,22 +164,28 @@ expect.extend({
|
|
|
164
164
|
return {
|
|
165
165
|
pass: this.isNot,
|
|
166
166
|
// always fail
|
|
167
|
-
message: () => this.utils.matcherHint(this.isNot ? ".not
|
|
167
|
+
message: () => this.utils.matcherHint(`${this.isNot ? ".not" : ""}.${name}`, "WS", "expected") + `
|
|
168
168
|
|
|
169
169
|
Expected the websocket server to receive a message,
|
|
170
170
|
but it didn't receive anything in ${waitDelay}ms.`
|
|
171
171
|
};
|
|
172
|
+
} else {
|
|
173
|
+
const received = messageOrTimeout;
|
|
174
|
+
const result = await Promise.resolve(fn.call(this, received, expected, options));
|
|
175
|
+
return { name, ...result };
|
|
172
176
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
var toReceiveMessage = deriveToReceiveMessage("toReceiveMessage", function(received, expected) {
|
|
180
|
+
const pass = this.equals(received, expected);
|
|
181
|
+
const message = pass ? () => this.utils.matcherHint(".not.toReceiveMessage", "WS", "expected") + `
|
|
176
182
|
|
|
177
183
|
Expected the next received message to not equal:
|
|
178
184
|
${this.utils.printExpected(expected)}
|
|
179
185
|
Received:
|
|
180
186
|
${this.utils.printReceived(received)}` : () => {
|
|
181
|
-
|
|
182
|
-
|
|
187
|
+
const diffString = diff(expected, received, { expand: this.expand });
|
|
188
|
+
return this.utils.matcherHint(".toReceiveMessage", "WS", "expected") + `
|
|
183
189
|
|
|
184
190
|
Expected the next received message to equal:
|
|
185
191
|
${this.utils.printExpected(expected)}
|
|
@@ -189,54 +195,64 @@ Received:
|
|
|
189
195
|
Difference:
|
|
190
196
|
|
|
191
197
|
${diffString}`;
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
198
|
+
};
|
|
199
|
+
return {
|
|
200
|
+
actual: received,
|
|
201
|
+
expected,
|
|
202
|
+
message,
|
|
203
|
+
pass
|
|
204
|
+
};
|
|
205
|
+
});
|
|
206
|
+
var deriveToHaveReceivedMessage = (name, fn) => {
|
|
207
|
+
return function(ws, expected, options) {
|
|
202
208
|
const isWS = ws instanceof WS;
|
|
203
209
|
if (!isWS) {
|
|
204
210
|
return {
|
|
205
211
|
pass: this.isNot,
|
|
206
212
|
// always fail
|
|
207
|
-
message: makeInvalidWsMessage.bind(this, ws,
|
|
213
|
+
message: makeInvalidWsMessage.bind(this, ws, name)
|
|
208
214
|
};
|
|
209
215
|
}
|
|
210
|
-
const
|
|
211
|
-
|
|
216
|
+
const result = fn.call(this, ws.messages, expected, options);
|
|
217
|
+
return { name, ...result };
|
|
218
|
+
};
|
|
219
|
+
};
|
|
220
|
+
var toHaveReceivedMessages = deriveToHaveReceivedMessage(
|
|
221
|
+
"toHaveReceivedMessages",
|
|
222
|
+
function(received, expected) {
|
|
223
|
+
const equalities = expected.map(
|
|
224
|
+
(expectedMsg) => (
|
|
212
225
|
// object comparison to handle JSON protocols
|
|
213
|
-
|
|
226
|
+
received.some((receivedMsg) => this.equals(receivedMsg, expectedMsg))
|
|
214
227
|
)
|
|
215
228
|
);
|
|
216
|
-
const pass = this.isNot ?
|
|
229
|
+
const pass = this.isNot ? equalities.some(Boolean) : equalities.every(Boolean);
|
|
217
230
|
const message = pass ? () => this.utils.matcherHint(".not.toHaveReceivedMessages", "WS", "expected") + `
|
|
218
231
|
|
|
219
232
|
Expected the WS server to not have received the following messages:
|
|
220
|
-
${this.utils.printExpected(
|
|
233
|
+
${this.utils.printExpected(expected)}
|
|
221
234
|
But it received:
|
|
222
|
-
${this.utils.printReceived(
|
|
235
|
+
${this.utils.printReceived(received)}` : () => {
|
|
223
236
|
return this.utils.matcherHint(".toHaveReceivedMessages", "WS", "expected") + `
|
|
224
237
|
|
|
225
238
|
Expected the WS server to have received the following messages:
|
|
226
|
-
${this.utils.printExpected(
|
|
239
|
+
${this.utils.printExpected(expected)}
|
|
227
240
|
Received:
|
|
228
|
-
${this.utils.printReceived(
|
|
241
|
+
${this.utils.printReceived(received)}
|
|
229
242
|
|
|
230
243
|
`;
|
|
231
244
|
};
|
|
232
245
|
return {
|
|
233
|
-
actual:
|
|
234
|
-
expected
|
|
246
|
+
actual: received,
|
|
247
|
+
expected,
|
|
235
248
|
message,
|
|
236
|
-
name: "toHaveReceivedMessages",
|
|
237
249
|
pass
|
|
238
250
|
};
|
|
239
251
|
}
|
|
252
|
+
);
|
|
253
|
+
expect.extend({
|
|
254
|
+
toReceiveMessage,
|
|
255
|
+
toHaveReceivedMessages
|
|
240
256
|
});
|
|
241
257
|
export {
|
|
242
258
|
WS,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vitest-websocket-mock",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Mock websockets and assert complex websocket interactions with Vitest",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
57
|
"jest-diff": "^29.2.0",
|
|
58
|
-
"mock-socket": "^9.1
|
|
58
|
+
"mock-socket": "^9.2.1"
|
|
59
59
|
},
|
|
60
60
|
"files": [
|
|
61
61
|
"dist",
|