Skip to content

Commit

Permalink
Pin the dependency on mock-socket
Browse files Browse the repository at this point in the history
  • Loading branch information
romgain committed Jan 19, 2022
1 parent 2a4592a commit 3af06d9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ In particular:

## Install

[Mock Socket](https://github.com/thoov/mock-socket) is a peer dependency and
needs to be installed alongside `jest-websocket-mock`:

```bash
npm install --save-dev jest-websocket-mock mock-socket
npm install --save-dev jest-websocket-mock
```

## Mock a websocket server
Expand Down
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,15 @@
"@types/jest": "^27.0.0",
"babel-jest": "^27.0.2",
"jest": "^27.0.4",
"mock-socket": "^9.0.2",
"prettier": "^2.0.2",
"rimraf": "^3.0.0",
"rollup": "^2.0.3",
"rollup-plugin-babel": "^4.0.3",
"rollup-plugin-node-resolve": "^5.2.0",
"typescript": "^4.0.2"
},
"peerDependencies": {
"mock-socket": "^8||^9"
},
"dependencies": {
"jest-diff": "^27.0.2"
"jest-diff": "^27.0.2",
"mock-socket": "^9.1.0"
}
}
16 changes: 8 additions & 8 deletions src/websocket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Server, WebSocket, ServerOptions, CloseOptions } from "mock-socket";
import { Server, ServerOptions, CloseOptions, Client } from "mock-socket";
import Queue from "./queue";
import act from "./act-compat";

Expand All @@ -13,7 +13,7 @@ export type DeserializedMessage<TMessage = object> = string | TMessage;
// a WebSocket proxy that overrides the signature of the `close` method.
// To work around this inconsistency, we need to override the WebSocket
// interface. See https://github.com/romgain/jest-websocket-mock/issues/26#issuecomment-571579567
interface MockWebSocket extends Omit<WebSocket, "close"> {
interface MockWebSocket extends Omit<Client, "close"> {
close(options?: CloseOptions): void;
}

Expand All @@ -26,7 +26,7 @@ export default class WS {
messages: Array<DeserializedMessage> = [];
messagesToConsume = new Queue();

private _isConnected: Promise<WebSocket>;
private _isConnected: Promise<Client>;
private _isClosed: Promise<{}>;

static clean() {
Expand All @@ -44,16 +44,16 @@ export default class WS {
this.serializer = jsonProtocol ? JSON.stringify : identity;
this.deserializer = jsonProtocol ? JSON.parse : identity;

let connectionResolver: (socket: WebSocket) => void,
closedResolver!: (socket: WebSocket) => void;
let connectionResolver: (socket: Client) => void,
closedResolver!: (socket: Client) => void;
this._isConnected = new Promise((done) => (connectionResolver = done));
this._isClosed = new Promise((done) => (closedResolver = done));

this.server = new Server(url, serverOptions);

this.server.on("close", closedResolver);

this.server.on("connection", (socket: WebSocket) => {
this.server.on("connection", (socket: Client) => {
connectionResolver(socket);

socket.on("message", (message) => {
Expand All @@ -65,8 +65,8 @@ export default class WS {
}

get connected() {
let resolve: (socket: WebSocket) => void;
const connectedPromise = new Promise<WebSocket>((done) => (resolve = done));
let resolve: (socket: Client) => void;
const connectedPromise = new Promise<Client>((done) => (resolve = done));
const waitForConnected = async () => {
await act(async () => {
await this._isConnected;
Expand Down

0 comments on commit 3af06d9

Please sign in to comment.