Skip to content

Commit 58725f0

Browse files
authored
test: createSocketServer (#510)
1 parent 471c5ca commit 58725f0

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { createSocketServer, SocketController } from '../../src';
2+
import { testConnection } from '../utilities/testSocketConnection';
3+
4+
describe('createSocketServer', () => {
5+
let wsApp: any;
6+
const PORT = 8080;
7+
const PATH_FOR_CLIENT = `ws://localhost:${PORT}`;
8+
9+
afterEach(() => {
10+
return new Promise(resolve => {
11+
if (wsApp)
12+
return wsApp.close(() => {
13+
resolve(null);
14+
});
15+
resolve(null);
16+
});
17+
});
18+
19+
it('should create socket server without options', async () => {
20+
expect.assertions(1);
21+
wsApp = await createSocketServer(PORT);
22+
expect(await testConnection(PATH_FOR_CLIENT)).toEqual(0);
23+
});
24+
25+
it('should create socket server with empty controllers array in options', async () => {
26+
expect.assertions(1);
27+
wsApp = await createSocketServer(PORT, { controllers: [] });
28+
expect(await testConnection(PATH_FOR_CLIENT)).toEqual(0);
29+
});
30+
31+
it('should create socket server with controllers array in options', async () => {
32+
expect.assertions(1);
33+
34+
@SocketController()
35+
class TestController {}
36+
37+
wsApp = await createSocketServer(PORT, { controllers: [TestController] });
38+
expect(await testConnection(PATH_FOR_CLIENT)).toEqual(0);
39+
});
40+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import socketio from 'socket.io-client';
2+
3+
export async function testConnection(path: string) {
4+
return await new Promise<number>((resolve, reject) => {
5+
const socket = socketio(path, { reconnection: false, timeout: 5000 });
6+
socket.on('connect', () => socket.disconnect());
7+
socket.on('connect_error', reject);
8+
socket.on('disconnect', () => {
9+
resolve(0);
10+
});
11+
});
12+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
"emitDecoratorMetadata": true,
1313
"forceConsistentCasingInFileNames": true
1414
},
15-
"exclude": ["node_modules", "sample", "**/*.spec.ts", "testing/**"]
15+
"exclude": ["node_modules", "sample", "**/*.spec.ts", "testing/**", "test/**"],
1616
}

tsconfig.spec.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"sourceMap": false,
77
"removeComments": true,
88
"noImplicitAny": false,
9+
"experimentalDecorators": true,
10+
"emitDecoratorMetadata": true,
11+
"rootDir": "./"
912
},
1013
"exclude": ["node_modules"]
1114
}

0 commit comments

Comments
 (0)