Skip to content

Commit a7fe0e7

Browse files
committed
Make @types/supertest dependency optional
1 parent 8708dcd commit a7fe0e7

11 files changed

Lines changed: 121 additions & 17 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ npm install --save-dev superwstest
1515
You can also optionally install supertest for access to `.get`, `.post`, etc.:
1616

1717
```bash
18-
npm install --save-dev superwstest supertest
18+
npm install --save-dev superwstest supertest @types/supertest
1919
```
2020

2121
## Usage
2222

2323
### Example server implementation
2424

2525
```javascript
26-
import http from 'http';
26+
import http from 'node:http';
2727
import WebSocket from 'ws';
2828

2929
const server = http.createServer();
@@ -744,7 +744,7 @@ this reason supertest has become optional and not included by default
744744
To restore the ability to use `.get`, `.post`, etc. simply run:
745745

746746
```shell
747-
npm install --save-dev supertest
747+
npm install --save-dev supertest @types/supertest
748748
```
749749

750750
The presence of this package will detected automatically and the supertest

index.d.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
declare module 'superwstest' {
2-
import type { Server } from 'net';
3-
import type { ClientRequestArgs, IncomingMessage } from 'http';
2+
import type { Server } from 'node:net';
3+
import type { ClientRequestArgs, IncomingMessage } from 'node:http';
44
import type { WebSocket, ClientOptions, WebSocketServer } from 'ws';
5-
import type { SuperTest, Test } from 'supertest';
5+
6+
// import SuperTest type if available, else fall-back to SuperTest = {}
7+
// @ts-ignore
8+
import type { SuperTest as MaybeSuperTest } from 'supertest';
9+
type IfKnown<T, Fallback> = 0 extends 1 & T ? Fallback : T;
10+
type SuperTest = IfKnown<MaybeSuperTest, {}>;
611

712
type JsonObject = { [member: string]: JsonValue };
813
interface JsonArray extends Array<JsonValue> {}
@@ -120,7 +125,7 @@ declare module 'superwstest' {
120125
expectConnectionError(expectedCode?: number | string | null | undefined): Promise<WebSocket>;
121126
}
122127

123-
export interface SuperWSTest extends SuperTest<Test> {
128+
export interface SuperWSTest extends SuperTest {
124129
ws(path: string, options?: ClientOptions | ClientRequestArgs | undefined): WSChain;
125130
ws(
126131
path: string,

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,24 @@
3535
},
3636
"homepage": "https://github.com/davidje13/superwstest#readme",
3737
"dependencies": {
38-
"@types/supertest": "*",
3938
"@types/ws": "7.x || 8.x",
4039
"ws": "7.x || 8.x"
4140
},
4241
"peerDependencies": {
42+
"@types/supertest": "*",
4343
"supertest": "*"
4444
},
4545
"peerDependenciesMeta": {
46+
"@types/supertest": {
47+
"optional": true
48+
},
4649
"supertest": {
4750
"optional": true
4851
}
4952
},
5053
"devDependencies": {
5154
"lean-test": "2.x",
52-
"prettier": "3.6.2",
55+
"prettier": "3.8.3",
5356
"rollup": "4.x"
5457
}
5558
}

test/package-no-supertest/.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package-lock=false
2+
ignore-scripts=true
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "superwstest-without-supertest-package-test",
3+
"private": true,
4+
"scripts": {
5+
"test": "tsc"
6+
},
7+
"devDependencies": {
8+
"@types/node": "24.x",
9+
"superwstest": "file:superwstest.tgz",
10+
"typescript": "6.0.x"
11+
}
12+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"compilerOptions": {
3+
"types": ["node"],
4+
5+
"strict": true,
6+
"allowUnusedLabels": false,
7+
"allowUnreachableCode": false,
8+
"exactOptionalPropertyTypes": true,
9+
"noFallthroughCasesInSwitch": true,
10+
"noImplicitOverride": true,
11+
"noImplicitReturns": true,
12+
"noPropertyAccessFromIndexSignature": true,
13+
"noUncheckedIndexedAccess": true,
14+
"noUnusedLocals": true,
15+
"noUnusedParameters": true,
16+
17+
"isolatedModules": true,
18+
"verbatimModuleSyntax": true,
19+
20+
"lib": ["es2022"],
21+
"target": "es2022",
22+
"module": "esnext",
23+
"moduleResolution": "bundler",
24+
"noEmit": true
25+
},
26+
"typeAcquisition": {
27+
"enable": false
28+
},
29+
"include": ["typescript.ts"]
30+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import request from 'superwstest';
2+
3+
// this file just checks types; the code is not executed
4+
5+
request('hello')
6+
.ws('foo')
7+
.send('hi')
8+
.exec((ws) => ws.ping('blah'))
9+
.close();
10+
11+
// @ts-expect-error
12+
request('hello').get('/hi');

test/package/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
"name": "superwstest-package-test",
33
"private": true,
44
"scripts": {
5-
"test": "node commonjs.js && node es6modules.mjs && tsc typescript.ts --noEmit"
5+
"test": "node commonjs.js && node es6modules.mjs && tsc"
66
},
77
"devDependencies": {
8+
"@types/node": "24.x",
9+
"@types/supertest": "7.x",
810
"superwstest": "file:superwstest.tgz",
911
"supertest": "7.x",
10-
"typescript": "5.8.x"
12+
"typescript": "6.0.x"
1113
}
1214
}

test/package/tsconfig.json

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
{
22
"compilerOptions": {
3+
"types": ["node"],
4+
35
"strict": true,
4-
"noImplicitAny": true
6+
"allowUnusedLabels": false,
7+
"allowUnreachableCode": false,
8+
"exactOptionalPropertyTypes": true,
9+
"noFallthroughCasesInSwitch": true,
10+
"noImplicitOverride": true,
11+
"noImplicitReturns": true,
12+
"noPropertyAccessFromIndexSignature": true,
13+
"noUncheckedIndexedAccess": true,
14+
"noUnusedLocals": true,
15+
"noUnusedParameters": true,
16+
17+
"isolatedModules": true,
18+
"verbatimModuleSyntax": true,
19+
20+
"lib": ["es2022"],
21+
"target": "es2022",
22+
"module": "esnext",
23+
"moduleResolution": "bundler",
24+
"noEmit": true
525
},
6-
"include": ["**/*"],
7-
"exclude": ["node_modules"]
26+
"typeAcquisition": {
27+
"enable": false
28+
},
29+
"include": ["typescript.ts"]
830
}

test/package/typescript.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import request from 'superwstest';
21
import { createServer as httpCreateServer } from 'node:http';
32
import { createServer as httpsCreateServer } from 'node:https';
3+
import request from 'superwstest';
44
import { WebSocketServer } from 'ws';
55

66
// this file just checks types; the code is not executed
@@ -11,6 +11,8 @@ request('hello')
1111
.exec((ws) => ws.ping('blah'))
1212
.close();
1313

14+
request('hello').get('/hi');
15+
1416
request(httpCreateServer()).ws('foo');
1517
request(httpsCreateServer()).ws('foo');
1618
request(new WebSocketServer({ port: 0 })).ws('foo');
@@ -41,3 +43,6 @@ request('hello').ws(1);
4143

4244
// @ts-expect-error
4345
scopedRequest('hello').ws(1);
46+
47+
// @ts-expect-error
48+
request('hello').get(0);

0 commit comments

Comments
 (0)