Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
superwstest
Advanced tools
Extends supertest with WebSocket capabilities. This is intended for testing servers which support both HTTP and WebSocket requests.
npm install --save-dev superwstest
import http from 'http';
import WebSocket from 'ws';
const server = http.createServer();
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws) => {
ws.on('message', (message) => {
ws.send(`echo ${message}`);
});
ws.send('hello');
});
export default server;
import request from 'superwstest';
import server from './myServer';
describe('MyThing', () => {
beforeEach((done) => {
server.listen(0, 'localhost', done);
});
afterEach((done) => {
server.close(done);
});
it('communicates via websockets', async () => {
await request(server)
.ws('/path/ws')
.expectText('hello')
.sendText('foo')
.expectText('echo foo')
.sendText('abc')
.expectText('echo abc')
.close()
.expectClosed();
});
});
Since this builds on supertest, all the HTTP checks are also available.
As long as you add server.close
in an afterEach
, all connections
will be closed automatically, so you do not need to close connections
in every test.
The main entrypoint is request(myServer).ws(path)
. This returns a
Promise
(eventually returning the WebSocket
) with additional
methods attached:
expectText
: waits for the next message to arrive then checks that
it matches the given text or function.expectJson
: waits for the next message to arrive, deserialises it
using JSON.parse
, then checks that it matches the given data or
function.wait
: adds a delay of a number of milliseconds using setTimeout
.exec
: invokes the given function. If the function returns a
promise, waits for the promise to resolve.
note: this differs from then
because you can continue to chain
web socket actions and expectationssendText
: sends the given text.sendJson
: sends the given JSON as text using JSON.stringify
.send
: sends a raw message (accepts any types accepted by
WebSocket.send
).close
: closes the socket.expectClosed
: waits for the socket to be closed.expectConnectionError
: expect the initial connection handshake to
fail. Optionally checks for a specific HTTP status code.
note: if you use this, it must be the only invocation in the chainYou can also pass options to request
:
request(myServer, { shutdownDelay: 500 }).ws(path)
: wait up to
the given number of milliseconds for connections to close by
themselves before forcing a shutdown when close
is called on the
server. By default this is 0 (i.e. all connections are closed
immediately).FAQs
supertest with added WebSocket capabilities
The npm package superwstest receives a total of 3,759 weekly downloads. As such, superwstest popularity was classified as popular.
We found that superwstest 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
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.