superwstest
Advanced tools
Comparing version 1.9.0 to 2.0.0
declare module 'superwstest' { | ||
import type { Server, ClientRequestArgs, IncomingMessage } from 'http'; | ||
import type WebSocket from 'ws'; | ||
import type { WebSocket, ClientOptions } from 'ws'; | ||
import type { SuperTest, Test } from 'supertest'; | ||
@@ -29,8 +29,13 @@ | ||
send(message: any, options?: { | ||
mask?: boolean | undefined; | ||
binary?: boolean | undefined; | ||
compress?: boolean | undefined; | ||
fin?: boolean | undefined; | ||
} | undefined): this; | ||
send( | ||
message: any, | ||
options?: | ||
| { | ||
mask?: boolean | undefined; | ||
binary?: boolean | undefined; | ||
compress?: boolean | undefined; | ||
fin?: boolean | undefined; | ||
} | ||
| undefined, | ||
): this; | ||
sendText(message: any): this; | ||
@@ -41,7 +46,7 @@ sendJson(message: JsonValue): this; | ||
wait(milliseconds: number): this; | ||
exec(fn: (ws: WebSocket) => (Promise<void> | void)): this; | ||
exec(fn: (ws: WebSocket) => Promise<void> | void): this; | ||
expectMessage<T>( | ||
conversion: (received: ReceivedMessage) => T, | ||
expected?: T | ((message: T) => (boolean | void)) | null | undefined, | ||
expected?: T | ((message: T) => boolean | void) | null | undefined, | ||
options?: ExpectMessageOptions | undefined, | ||
@@ -51,3 +56,3 @@ ): this; | ||
expectText( | ||
expected?: string | RegExp | ((message: string) => (boolean | void)) | undefined, | ||
expected?: string | RegExp | ((message: string) => boolean | void) | undefined, | ||
options?: ExpectMessageOptions | undefined, | ||
@@ -57,3 +62,3 @@ ): this; | ||
expectJson( | ||
expected?: JsonValue | ((message: any) => (boolean | void)) | undefined, | ||
expected?: JsonValue | ((message: any) => boolean | void) | undefined, | ||
options?: ExpectMessageOptions | undefined, | ||
@@ -63,3 +68,9 @@ ): this; | ||
expectBinary( | ||
expected?: Uint8Array | Buffer | ArrayBuffer | number[] | ((message: Uint8Array) => (boolean | void)) | undefined, | ||
expected?: | ||
| Uint8Array | ||
| Buffer | ||
| ArrayBuffer | ||
| number[] | ||
| ((message: Uint8Array) => boolean | void) | ||
| undefined, | ||
options?: ExpectMessageOptions | undefined, | ||
@@ -74,3 +85,3 @@ ): this; | ||
expectUpgrade(test: (upgradeResponse: IncomingMessage) => (boolean | void)): this; | ||
expectUpgrade(test: (upgradeResponse: IncomingMessage) => boolean | void): this; | ||
@@ -81,4 +92,8 @@ expectConnectionError(expectedCode?: number | string | null | undefined): Promise<WebSocket>; | ||
export interface SuperWSTest extends SuperTest<Test> { | ||
ws(path: string, options?: WebSocket.ClientOptions | ClientRequestArgs | undefined): WSChain; | ||
ws(path: string, protocols?: string | string[] | undefined, options?: WebSocket.ClientOptions | ClientRequestArgs | undefined): WSChain; | ||
ws(path: string, options?: ClientOptions | ClientRequestArgs | undefined): WSChain; | ||
ws( | ||
path: string, | ||
protocols?: string | string[] | undefined, | ||
options?: ClientOptions | ClientRequestArgs | undefined, | ||
): WSChain; | ||
} | ||
@@ -85,0 +100,0 @@ |
{ | ||
"name": "superwstest", | ||
"version": "1.9.0", | ||
"version": "2.0.0", | ||
"description": "supertest with added WebSocket capabilities", | ||
"files": [ | ||
"build/**/*", | ||
"index.d.ts" | ||
"src/**/*", | ||
"index.d.ts", | ||
"!**/.DS_Store" | ||
], | ||
"main": "build/index.js", | ||
"main": "build/superwstest.js", | ||
"module": "src/superwstest.mjs", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"build": "NODE_OPTIONS=--openssl-legacy-provider webpack --mode production", | ||
"lint": "eslint --format codeframe --ext mjs,jsx,js src", | ||
"test": "lean-test src --parallel-suites", | ||
"posttest": "npm run lint", | ||
"test:watch": "jest --watch" | ||
"format": "prettier --write .", | ||
"test": "lean-test test --parallel-suites && rollup --config rollup.config.js && test/run-package.sh && prettier --check ." | ||
}, | ||
@@ -40,14 +40,17 @@ "repository": { | ||
"@types/ws": "7.x || 8.x", | ||
"supertest": "4.x || 5.x || 6.x", | ||
"ws": "7.x || 8.x" | ||
}, | ||
"peerDependencies": { | ||
"supertest": "*" | ||
}, | ||
"peerDependenciesMeta": { | ||
"supertest": { | ||
"optional": true | ||
} | ||
}, | ||
"devDependencies": { | ||
"@neutrinojs/airbnb-base": "9.x", | ||
"@neutrinojs/library": "9.x", | ||
"eslint": "7.x", | ||
"lean-test": "1.x", | ||
"neutrino": "9.x", | ||
"webpack": "4.x", | ||
"webpack-cli": "3.x" | ||
"prettier": "2.5.1", | ||
"rollup": "2.x" | ||
} | ||
} |
# SuperWSTest | ||
Extends [supertest](https://github.com/visionmedia/supertest) with | ||
WebSocket capabilities. This is intended for testing servers which | ||
support both HTTP and WebSocket requests. | ||
Provides a [supertest](https://github.com/visionmedia/supertest)-compatible API | ||
for testing WebSockets. | ||
If supertest is installed, this package also exposes supertest's API for | ||
convenience when testing servers which provide both HTTP and WebSocket URLs. | ||
## Install dependency | ||
@@ -13,2 +15,8 @@ | ||
You can also optionally install supertest for access to `.get`, `.post`, etc.: | ||
```bash | ||
npm install --save-dev superwstest supertest | ||
``` | ||
## Usage | ||
@@ -62,4 +70,2 @@ | ||
Since this builds on supertest, all the HTTP checks are also available. | ||
As long as you add `server.close` in an `afterEach`, all connections | ||
@@ -69,2 +75,23 @@ will be closed automatically, so you do not need to close connections | ||
### Testing non-WebSocket endpoints | ||
If you have installed supertest, all the HTTP checks are also available | ||
by proxy: | ||
```javascript | ||
import request from 'superwstest'; | ||
import server from './myServer'; | ||
describe('My Server', () => { | ||
beforeEach((done) => server.listen(0, 'localhost', done)); | ||
afterEach((done) => server.close(done)); | ||
it('communicates via HTTP', async () => { | ||
await request(server) | ||
.get('/path') | ||
.expect(200); | ||
}); | ||
}); | ||
``` | ||
### Testing a remote webserver | ||
@@ -92,2 +119,6 @@ | ||
The server URL given should be http(s) rather than ws(s); this will | ||
provide compatibility with native supertest requests such as `post`, | ||
`get`, etc. and will be converted automatically as needed. | ||
Note that adding `request.closeAll()` to an `afterEach` will | ||
@@ -109,5 +140,3 @@ ensure connections are closed in all situations (including test | ||
const request = baseRequest.scoped(); | ||
afterEach(() => { | ||
request.closeAll(); | ||
}); | ||
afterEach(() => request.closeAll()); | ||
/* ... */ | ||
@@ -117,6 +146,2 @@ }); | ||
The server URL given should be http(s) rather than ws(s); this will | ||
provide compatibility with native supertest requests such as `post`, | ||
`get`, etc. and will be converted automatically as needed. | ||
## API | ||
@@ -510,2 +535,19 @@ | ||
### Why do I see "supertest dependency not found" errors? | ||
Older versions of this library bundled supertest by default because they | ||
used some functionality from it. The latest version does not require any | ||
functionality from supertest but remains API-compatible with it, and for | ||
this reason supertest has become optional and not included by default | ||
(reducing dependencies when testing WebSocket-only servers). | ||
To restore the ability to use `.get`, `.post`, etc. simply run: | ||
```shell | ||
npm install --save-dev supertest | ||
``` | ||
The presence of this package will detected automatically and the supertest | ||
API will be available via superwstest as before. | ||
### Why isn't `request(app)` supported? | ||
@@ -512,0 +554,0 @@ |
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
3
7
961
0
581
51335
3
+ Addedformidable@3.5.1(transitive)
+ Addedsuperagent@9.0.2(transitive)
+ Addedsupertest@7.0.0(transitive)
- Removedsupertest@4.x || 5.x || 6.x
- Removedformidable@2.1.2(transitive)
- Removedsemver@7.6.2(transitive)
- Removedsuperagent@8.1.2(transitive)
- Removedsupertest@6.3.4(transitive)