refactor(api): websocket gateway unit tests

This commit is contained in:
yassinedorbozgithub 2025-05-12 10:47:58 +01:00
parent e9b7b8c229
commit 91434eb53e

View File

@ -7,7 +7,6 @@
*/ */
import { INestApplication } from '@nestjs/common'; import { INestApplication } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { Socket, io } from 'socket.io-client'; import { Socket, io } from 'socket.io-client';
import { import {
@ -27,11 +26,7 @@ describe('WebsocketGateway', () => {
beforeAll(async () => { beforeAll(async () => {
// Instantiate the app // Instantiate the app
const { module } = await buildTestingMocks({ const { module } = await buildTestingMocks({
providers: [ providers: [WebsocketGateway, SocketEventDispatcherService],
WebsocketGateway,
EventEmitter2,
SocketEventDispatcherService,
],
imports: [ imports: [
rootMongooseTestModule(({ uri, dbName }) => { rootMongooseTestModule(({ uri, dbName }) => {
process.env.MONGO_URI = uri; process.env.MONGO_URI = uri;
@ -51,7 +46,7 @@ describe('WebsocketGateway', () => {
query: { EIO: '4', transport: 'websocket', channel: 'web-channel' }, query: { EIO: '4', transport: 'websocket', channel: 'web-channel' },
}); });
app.listen(3000); await app.listen(3000);
}); });
afterAll(async () => { afterAll(async () => {
@ -64,22 +59,31 @@ describe('WebsocketGateway', () => {
}); });
it('should connect successfully', async () => { it('should connect successfully', async () => {
ioClient.on('connect', () => { const socketClient = ioClient.connect();
expect(true).toBe(true);
await new Promise<void>((resolve) => {
socketClient.on('connect', () => {
expect(true).toBe(true);
resolve();
});
}); });
ioClient.connect();
ioClient.disconnect(); socketClient.disconnect();
}); });
it('should emit "OK" on "healthcheck"', async () => { it('should emit "OK" on "healthcheck"', async () => {
ioClient.on('connect', () => { const socketClient = ioClient.connect();
ioClient.emit('healthcheck', 'Hello world');
ioClient.on('event', (data) => { await new Promise<void>((resolve) => {
expect(data).toBe('OK'); socketClient.on('connect', () => {
socketClient.emit('healthcheck', 'Hello world!');
socketClient.on('event', (data) => {
expect(data).toBe('OK');
resolve();
});
}); });
}); });
ioClient.connect();
ioClient.disconnect(); socketClient.disconnect();
}); });
}); });