@relaycorp/ws-mock
Advanced tools
Comparing version 1.1.1 to 1.2.0
export { MockClient } from './lib/MockClient'; | ||
export { MockConnection } from './lib/MockConnection'; | ||
export { MockPeer } from './lib/MockPeer'; | ||
export { CloseFrame } from './lib/CloseFrame'; | ||
export { createMockWebSocketStream } from './lib/stream'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MockPeer = exports.MockConnection = exports.MockClient = void 0; | ||
exports.createMockWebSocketStream = exports.MockPeer = exports.MockClient = void 0; | ||
var MockClient_1 = require("./lib/MockClient"); | ||
Object.defineProperty(exports, "MockClient", { enumerable: true, get: function () { return MockClient_1.MockClient; } }); | ||
var MockConnection_1 = require("./lib/MockConnection"); | ||
Object.defineProperty(exports, "MockConnection", { enumerable: true, get: function () { return MockConnection_1.MockConnection; } }); | ||
var MockPeer_1 = require("./lib/MockPeer"); | ||
Object.defineProperty(exports, "MockPeer", { enumerable: true, get: function () { return MockPeer_1.MockPeer; } }); | ||
var stream_1 = require("./lib/stream"); | ||
Object.defineProperty(exports, "createMockWebSocketStream", { enumerable: true, get: function () { return stream_1.createMockWebSocketStream; } }); | ||
//# sourceMappingURL=index.js.map |
@@ -6,7 +6,6 @@ "use strict"; | ||
const net_1 = require("net"); | ||
const MockConnection_1 = require("./MockConnection"); | ||
const MockPeer_1 = require("./MockPeer"); | ||
class MockClient extends MockPeer_1.MockPeer { | ||
constructor(wsServer, headers = {}) { | ||
super(new MockConnection_1.MockConnection()); | ||
super(); | ||
this.wsServer = wsServer; | ||
@@ -29,3 +28,3 @@ this.headers = headers; | ||
this.wsServer.once('connection', resolve); | ||
this.wsServer.emit('connection', this.peerConnection, incomingMessage); | ||
this.wsServer.emit('connection', this.peerWebSocket, incomingMessage); | ||
}); | ||
@@ -32,0 +31,0 @@ } |
@@ -5,6 +5,5 @@ /// <reference types="node" /> | ||
import { CloseFrame } from './CloseFrame'; | ||
import { MockConnection } from './MockConnection'; | ||
import { MockWebSocket } from './MockWebSocket'; | ||
export declare abstract class MockPeer extends EventEmitter { | ||
protected peerConnection: MockConnection; | ||
constructor(peerConnection: MockConnection); | ||
protected readonly peerWebSocket: MockWebSocket; | ||
get wasConnectionClosed(): boolean; | ||
@@ -11,0 +10,0 @@ disconnect(code?: number, reason?: string): void; |
@@ -5,15 +5,16 @@ "use strict"; | ||
const events_1 = require("events"); | ||
const MockWebSocket_1 = require("./MockWebSocket"); | ||
class MockPeer extends events_1.EventEmitter { | ||
constructor(peerConnection) { | ||
super(); | ||
this.peerConnection = peerConnection; | ||
constructor() { | ||
super(...arguments); | ||
this.peerWebSocket = new MockWebSocket_1.MockWebSocket(); | ||
} | ||
get wasConnectionClosed() { | ||
return this.peerConnection.closeFrame !== null; | ||
return this.peerWebSocket.closeFrame !== null; | ||
} | ||
disconnect(code, reason) { | ||
this.peerConnection.emit('close', code, reason); | ||
this.peerWebSocket.emit('close', code, reason); | ||
} | ||
abort(error) { | ||
this.peerConnection.emit('error', error); | ||
this.peerWebSocket.emit('error', error); | ||
} | ||
@@ -23,4 +24,4 @@ async send(message) { | ||
return new Promise((resolve) => { | ||
this.peerConnection.once('message', resolve); | ||
this.peerConnection.emit('message', message); | ||
this.peerWebSocket.once('message', resolve); | ||
this.peerWebSocket.emit('message', message); | ||
}); | ||
@@ -34,9 +35,9 @@ } | ||
} | ||
return this.peerConnection.getLastMessageSent(); | ||
return this.peerWebSocket.getLastMessageSent(); | ||
} | ||
popLastPeerMessage() { | ||
return this.peerConnection.popLastMessage(); | ||
return this.peerWebSocket.popLastMessage(); | ||
} | ||
async waitForPeerClosure() { | ||
return this.peerConnection.getCloseFrameWhenAvailable(); | ||
return this.peerWebSocket.getCloseFrameWhenAvailable(); | ||
} | ||
@@ -43,0 +44,0 @@ requireConnectionStillOpen() { |
export { MockClient } from './lib/MockClient'; | ||
export { MockConnection } from './lib/MockConnection'; | ||
export { MockPeer } from './lib/MockPeer'; | ||
export { CloseFrame } from './lib/CloseFrame'; | ||
export { createMockWebSocketStream } from './lib/stream'; |
export { MockClient } from './lib/MockClient'; | ||
export { MockConnection } from './lib/MockConnection'; | ||
export { MockPeer } from './lib/MockPeer'; | ||
export { createMockWebSocketStream } from './lib/stream'; | ||
//# sourceMappingURL=index.js.map |
import { IncomingMessage } from 'http'; | ||
import { Socket } from 'net'; | ||
import { MockConnection } from './MockConnection'; | ||
import { MockPeer } from './MockPeer'; | ||
export class MockClient extends MockPeer { | ||
constructor(wsServer, headers = {}) { | ||
super(new MockConnection()); | ||
super(); | ||
this.wsServer = wsServer; | ||
@@ -25,3 +24,3 @@ this.headers = headers; | ||
this.wsServer.once('connection', resolve); | ||
this.wsServer.emit('connection', this.peerConnection, incomingMessage); | ||
this.wsServer.emit('connection', this.peerWebSocket, incomingMessage); | ||
}); | ||
@@ -28,0 +27,0 @@ } |
@@ -5,6 +5,5 @@ /// <reference types="node" /> | ||
import { CloseFrame } from './CloseFrame'; | ||
import { MockConnection } from './MockConnection'; | ||
import { MockWebSocket } from './MockWebSocket'; | ||
export declare abstract class MockPeer extends EventEmitter { | ||
protected peerConnection: MockConnection; | ||
constructor(peerConnection: MockConnection); | ||
protected readonly peerWebSocket: MockWebSocket; | ||
get wasConnectionClosed(): boolean; | ||
@@ -11,0 +10,0 @@ disconnect(code?: number, reason?: string): void; |
import { EventEmitter } from 'events'; | ||
import { MockWebSocket } from './MockWebSocket'; | ||
export class MockPeer extends EventEmitter { | ||
constructor(peerConnection) { | ||
super(); | ||
this.peerConnection = peerConnection; | ||
constructor() { | ||
super(...arguments); | ||
this.peerWebSocket = new MockWebSocket(); | ||
} | ||
get wasConnectionClosed() { | ||
return this.peerConnection.closeFrame !== null; | ||
return this.peerWebSocket.closeFrame !== null; | ||
} | ||
disconnect(code, reason) { | ||
this.peerConnection.emit('close', code, reason); | ||
this.peerWebSocket.emit('close', code, reason); | ||
} | ||
abort(error) { | ||
this.peerConnection.emit('error', error); | ||
this.peerWebSocket.emit('error', error); | ||
} | ||
@@ -19,4 +20,4 @@ async send(message) { | ||
return new Promise((resolve) => { | ||
this.peerConnection.once('message', resolve); | ||
this.peerConnection.emit('message', message); | ||
this.peerWebSocket.once('message', resolve); | ||
this.peerWebSocket.emit('message', message); | ||
}); | ||
@@ -30,9 +31,9 @@ } | ||
} | ||
return this.peerConnection.getLastMessageSent(); | ||
return this.peerWebSocket.getLastMessageSent(); | ||
} | ||
popLastPeerMessage() { | ||
return this.peerConnection.popLastMessage(); | ||
return this.peerWebSocket.popLastMessage(); | ||
} | ||
async waitForPeerClosure() { | ||
return this.peerConnection.getCloseFrameWhenAvailable(); | ||
return this.peerWebSocket.getCloseFrameWhenAvailable(); | ||
} | ||
@@ -39,0 +40,0 @@ requireConnectionStillOpen() { |
{ | ||
"name": "@relaycorp/ws-mock", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "email": "no-reply@relaycorp.tech", |
@@ -46,1 +46,12 @@ # @relaycorp/ws-mock | ||
You'll find [real-world examples in relaycorp/relaynet-poweb-js](https://github.com/relaycorp/relaynet-poweb-js/search?l=TypeScript&q=%22%40relaycorp%2Fws-mock%22). | ||
## Using streams | ||
When using streams in the unit under test, make sure to mock the `createWebSocketStream` function in `ws`. Here's an example with Jest: | ||
```javascript | ||
import { createMockWebSocketStream } from '@relaycorp/ws-mock'; | ||
import WebSocket from 'ws'; | ||
jest.spyOn(WebSocket, 'createWebSocketStream').mockImplementation(createMockWebSocketStream); | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30823
45
457
57
5