![Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility](https://cdn.sanity.io/images/cgdhsj6q/production/97774ea8c88cc8f4bed2766c31994ebc38116948-1664x1366.png?w=400&fit=max&auto=format)
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@relaycorp/ws-mock
Advanced tools
Mock client and server to unit test the NPM package ws
.
npm install @relaycorp/ws-mock
You should initialise MockClient
by passing the ws
server to be tested and then call client.connect()
to initiate the connection. From that point you can interact with the server. For example:
test('Challenge should be sent as soon as client connects', async () => {
const client = new MockClient(wsServer);
await client.connect();
const challengeSerialized = await client.receive();
expect(challengeSerialized).toBeInstanceOf(ArrayBuffer);
});
You'll find real-world examples in relaycorp/relaynet-internet-gateway.
You basically need to initialise MockServer
and replace the default export from ws
with a mock WebSocket. Here's an example with Jest:
let mockServer: MockServer;
beforeEach(() => {
mockServer = new MockServer();
});
jest.mock('ws', () => ({
__esModule: true,
default: jest.fn().mockImplementation(() => mockServer.mockClientWebSocket),
}));
test('Server message should be played back', async () => {
const clientUnderTest = new ClientUnderTest();
const messageToEcho = 'foo';
await Promise.all([
clientUnderTest.connectToServerAndInteractWithIt(),
// Configure the mock server to accept the incoming connection and return a message straightaway
mockServer.runActions(
new AcceptConnectionAction(),
new SendMessageAction(messageToEcho),
),
]);
// Check that the client sent the message back to the server:
const clientResponse = await mockServer.receive();
expect(clientResponse).toEqual(messageToEcho);
});
You'll find real-world examples in relaycorp/relaynet-poweb-js.
When using streams in the unit under test, make sure to mock the createWebSocketStream
function in ws
. Here's an example with Jest:
import { createMockWebSocketStream } from '@relaycorp/ws-mock';
import WebSocket from 'ws';
jest
.spyOn(WebSocket, 'createWebSocketStream')
.mockImplementation(createMockWebSocketStream);
FAQs
Mock client and server to unit test the NPM package 'ws'
The npm package @relaycorp/ws-mock receives a total of 881 weekly downloads. As such, @relaycorp/ws-mock popularity was classified as not popular.
We found that @relaycorp/ws-mock demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.