diff --git a/test/unit/createSocketServer.spec.ts b/test/unit/createSocketServer.spec.ts new file mode 100644 index 00000000..f44a28c1 --- /dev/null +++ b/test/unit/createSocketServer.spec.ts @@ -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); + }); +}); diff --git a/test/utilities/testSocketConnection.ts b/test/utilities/testSocketConnection.ts new file mode 100644 index 00000000..9acdf34a --- /dev/null +++ b/test/utilities/testSocketConnection.ts @@ -0,0 +1,12 @@ +import socketio from 'socket.io-client'; + +export async function testConnection(path: string) { + return await new Promise((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); + }); + }); +} diff --git a/tsconfig.json b/tsconfig.json index b2e44268..6da0c9ac 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,5 +12,5 @@ "emitDecoratorMetadata": true, "forceConsistentCasingInFileNames": true }, - "exclude": ["node_modules", "sample", "**/*.spec.ts", "testing/**"] + "exclude": ["node_modules", "sample", "**/*.spec.ts", "testing/**", "test/**"], } diff --git a/tsconfig.spec.json b/tsconfig.spec.json index c0215c96..63343a56 100644 --- a/tsconfig.spec.json +++ b/tsconfig.spec.json @@ -6,6 +6,9 @@ "sourceMap": false, "removeComments": true, "noImplicitAny": false, + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "rootDir": "./" }, "exclude": ["node_modules"] }