Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions test/unit/createSocketServer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { createSocketServer, SocketController } from '../../src';
import { testConnection } from '../utilities/testSocketConnection';

describe('createSocketServer', () => {
let wsApp: any;
const PORT = 8080;
const PATH_FOR_CLIENT = `ws://localhost:${PORT}`;

afterEach(() => {
return new Promise(resolve => {
if (wsApp)
return wsApp.close(() => {
resolve(null);
});
resolve(null);
});
});

it('should create socket server without options', async () => {
expect.assertions(1);
wsApp = await createSocketServer(PORT);
expect(await testConnection(PATH_FOR_CLIENT)).toEqual(0);
});

it('should create socket server with empty controllers array in options', async () => {
expect.assertions(1);
wsApp = await createSocketServer(PORT, { controllers: [] });
expect(await testConnection(PATH_FOR_CLIENT)).toEqual(0);
});

it('should create socket server with controllers array in options', async () => {
expect.assertions(1);

@SocketController()
class TestController {}

wsApp = await createSocketServer(PORT, { controllers: [TestController] });
expect(await testConnection(PATH_FOR_CLIENT)).toEqual(0);
});
});
12 changes: 12 additions & 0 deletions test/utilities/testSocketConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import socketio from 'socket.io-client';

export async function testConnection(path: string) {
return await new Promise<number>((resolve, reject) => {
const socket = socketio(path, { reconnection: false, timeout: 5000 });
socket.on('connect', () => socket.disconnect());
socket.on('connect_error', reject);
socket.on('disconnect', () => {
resolve(0);
});
});
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"emitDecoratorMetadata": true,
"forceConsistentCasingInFileNames": true
},
"exclude": ["node_modules", "sample", "**/*.spec.ts", "testing/**"]
"exclude": ["node_modules", "sample", "**/*.spec.ts", "testing/**", "test/**"],
}
3 changes: 3 additions & 0 deletions tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"sourceMap": false,
"removeComments": true,
"noImplicitAny": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"rootDir": "./"
},
"exclude": ["node_modules"]
}