vitest-websocket-mock 0.1.3 → 0.2.1

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 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
@@ -1,3 +1,4 @@
1
+ import { RawMatcherFn } from '@vitest/expect';
1
2
  import { Server, Client, CloseOptions, ServerOptions } from 'mock-socket';
2
3
 
3
4
  /**
@@ -20,9 +21,6 @@ interface WSOptions extends ServerOptions {
20
21
  jsonProtocol?: boolean;
21
22
  }
22
23
  type DeserializedMessage<TMessage = object> = string | TMessage;
23
- interface MockWebSocket extends Omit<Client, 'close'> {
24
- close(options?: CloseOptions): void;
25
- }
26
24
  declare class WS {
27
25
  server: Server;
28
26
  serializer: (deserializedMessage: DeserializedMessage) => string;
@@ -37,7 +35,7 @@ declare class WS {
37
35
  get connected(): Promise<Client>;
38
36
  get closed(): Promise<void>;
39
37
  get nextMessage(): Promise<unknown>;
40
- on(eventName: 'connection' | 'message' | 'close', callback: (socket: MockWebSocket) => void): void;
38
+ on(eventName: 'connection' | 'message' | 'close', callback: (socket: Client) => void): void;
41
39
  send(message: DeserializedMessage): void;
42
40
  close(options?: CloseOptions): void;
43
41
  error(options?: CloseOptions): void;
@@ -61,5 +59,7 @@ declare module 'vitest' {
61
59
  interface AsymmetricMatchersContaining extends CustomMatchers {
62
60
  }
63
61
  }
62
+ declare const deriveToReceiveMessage: (name: string, fn: RawMatcherFn) => RawMatcherFn;
63
+ declare const deriveToHaveReceivedMessage: (name: string, fn: RawMatcherFn) => RawMatcherFn;
64
64
 
65
- export { WS, WS as default };
65
+ export { WS, WS as default, deriveToHaveReceivedMessage, deriveToReceiveMessage };
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("timoeut");
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
- expect.extend({
149
- async toReceiveMessage(ws, expected, options) {
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, "toReceiveMessage")
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.toReceiveMessage" : ".toReceiveMessage", "WS", "expected") + `
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
- const received = messageOrTimeout;
174
- const pass = this.equals(received, expected);
175
- const message = pass ? () => this.utils.matcherHint(".not.toReceiveMessage", "WS", "expected") + `
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
- const diffString = diff(expected, received, { expand: this.expand });
182
- return this.utils.matcherHint(".toReceiveMessage", "WS", "expected") + `
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,56 +195,68 @@ Received:
189
195
  Difference:
190
196
 
191
197
  ${diffString}`;
192
- };
193
- return {
194
- actual: received,
195
- expected,
196
- message,
197
- name: "toReceiveMessage",
198
- pass
199
- };
200
- },
201
- toHaveReceivedMessages(ws, messages) {
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, "toHaveReceivedMessages")
213
+ message: makeInvalidWsMessage.bind(this, ws, name)
208
214
  };
209
215
  }
210
- const received = messages.map(
211
- (expected) => (
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
- ws.messages.some((actual) => this.equals(actual, expected))
226
+ received.some((receivedMsg) => this.equals(receivedMsg, expectedMsg))
214
227
  )
215
228
  );
216
- const pass = this.isNot ? received.some(Boolean) : received.every(Boolean);
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(messages)}
233
+ ${this.utils.printExpected(expected)}
221
234
  But it received:
222
- ${this.utils.printReceived(ws.messages)}` : () => {
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(messages)}
239
+ ${this.utils.printExpected(expected)}
227
240
  Received:
228
- ${this.utils.printReceived(ws.messages)}
241
+ ${this.utils.printReceived(received)}
229
242
 
230
243
  `;
231
244
  };
232
245
  return {
233
- actual: ws.messages,
234
- expected: messages,
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,
243
- WS as default
259
+ WS as default,
260
+ deriveToHaveReceivedMessage,
261
+ deriveToReceiveMessage
244
262
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitest-websocket-mock",
3
- "version": "0.1.3",
3
+ "version": "0.2.1",
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.0"
58
+ "mock-socket": "^9.2.1"
59
59
  },
60
60
  "files": [
61
61
  "dist",