superwstest
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -45,3 +45,3 @@ declare module 'superwstest' { | ||
wait(milliseconds: number): this; | ||
exec(fn: (ws: WebSocket) => Promise<void> | void): this; | ||
exec(fn: (ws: WebSocket) => Promise<unknown> | void): this; | ||
@@ -48,0 +48,0 @@ expectMessage<T>( |
{ | ||
"name": "superwstest", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "supertest with added WebSocket capabilities", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -513,2 +513,6 @@ # SuperWSTest | ||
See [the FAQ](#how-can-i-perform-another-asynchronous-task-while-a-connection-is-open) | ||
for examples of how `exec` can be used to perform side operations | ||
during a connection. | ||
## FAQ | ||
@@ -582,1 +586,55 @@ | ||
and any dangling connections will prevent the test process from terminating). | ||
### How can I perform another asynchronous task while a connection is open? | ||
Often when testing websockets, you will want to perform another action and | ||
check for a reaction on the websocket. This can be achieved using `exec`: | ||
```javascript | ||
await request(server).ws('here') | ||
.sendText('hello') | ||
.expectText('session open') | ||
.exec(async () => { | ||
await myOtherOperation(); | ||
}) | ||
.expectText('something happened') | ||
.close(); | ||
``` | ||
The recommended approach is to pull these out as helper functions, for example: | ||
```javascript | ||
const makeThing = (name) => () => request(server) | ||
.post('blah') | ||
.expect(200); | ||
await request(server).ws('here') | ||
.sendText('hello') | ||
.expectText('session open') | ||
.exec(makeThing('my first thing')) | ||
.expectText('made "my first thing"') | ||
.exec(makeThing('my second thing')) | ||
.expectText('made "my second thing"') | ||
.close(); | ||
``` | ||
If you need 2 websocket connections to interact with each other, you can use | ||
`Promise.all`: | ||
```javascript | ||
await Promise.all([ | ||
request(server).ws('here') | ||
.expectText('Welcome to the chat room') | ||
.sendText('Hi all! I am foo') | ||
.expectText('Hi foo, I am bar') | ||
.close(), | ||
request(server).ws('here') | ||
.expectText('Welcome to the chat room') | ||
.expectText('Hi all! I am foo') | ||
.sendText('Hi foo, I am bar') | ||
.close(), | ||
]); | ||
``` |
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
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
52834
639
2