From 94453ff28fa03e6596e895052531b4ecc69d7599 Mon Sep 17 00:00:00 2001 From: Attila Orosz Date: Tue, 20 Dec 2022 17:58:50 +0100 Subject: [PATCH 1/2] test: createSocketServer --- test/unit/createSocketServer.spec.ts | 40 ++++++++++++++++++++++++++ test/utilities/testSocketConnection.ts | 12 ++++++++ tsconfig.json | 2 +- tsconfig.spec.json | 3 ++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 test/unit/createSocketServer.spec.ts create mode 100644 test/utilities/testSocketConnection.ts diff --git a/test/unit/createSocketServer.spec.ts b/test/unit/createSocketServer.spec.ts new file mode 100644 index 00000000..1aaafcac --- /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"] } From 08fff0121251283cfc37188d73007c9c4aa7b1d4 Mon Sep 17 00:00:00 2001 From: Attila Orosz Date: Tue, 20 Dec 2022 18:00:07 +0100 Subject: [PATCH 2/2] fix: prettier fix to createSocketServer.spec.ts --- test/unit/createSocketServer.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/createSocketServer.spec.ts b/test/unit/createSocketServer.spec.ts index 1aaafcac..f44a28c1 100644 --- a/test/unit/createSocketServer.spec.ts +++ b/test/unit/createSocketServer.spec.ts @@ -1,4 +1,4 @@ -import { createSocketServer, SocketController } from "../../src"; +import { createSocketServer, SocketController } from '../../src'; import { testConnection } from '../utilities/testSocketConnection'; describe('createSocketServer', () => {